Flapjacks基本的Java代码;创建方法? [英] Flapjacks basic java code; Create methods?

查看:101
本文介绍了Flapjacks基本的Java代码;创建方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

FlapJacks


在本练习中,您将模拟一堆被翻转的烙饼.堆栈实际上只是一个整数数组.烙饼始终以常规编号顺序开始.例如,一堆10个烙饼看起来像这样:

0
1
2
3
4
5
6
7
8
9

现在,您将通过输入数字来让您的用户翻转"所有或一些烙饼,这是放置小铲"的数字的索引.例如,如果我要翻转前6个烙饼,则将其称为索引5的翻转,这将使烙饼堆栈变为如下所示:


5
4
3
2
1
0
6
7
8
9

现在,我可以在索引7处翻转此堆栈以获得该堆栈:


7
6
0
1
2
3
4
5
8
9

您的任务包括两种方法.您必须首先弄清楚如何执行翻转操作,然后应该创建一种方法来弄清楚如何将任何混合堆栈翻转回原始顺序.您不能只创建一个已排序的新数组并返回该数组.您必须将堆栈翻转"回已排序的顺序.您也不应随机翻转,直到对其进行排序.尽管有机会最终实现,但是对于大型堆栈将花费很长时间.您的代码应调用flip方法和提供给您的find方法.

这是一个逻辑难题.如果您能弄清楚算法,则代码很简单...

该文件中除了您要使用的两种方法外,这些代码都在该文件中:FlapJacks.java(我在下面包括了代码)


导入javax.swing.*;
公共类FlapJacks {

公共静态void main(String [] args){
//在此处输入您想要的任何数字.这是
//要翻转的煎饼"数.
final int STACK_HEIGHT = 10;
int [] stack = getArray(STACK_HEIGHT);
printStack(stack);
字符串quit ="y";
while(quit.equals("y")){
字符串s = JOptionPane.showInputDialog(null,我应该在哪里翻转?");
int i = Integer.parseInt(s);
stack = flip(stack,i);
printStack(stack);
quit = JOptionPane.showInputDialog(null,键入y以继续翻转.");
}
stack = sortStack(stack);
printStack(stack);
System.exit(0);
}

//创建一个由len个项目组成的数组,其中第一个数字为0,第二个为
//数字为1,依此类推.
公共静态int [] getArray(int len){
int []答案=新的int [len];
for(int i = 0; i< len; i ++){
答案[i] = i;
}
返回答案;
}

//垂直打印数组的所有元素
公共静态无效printStack(int [] stack){
字符串toPrint =";
for(int i = 0; i< stack.length; i ++){
toPrint = toPrint + stack [i] +''\ n'';
}
JOptionPane.showMessageDialog(null,toPrint);
}

//在数组中找到某个数字的索引
//返回编号为
的索引 //找到,如果找不到该号码,则返回-1.
public static int find(int numToFind,int [] array){
for(int i = 0; i< array.length; i ++){
如果(array [i] == numToFind){
返回我;
}
}
返回-1;
}

//高是放置小铲"并翻转
的索引 //叠加.
public static int [] flip(int [] stack,int high){

}

//执行一系列翻转,直到堆栈按顺序返回.
公共静态int [] sortStack(int [] stack){

}
}

FlapJacks


For this exercise, you will simulate a stack of flapjacks that gets flipped. The stack is really just an array of integers. The flapjacks always start out in regular numbered order. For example, a stack of 10 flapjacks would look like this:

0
1
2
3
4
5
6
7
8
9

Now, you will let your user "flip" all or some of the flapjacks by entering a number, which is the index of the number under which to put the "spatula". For example, if I want to flip the first 6 flapjacks I would call flip of index 5, which would change my flapjack stack to look like this:


5
4
3
2
1
0
6
7
8
9

Now, I can flip this stack at index 7 to get this stack:


7
6
0
1
2
3
4
5
8
9

Your mission consists of two methods. You have to first figure out how to perform the flip operation, then you should create a method that figures out how to flip any mixed-up stack back into original order. You CANNOT just make a new array that is sorted and return that. You must "flip" the stack back to sorted order. You should also not flip randomly until it is sorted. Although chances are that this will eventually work, it will take a very long time for large stacks. Your code should call the flip method and the find method that is given to you.

This is a logic puzzle. The code is easy if you can figure out the algorithm...

The code, except for the two methods for you to make, is in this file: FlapJacks.java (I included the code below)


import javax.swing.*;
public class FlapJacks {

public static void main(String[] args) {
// Put whatever number you want here. This is the
// number of "pancakes" to flip.
final int STACK_HEIGHT = 10;
int[] stack = getArray(STACK_HEIGHT);
printStack(stack);
String quit = "y";
while(quit.equals("y")){
String s = JOptionPane.showInputDialog(null, "Where should I flip?");
int i = Integer.parseInt(s);
stack = flip(stack,i);
printStack(stack);
quit = JOptionPane.showInputDialog(null, "Type y to continue flipping.");
}
stack = sortStack(stack);
printStack(stack);
System.exit(0);
}

// Create an array of len items, where the first number is 0, the second
// number is 1, and so forth.
public static int[] getArray(int len){
int[] answer = new int[len];
for(int i=0; i<len; i++){
answer[i] = i;
}
return answer;
}

// Prints all the elements of an array vertically
public static void printStack(int[] stack){
String toPrint = "";
for(int i = 0; i<stack.length; i++){
toPrint = toPrint + stack[i] + ''\n'';
}
JOptionPane.showMessageDialog(null,toPrint);
}

// Find the index of a certain number in the array
// Returns either the index where the number was
// found, or -1 if the number wasn''t found.
public static int find(int numToFind, int[] array){
for(int i = 0; i<array.length; i++){
if (array[i] == numToFind){
return i;
}
}
return -1;
}

// High is the index under which I put my "spatula" and flip the
// stack over.
public static int[] flip(int[] stack, int high){

}

// Perform a series of flips until the stack is back in order.
public static int[] sortStack(int[] stack){

}
}

推荐答案

重新发布Java论坛中已经回答的内容.
Repost that is already answered in the Java forum.


这篇关于Flapjacks基本的Java代码;创建方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆