我的代码中的一个错误。我找不到那个
An error in my codes. I cant find that
import java.io.*;
import java.util.Scanner;
public class Helloworld{
public static BufferedReader input=new BufferedReader(new InputStreamReader(System.in));
public static void main(String[]args) throws IOException{
System.out.print('\u000C');
//variables
String lev,tower,disk;
int [] slot;
slot= new int [9];
slot[0]=1;
slot[3]=2;
slot[6]=3;
System.out.println("Press enter to continue...");
Scanner keyboard = new Scanner(System.in);
keyboard.nextLine();
System.out.print('\u000C');
System.out.println("Select Levels:");
System.out.println("[1]Level 1 <3 numbers>");
System.out.println("[2]Level 2 <4 numbers>");
System.out.println("[3]Level 3 <5 numbers>");
System.out.println("[4]Instructions");
System.out.print("Choose one: ");
lev =(input.readLine());
if (lev.equals("4")){
System.out.println("* You must transfer all numbers from tower 1 - tower 3 in ascending order.");
System.out.println("* For every move you can only move one number to a certain.");
System.out.println("* The number on the top of the tower are the one that you can move only.");
}
else if (lev.equals("1")){
//loop
while (true) {
System.out.print('\u000C');
//display
System.out.println(" A , B , C ");
System.out.println("| " +slot[0]+ " | " +slot [1]+ " | " +slot[2]+ " |");
System.out.println("| " +slot[3]+ " | " +slot [4]+ " | " +slot[5]+ " |");
System.out.println("| " +slot[6]+ " | " +slot [7]+ " | " +slot[8]+ " |");
// input a disk for user
System.out.println("What number do you want to move <1, 2, 3, e to exit>: ");
disk = (input.readLine());
if (disk.equals("e")) {
break;
}
// input tower where the disk will contain.
System.out.println("What tower do you want to put <A, B, C>: ");
tower = (input.readLine());
// Possible moves for user
if (disk.equals("1") && tower.equals("b")){
slot[7]=slot[0];
slot[0]=0;
System.out.println("Hello");
}
else if (disk.equals("2") && tower.equals("c")){
slot[8]=slot[3]; slot[3]=0;
}
if (disk.equals("3") && tower.equals("b")){
slot[5]=slot[7]; slot[7]=0;
}
else if (disk.equals("1") && tower.equals("b")){
slot[5]=slot[4]; slot[4]=0;
}
else if (disk.equals("1") && tower.equals("a")){
slot[6]=slot[5]; slot[5]=0;
}
else if (disk.equals("2") && tower.equals("b")){
slot[4]=slot[8]; slot[8]=0;
}
else if (disk.equals("2") && tower.equals("a")){
slot[2]=slot[6]; slot[6]=0;
}
else if (disk.equals("1") && tower.equals("a")){
slot[3]=slot[4]; slot[4]=0;
}//
else if (disk.equals("1") && tower.equals("b")){
slot[2]=slot[6]; slot[6]=0;
}
else if (disk.equals("3") && tower.equals("c")){
slot[8]=slot[7]; slot[7]=0;
}
else if (disk.equals("1") && tower.equals("b")){
slot[7]=slot[3]; slot[3]=0;
}
else if (disk.equals("2") && tower.equals("c")){
slot[5]=slot[6]; slot[6]=0;
}
else if (disk.equals("1") && tower.equals("c")){
slot[2]=slot[7]; slot[7]=0;
}
}
}
}
}
我不知道为什么 error.I 不能将 1 放在 2 的顶部。当我将 1 放在 2 的顶部时,2 变成 0。那是汉诺塔。我想创建一个名为河内塔的游戏。我想让用户选择 he/she 想要移动的内容。那是第一个输入。第二个输入是我想用户 he/she 想要放什么塔。
切勿使用 GOTO!
如果我没理解错的话,你想在每一步之后提示用户输入新的内容。所以你想把它放在一个循环中
类似于
while(continueProgramm){
System.out.println("What number do you want to move <1, 2, 3>: ");
disk =(input.readLine());
System.out.println("What tower do you want to put <A, B, C>: ");
tower =(input.readLine());
//handle your input here ...
}
在 continue 语句中使用 while 循环。
if(disk.equals("1") && tower.equals("b")) {
slot[0] = slot[8];
continue;
}
如 fge 所述,goto
是 Java 中的保留字,但未实现。在大多数语言中,使用 goto
通常被认为是不好的编程习惯。你应该使用一个循环。具有突破条件的无限 while
循环应该足够了:
while (true) {
System.out.print('\u000C');
System.out.println(" A , B , C ");
System.out.println("| " +slot[0]+ " | " +slot [1]+ " | " +slot[2]+ " |");
System.out.println("| " +slot[3]+ " | " +slot [4]+ " | " +slot[5]+ " |");
System.out.println("| " +slot[6]+ " | " +slot [7]+ " | " +slot[8]+ " |");
System.out.println("What number do you want to move <1, 2, 3, e to exit>: ");
disk = (input.readLine());
if (disk.equals("e")) {
break;
}
System.out.println("What tower do you want to put <A, B, C>: ");
tower = (input.readLine());
// First Move
if (disk.equals("1") && tower.equals("b")) {
slot[0] = slot[8];
}
}
while (condition) {
top:
System.out.print('\u000C');
System.out.println(" A , B , C ");
System.out.println("| " +slot[0]+ " | " +slot [1]+ " | " +slot[2]+ " |");
System.out.println("| " +slot[3]+ " | " +slot [4]+ " | " +slot[5]+ " |");
System.out.println("| " +slot[6]+ " | " +slot [7]+ " | " +slot[8]+ " |");
middle:
System.out.println("What number do you want to move <1, 2, 3>: ");
disk = (input.readLine());
System.out.println("What tower do you want to put <A, B, C>: ");
tower = (input.readLine());
// First Move
if(disk.equals("1") && tower.equals("b")) {
slot[0] = slot[8];
// use continue skip the rest code
// continue top;
// if you want goto top, continue and continue top is same
continue;
// if you want goto middle, you can use the label
// continue middle;
}
// some other code
}
你可以在循环中正确使用label,但不能goto,比如上面的代码
import java.io.*;
import java.util.Scanner;
public class Helloworld{
public static BufferedReader input=new BufferedReader(new InputStreamReader(System.in));
public static void main(String[]args) throws IOException{
System.out.print('\u000C');
//variables
String lev,tower,disk;
int [] slot;
slot= new int [9];
slot[0]=1;
slot[3]=2;
slot[6]=3;
System.out.println("Press enter to continue...");
Scanner keyboard = new Scanner(System.in);
keyboard.nextLine();
System.out.print('\u000C');
System.out.println("Select Levels:");
System.out.println("[1]Level 1 <3 numbers>");
System.out.println("[2]Level 2 <4 numbers>");
System.out.println("[3]Level 3 <5 numbers>");
System.out.println("[4]Instructions");
System.out.print("Choose one: ");
lev =(input.readLine());
if (lev.equals("4")){
System.out.println("* You must transfer all numbers from tower 1 - tower 3 in ascending order.");
System.out.println("* For every move you can only move one number to a certain.");
System.out.println("* The number on the top of the tower are the one that you can move only.");
}
else if (lev.equals("1")){
//loop
while (true) {
System.out.print('\u000C');
//display
System.out.println(" A , B , C ");
System.out.println("| " +slot[0]+ " | " +slot [1]+ " | " +slot[2]+ " |");
System.out.println("| " +slot[3]+ " | " +slot [4]+ " | " +slot[5]+ " |");
System.out.println("| " +slot[6]+ " | " +slot [7]+ " | " +slot[8]+ " |");
// input a disk for user
System.out.println("What number do you want to move <1, 2, 3, e to exit>: ");
disk = (input.readLine());
if (disk.equals("e")) {
break;
}
// input tower where the disk will contain.
System.out.println("What tower do you want to put <A, B, C>: ");
tower = (input.readLine());
// Possible moves for user
if (disk.equals("1") && tower.equals("b")){
slot[7]=slot[0];
slot[0]=0;
System.out.println("Hello");
}
else if (disk.equals("2") && tower.equals("c")){
slot[8]=slot[3]; slot[3]=0;
}
if (disk.equals("3") && tower.equals("b")){
slot[5]=slot[7]; slot[7]=0;
}
else if (disk.equals("1") && tower.equals("b")){
slot[5]=slot[4]; slot[4]=0;
}
else if (disk.equals("1") && tower.equals("a")){
slot[6]=slot[5]; slot[5]=0;
}
else if (disk.equals("2") && tower.equals("b")){
slot[4]=slot[8]; slot[8]=0;
}
else if (disk.equals("2") && tower.equals("a")){
slot[2]=slot[6]; slot[6]=0;
}
else if (disk.equals("1") && tower.equals("a")){
slot[3]=slot[4]; slot[4]=0;
}//
else if (disk.equals("1") && tower.equals("b")){
slot[2]=slot[6]; slot[6]=0;
}
else if (disk.equals("3") && tower.equals("c")){
slot[8]=slot[7]; slot[7]=0;
}
else if (disk.equals("1") && tower.equals("b")){
slot[7]=slot[3]; slot[3]=0;
}
else if (disk.equals("2") && tower.equals("c")){
slot[5]=slot[6]; slot[6]=0;
}
else if (disk.equals("1") && tower.equals("c")){
slot[2]=slot[7]; slot[7]=0;
}
}
}
}
}
我不知道为什么 error.I 不能将 1 放在 2 的顶部。当我将 1 放在 2 的顶部时,2 变成 0。那是汉诺塔。我想创建一个名为河内塔的游戏。我想让用户选择 he/she 想要移动的内容。那是第一个输入。第二个输入是我想用户 he/she 想要放什么塔。
切勿使用 GOTO!
如果我没理解错的话,你想在每一步之后提示用户输入新的内容。所以你想把它放在一个循环中
类似于
while(continueProgramm){
System.out.println("What number do you want to move <1, 2, 3>: ");
disk =(input.readLine());
System.out.println("What tower do you want to put <A, B, C>: ");
tower =(input.readLine());
//handle your input here ...
}
在 continue 语句中使用 while 循环。
if(disk.equals("1") && tower.equals("b")) {
slot[0] = slot[8];
continue;
}
如 fge 所述,goto
是 Java 中的保留字,但未实现。在大多数语言中,使用 goto
通常被认为是不好的编程习惯。你应该使用一个循环。具有突破条件的无限 while
循环应该足够了:
while (true) {
System.out.print('\u000C');
System.out.println(" A , B , C ");
System.out.println("| " +slot[0]+ " | " +slot [1]+ " | " +slot[2]+ " |");
System.out.println("| " +slot[3]+ " | " +slot [4]+ " | " +slot[5]+ " |");
System.out.println("| " +slot[6]+ " | " +slot [7]+ " | " +slot[8]+ " |");
System.out.println("What number do you want to move <1, 2, 3, e to exit>: ");
disk = (input.readLine());
if (disk.equals("e")) {
break;
}
System.out.println("What tower do you want to put <A, B, C>: ");
tower = (input.readLine());
// First Move
if (disk.equals("1") && tower.equals("b")) {
slot[0] = slot[8];
}
}
while (condition) {
top:
System.out.print('\u000C');
System.out.println(" A , B , C ");
System.out.println("| " +slot[0]+ " | " +slot [1]+ " | " +slot[2]+ " |");
System.out.println("| " +slot[3]+ " | " +slot [4]+ " | " +slot[5]+ " |");
System.out.println("| " +slot[6]+ " | " +slot [7]+ " | " +slot[8]+ " |");
middle:
System.out.println("What number do you want to move <1, 2, 3>: ");
disk = (input.readLine());
System.out.println("What tower do you want to put <A, B, C>: ");
tower = (input.readLine());
// First Move
if(disk.equals("1") && tower.equals("b")) {
slot[0] = slot[8];
// use continue skip the rest code
// continue top;
// if you want goto top, continue and continue top is same
continue;
// if you want goto middle, you can use the label
// continue middle;
}
// some other code
}
你可以在循环中正确使用label,但不能goto,比如上面的代码