gpt4 book ai didi

Java程序无法读取字符

转载 作者:行者123 更新时间:2023-12-02 10:07:18 25 4
gpt4 key购买 nike

这是我当前的代码,我省略了程序的前半部分。我正在尝试使用 switch case 语句来确定是否重复此片段所在的 while 循环。此外,我的 IDE (Eclipse) 不会将“Asker”识别为变量,并且不会让我运行或调试

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

最佳答案

Adeel 提供了正确的解决方案:您需要在 case 语句中使用 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程序无法读取字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55253049/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com