如何解决用字母 Q 终止程序
我想随时通过键入 Q 来终止程序。我不能把它放在“diceChosen”中,因为它是一个整数。我尝试了不同的方法,但对我没有任何效果。
Scanner userInput = new Scanner(system.in);
int wins = 0;
int losses = 0;
int diceOne = 0;
int diceTwo = 0;
int diceThree = 0;
int sum = 0;
System.out.println("Välkommen till spelet 12. Du ska slå 1-3 tärningar och försöka få summan 12...");
for (int counter = 0; counter < 3; counter++) {
System.out.printf("%nAnge vilken tärning du vill slå[1,2,3](avsluta med q): ");
int diceChosen = userInput.nextInt();
if (diceChosen == 1)
diceOne = (int)(Math.random() * 6) + 1;
if (diceChosen == 2)
diceTwo = (int)(Math.random() * 6) + 1;
if (diceChosen == 3)
diceThree = (int)(Math.random() * 6) + 1;
sum = diceOne + diceTwo + diceThree;
if (sum == 12)
++wins;
if (sum > 12)
++losses;
System.out.printf("%d %d %d sum: %d #vinst: %d #förlust: %d",diceOne,diceTwo,diceThree,sum,wins,losses);
}
解决方法
由于您不能使用 try 和 catch 其他替代方法,可以通过将 userInput.nextInt()
更改为 userInput.nextLine();
以便它可以检测何时按下 Q 如果未按下 Q 那么我们将 String 转换为 int 以便它可以用 Integer.parseInt(diceChosen);
所有代码
while (true) {
for (int counter = 0; counter < 3; counter++) {
System.out.printf("%nAnge vilken tärning du vill slå[1,2,3](avsluta med q): ");
String diceChosen = diceChosen = userInput.nextLine();
int diceChosenToInteger = 0;
if (diceChosen.equalsIgnoreCase("Q")) {
System.exit(0);
} else {
diceChosenToInteger = Integer.parseInt(diceChosen);
}
if (diceChosenToInteger == 1)
diceOne = (int) (Math.random() * 6) + 1;
if (diceChosenToInteger == 2)
diceTwo = (int) (Math.random() * 6) + 1;
if (diceChosenToInteger == 3)
diceThree = (int) (Math.random() * 6) + 1;
sum = diceOne + diceTwo + diceThree;
if (sum == 12)
++wins;
if (sum > 12)
++losses;
System.out.printf("%d %d %d sum: %d #vinst: %d #förlust: %d",diceOne,diceTwo,diceThree,sum,wins,losses);
}
}
}
}
,
您可以有一个额外的问题询问用户是否愿意再次玩游戏:
import java.util.Random;
import java.util.Scanner;
public class Main {
private static int getIntegerInput(Scanner scanner,String prompt,int minValue,int maxValue) {
System.out.print(prompt);
int validInteger = -1;
while (scanner.hasNext()) {
if (scanner.hasNextInt()) {
validInteger = scanner.nextInt();
if (validInteger >= minValue && validInteger <= maxValue) {
break;
} else {
System.out.printf("Error: Please enter an integer between %d and %d inclusive%n",minValue,maxValue);
System.out.print(prompt);
}
} else {
System.out.printf("Error: Please enter an integer between %d and %d inclusive%n",maxValue);
System.out.print(prompt);
scanner.next();
}
}
return validInteger;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int gamesPlayed = 0;
int wins = 0;
System.out.println("Welcome! You must roll 1-3 dice and try to get the sum 12...");
while (true) {
System.out.printf("Game %d:%n",++gamesPlayed);
int numDiceRolls = getIntegerInput(scanner,"How many dice do you want to roll? ",1,3);
int total = 0;
for (int i = 1; i <= numDiceRolls; i++) {
int roll = new Random().nextInt(6) + 1;
System.out.printf("Roll %d: %d%n",i,roll);
total += roll;
}
System.out.printf("The total for this game was %d%n",total);
if (total == 12) {
System.out.println("You won this game!");
wins++;
} else {
System.out.println("You lost this game!");
}
System.out.print("Enter q to quit or anything else to play again: ");
String playAgain = scanner.next();
if (playAgain.toLowerCase().equals("q")) {
break;
}
}
System.out.printf("You won %d time(s) and lost %d time(s)",gamesPlayed - wins);
}
}
示例用法:
Welcome! You must roll 1-3 dice and try to get the sum 12...
Game 1:
How many dice do you want to roll? 5
Error: Please enter an integer between 1 and 3 inclusive
How many dice do you want to roll? 3
Roll 1: 1
Roll 2: 2
Roll 3: 6
The total for this game was 9
You lost this game!
Enter q to quit or anything else to play again: a
Game 2:
How many dice do you want to roll? 3
Roll 1: 6
Roll 2: 3
Roll 3: 3
The total for this game was 12
You won this game!
Enter q to quit or anything else to play again: q
You won 1 time(s) and lost 1 time(s)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。