用字母 Q 终止程序

Terminate program with letter 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 and 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", minValue, 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)", wins, 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)