我正在尝试使“按q退出”功能正常工作,但是遇到了一些麻烦。 “无效字符”功能也无法正常工作。我将如何解决这个问题?我的教授建议在代码的开头放置“按q退出”功能,但没有给我任何进一步的说明。
public static void main(String[] args) {
char userChar = 0;
Scanner sc = new Scanner(System.in);
Random rnd = new Random();
// Intro/directions/prompting for user input
System.out.println("Welcome to Rock, Paper, Scissors by Rancid!");
System.out.println("Choose R for Rock, P for Paper, S for Scissors, or Q to Quit, then press Enter: ");
// If player chooses to quit
if (userChar == 'q' || userChar == 'Q') {
System.out.println("Player chose to quit. Goodbye!");
}
// Start of loop
while (userChar != 'q' && userChar != 'Q') {
// Prompting computer to generate a random number
int randomNumber = rnd.nextInt(3) + 1;
// If computer generates 1 (Rock)
if (randomNumber == 1) {
if (userChar == 'r' || userChar == 'R') {
System.out.println("Rock vs. Rock! It's a tie!");
} else if (userChar == 'p' || userChar == 'P') {
System.out.println("Paper covers Rock, you win!");
} else if (userChar == 's' || userChar == 'S') {
System.out.println("Rock breaks Scissors, you lose!");
}
}
// If computer generates 2 (Paper)
else if (randomNumber == 2) {
if (userChar == 'r' || userChar == 'R') {
System.out.println("Paper covers Rock, you lose!");
} else if (userChar == 'p' || userChar == 'P') {
System.out.println("Paper vs. Paper! It's a tie!");
} else if (userChar == 's' || userChar == 'S') {
System.out.println("Scissors cuts Paper, you win!");
}
}
// If computer generates 3 (Scissors)
else if (randomNumber == 3) {
if (userChar == 'r' || userChar == 'R') {
System.out.println("Rock breaks Scissors, you win!");
} else if (userChar == 'p' || userChar == 'P') {
System.out.println("Scissors cuts Paper, you lose!");
} else if (userChar == 's' || userChar == 'S') {
System.out.println("Scissors vs. Scissors! It's a tie!");
}
}
// If player types an invalid character
else {
System.out.println("Invalid input! Please enter a valid character.");
}
userChar = sc.next().charAt(0);
}
}
}
答案 0 :(得分:0)
好吧,首先,您要退出的条件超出了while循环的范围,这意味着它永远不会在给定的上下文中执行。
第二,除非userChar不同时是q和Q,否则while循环中的条件将不成立。
我建议您稍微重构一下while循环,例如:
while (true) {
//Your logic here
if (userChar == 'q' || userChar == 'Q') {
System.out.println("Player chose to quit. Goodbye!");
break;
}
//Your logic here
}
答案 1 :(得分:0)
为了真正处理无效输入,您需要在循环中首先进行此操作。此外,您无法在else
语句的if-else
块中执行此操作,因为在那些if-else
中,您检查的是randomNumber
,而不是输入,因此它不会感觉,它将永远无法工作。另外,您检查输入内容是从sc.next().charAt(0)
中获取的内容,但是例如,如果用户插入了"R_invalid_something_without_space"
之类的字符串,您只会查看第一个字母R
并将其当作有效输入。我修改了您的代码,以便您在每次循环迭代中读取一行,如果该行不是q / r / p / s
,它将被视为无效输入,如果它的q
,则该程序将结束。
public static void main(String[] args) {
char userChar = 0;
Scanner sc = new Scanner(System.in);
Random rnd = new Random();
// Intro/directions/prompting for user input
System.out.println("Welcome to Rock, Paper, Scissors by Rancid!");
System.out.println("Choose R for Rock, P for Paper, S for Scissors, or Q to Quit, then press Enter: ");
// Start of loop
while (true) {
// what I changed
// **********************************************************
String line = sc.nextLine().toLowerCase();
if(line.equals("q")){
System.out.println("Player chose to quit. Goodbye!");
break;
}
if(!line.equals("r") && !line.equals("s") && !line.equals("p")) {
System.out.println("Invalid input! Please enter a valid character.");
continue;
}
userChar = line.charAt(0);
// **********************************************************
// Prompting computer to generate a random number
int randomNumber = rnd.nextInt(3) + 1;
// If computer generates 1 (Rock)
if (randomNumber == 1) {
if (userChar == 'r' || userChar == 'R') {
System.out.println("Rock vs. Rock! It's a tie!");
} else if (userChar == 'p' || userChar == 'P') {
System.out.println("Paper covers Rock, you win!");
} else if (userChar == 's' || userChar == 'S') {
System.out.println("Rock breaks Scissors, you lose!");
}
}
// If computer generates 2 (Paper)
else if (randomNumber == 2) {
if (userChar == 'r' || userChar == 'R') {
System.out.println("Paper covers Rock, you lose!");
} else if (userChar == 'p' || userChar == 'P') {
System.out.println("Paper vs. Paper! It's a tie!");
} else if (userChar == 's' || userChar == 'S') {
System.out.println("Scissors cuts Paper, you win!");
}
}
// If computer generates 3 (Scissors)
else if (randomNumber == 3) {
if (userChar == 'r' || userChar == 'R') {
System.out.println("Rock breaks Scissors, you win!");
} else if (userChar == 'p' || userChar == 'P') {
System.out.println("Scissors cuts Paper, you lose!");
} else if (userChar == 's' || userChar == 'S') {
System.out.println("Scissors vs. Scissors! It's a tie!");
}
}
}
}