【问题标题】:Terminate program with letter Q用字母 Q 终止程序
【发布时间】:2021-06-05 19:53:44
【问题描述】:

我想随时通过键入 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 语句输入不匹配异常,然后创建一个if 语句来查看用户是否按下了q,然后在if 中输入System.exit(0);

标签: java if-statement terminate dice


【解决方案1】:

由于您不能使用 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);


            }
        }

    }
   
}

【讨论】:

  • 很酷,谢谢,但这是一个作业,我们还没有学习 try/catch,如果我使用它可能看起来很可疑。这是一个入门课程,所以我是一个完整的初学者。有没有更基本的方法?我在想如果 (diceChosen == q) 要么放另一个,但我必须使用 (diceChosen.equals()) 并且我不让它工作。此外,游戏不会按预期重复。我曾想过将整个 for 循环放在一个 while 语句中,但不知道它是如何工作的。如前所述,完全初学者。谢谢!
  • 看起来并不可疑,看起来您尝试自己完成作业并在网上寻求帮助,这完全没问题。
  • 您可以立即查看。
【解决方案2】:

您可能有一个额外的问题询问用户是否想再次玩游戏:

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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-26
    • 2015-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-14
    • 1970-01-01
    • 2020-09-03
    相关资源
    最近更新 更多