猿问

如何修复此单人骰子游戏(4 - 4 面骰子)的循环?扫描仪输入无法产生正确输出的问题

// 我一整天都在研究这个问题,但似乎仍然被卡住了。// 我没有收到任何明显的错误,但循环似乎被破坏了。// 我是一个初学者,所以很可能我错过了一些大事,但只是忽略了它。// 我的课的作业要在午夜截止,哈哈。// 我觉得我构建了基本格式,但是我对使用循环的不熟悉确实让我陷入了困境。我在网上其他地方查看过,但人们制作的许多“骰子”程序仅与一个六面骰子有关,并且不涉及基于回合的用户输入。// 任何有用的提示都很棒,有没有更有效的方法来构建这个游戏?我知道创建多个类会清理程序的外观,但目前我实际上只是在寻找功能。

package prgm06;


import java.util.Scanner;


public class DiceGame 

{   

    public static void main(String []args) //main DiceGame loop.

    {

        String answer;

        Scanner stdIn = new Scanner(System.in);

        int userWin = 0, userLose = 0, turnCounter = 0;

        System.out.println("\t" + "Welcome to Computer Dice");

        System.out.println("-----------------------------------------");

        System.out.println("The outcome of your roll will determine" + "\n" + "if you win or lose the round." + "\n");

        System.out.println("Any Quad and you win.");

        System.out.println("Any Triple and you win.");

        System.out.println("Any High Pair and you win.");

        System.out.println("Anything else and you lose.");

        System.out.println("-----------------------------------------");

        System.out.println("Do you wish to play? [y,n]: ");

        do { // I always want the dice to roll unless "n" is selected.

            answer = stdIn.next();

            int d1 = (int)(Math.random() * 4) + 1;

            int d2 = (int)(Math.random() * 4) + 1;

            int d3 = (int)(Math.random() * 4) + 1;

            int d4 = (int)(Math.random() * 4) + 1;

        }

        while(answer.equalsIgnoreCase("y")); // issues with "y" not printing if/ else statements

        {

            int d1 = (int)(Math.random() * 4) + 1;

            int d2 = (int)(Math.random() * 4) + 1;

            int d3 = (int)(Math.random() * 4) + 1;

            int d4 = (int)(Math.random() * 4) + 1;

            System.out.println(d1 + "\t" + d2 + "\t" + d3 + "\t" + d4);


慕森卡
浏览 83回答 1
1回答

绝地无双

我已经编辑了你的代码。有些错误与语法有关,有些可能与逻辑流程有关。这应该作为基础,您可以根据需要修改和改进它:import java.util.Scanner;public class DiceGame {&nbsp; &nbsp; public static void main(String []args) //main DiceGame loop.&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; String answer;&nbsp; &nbsp; &nbsp; &nbsp; Scanner stdIn = new Scanner(System.in);&nbsp; &nbsp; &nbsp; &nbsp; int userWin = 0, userLose = 0, turnCounter = 0;&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("\t" + "Welcome to Computer Dice");&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("-----------------------------------------");&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("The outcome of your roll will determine" + "\n" + "if you win or lose the round." + "\n");&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Any Quad and you win.");&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Any Triple and you win.");&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Any High Pair and you win.");&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Anything else and you lose.");&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("-----------------------------------------");&nbsp; &nbsp; &nbsp; &nbsp; do { // I always want the dice to roll unless "n" is selected.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Do you wish to play? [y,n]: ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; answer = stdIn.next();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (answer.equalsIgnoreCase("y")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; turnCounter++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int d1 = (int)(Math.random() * 4) + 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int d2 = (int)(Math.random() * 4) + 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int d3 = (int)(Math.random() * 4) + 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int d4 = (int)(Math.random() * 4) + 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(d1 + "\t" + d2 + "\t" + d3 + "\t" + d4);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ((d1 == d2) || (d1 == d3) || (d1 == d4) || (d2 == d3) || (d2 == d4) || (d3 == d4) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; userWin++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("\n" + "Round Results: Win");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(turnCounter + " Rounds played.");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; userLose++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("\n" + "Round Results: Loss");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(turnCounter + " Rounds played.");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Game Results:");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("User won: " + userWin + " Games.");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("User lost: " + userLose + " Games.");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Your win/loss ratio is: " + userWin + ":" + userLose);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (userWin > userLose) {System.out.println("Good Job!");}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (userWin < userLose) {System.out.println("You shouldn't bet money on this game...");}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(turnCounter + " Rounds played.");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; } while(answer.equalsIgnoreCase("y"));&nbsp; &nbsp; }}需要注意的几点:只要用户输入“y”,游戏就会继续运行,因为这是您的条件:answer.equalsIgnoreCase(“y”)。我更改了获胜条件逻辑以使用逻辑 OR 运算符检查至少一对我删除了赢/输比率结果的除法运算符,只是用赢:输的显示来替换它;如果您希望它计算实际百分比或小数值,则可以更改此值,但您必须检查 Loss == 0 的情况以防止除以零错误Do-While 循环应该涵盖从开始到结束的所有游戏玩法,因此要求您再次玩的问题应该出现在该循环的开始或结束处(我将其放在开始处)
随时随地看视频慕课网APP

相关分类

Java
我要回答