Java程序不会读取字符
这是我当前的代码,我省略了程序的前半部分。我试图使用switch-case语句来确定是否重复while循环,这段代码就在其中。此外,我的IDEEclipse不会将“Asker”识别为变量,也不会让我运行或调试Java程序不会读取字符,java,java.util.scanner,Java,Java.util.scanner,这是我当前的代码,我省略了程序的前半部分。我试图使用switch-case语句来确定是否重复while循环,这段代码就在其中。此外,我的IDEEclipse不会将“Asker”识别为变量,也不会让我运行或调试 import java.util.Scanner; public class Main { public static void main(String args[]) { int Customers = 1; boolean Repeat =
import java.util.Scanner;
public class Main {
public static void main(String args[]) {
int Customers = 1;
boolean Repeat = true;
Scanner keyboard = new Scanner(System.in);
String CustomerInfo[][] = new String[1][6];
// i = number j = name k = hotel m = location n = date p = discount type
String CustomerData[][] = new String[1][4];
// i = stay j = pre cost k = stay length m = final cost
System.out.println("");
System.out.println("");
System.out.println("");
System.out.println("");
System.out.println("Welcome to the Northwoods Hotel Reservation System");
System.out.println("");
while (Repeat) {
for (int i = 0; i < Customers; i++) {
System.out.println("Please Enter Your Name");
CustomerInfo[i][0] = keyboard.nextLine();
System.out.println("Please Enter Your Hotel");
CustomerInfo[i][1] = keyboard.nextLine();
System.out.println("Please Enter Your Reservation Date");
CustomerInfo[i][2] = keyboard.nextLine();
System.out.println("Please Enter The Location");
CustomerInfo[i][3] = keyboard.nextLine();
System.out.println("Please Enter Your Room Accomodations");
CustomerInfo[i][4] = keyboard.nextLine();
System.out.println("Please Enter Your Discount Type");
CustomerInfo[i][5] = keyboard.nextLine();
} //End I for Loop
System.out.println("Would you like to add another Customer?? (Y or N)");
char asker = 'y';
asker = keyboard.next().charAt(0);
switch (asker) {
case 'y':
case 'Y':
Repeat = true;
case 'n':
case 'N':
Repeat = false;
default:
Repeat = false;
} //End switch
} //End While
//System.out.println(Asker);
System.out.println(Repeat);
System.out.println(Asker);
System.out.println("");
System.out.println("");
System.out.println("Customer Number\t\t" + "Customer Name \t\t" + "Hotel\t\t" + "Reservation Date \t" + "Accomodations \t" + "Length of Reservation \t" + "Cost" + "Tax \t" + "Discount \t\t" + "Total Cost:");
for (int i = 0; i < Customers; i++) {
System.out.println(CustomerInfo[i][0] + " \t" + CustomerInfo[i][1] + " \t" + CustomerInfo[i][2] + " \t" + CustomerInfo[i][3] + " \t" + CustomerInfo[i][4] + " \t" + CustomerInfo[i][5]);
} //End For Loop
} //End Main Method
} //End Class
阿德尔提供了正确的解决方案:你需要使用break;在你的陈述中
switch (asker) {
case 'y':
case 'Y':
Repeat = true;
break; // without a break here, it will fall through
case 'n':
case 'N':
Repeat = false;
break; // without a break here, it will fall through
// but this one isn't as bad since it is the same outcome
default:
Repeat = false;
} //End switch
第一件与您的问题无关的事情是,您没有遵循正确的Java命名约定。变量应该以小写字母开头,所以Asker应该是Asker,等等。其次,您实际上没有指定您的问题是什么。。。您还应该包括代码的前半部分,并提供一个演示该问题的示例。您提到了一个循环,但您发布的代码甚至没有包含一个循环。你期望得到什么结果,而看到的是什么?你需要在switch语句中使用break。Asker是在你的循环之外定义的。main方法也是一个方法,所以你至少在这方面很好。这不是问题,这实际上是int'Asker'@KyleMccoy初始化的位置,而不是根据我看到的。它在switch语句上方三行。