亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何修復此單人骰子游戲(4 - 4 面骰子)的循環?掃描儀輸入無法產生正確輸出的問題

如何修復此單人骰子游戲(4 - 4 面骰子)的循環?掃描儀輸入無法產生正確輸出的問題

慕森卡 2024-01-05 09:58:16
// 我一整天都在研究這個問題,但似乎仍然被卡住了。// 我沒有收到任何明顯的錯誤,但循環似乎被破壞了。// 我是一個初學者,所以很可能我錯過了一些大事,但只是忽略了它。// 我的課的作業要在午夜截止,哈哈。// 我覺得我構建了基本格式,但是我對使用循環的不熟悉確實讓我陷入了困境。我在網上其他地方查看過,但人們制作的許多“骰子”程序僅與一個六面骰子有關,并且不涉及基于回合的用戶輸入。// 任何有用的提示都很棒,有沒有更有效的方法來構建這個游戲?我知道創建多個類會清理程序的外觀,但目前我實際上只是在尋找功能。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);
查看完整描述

1 回答

?
絕地無雙

TA貢獻1946條經驗 獲得超4個贊

我已經編輯了你的代碼。有些錯誤與語法有關,有些可能與邏輯流程有關。這應該作為基礎,您可以根據需要修改和改進它:


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("-----------------------------------------");


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


            System.out.println();


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

            answer = stdIn.next();


            if (answer.equalsIgnoreCase("y")) {


                turnCounter++;


                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);


                if ((d1 == d2) || (d1 == d3) || (d1 == d4) || (d2 == d3) || (d2 == d4) || (d3 == d4) {

                    userWin++;

                    System.out.println("\n" + "Round Results: Win");

                    System.out.println(turnCounter + " Rounds played.");

                } else {

                    userLose++;

                    System.out.println("\n" + "Round Results: Loss");

                    System.out.println(turnCounter + " Rounds played.");

                }


                System.out.println("Game Results:");

                System.out.println("User won: " + userWin + " Games.");

                System.out.println("User lost: " + userLose + " Games.");


                System.out.println("Your win/loss ratio is: " + userWin + ":" + userLose);


                if (userWin > userLose) {System.out.println("Good Job!");}


                if (userWin < userLose) {System.out.println("You shouldn't bet money on this game...");}


                System.out.println(turnCounter + " Rounds played.");


            }


        } while(answer.equalsIgnoreCase("y"));

    }

}

需要注意的幾點:

  • 只要用戶輸入“y”,游戲就會繼續運行,因為這是您的條件:answer.equalsIgnoreCase(“y”)。

  • 我更改了獲勝條件邏輯以使用邏輯 OR 運算符檢查至少一對

  • 我刪除了贏/輸比率結果的除法運算符,只是用贏:輸的顯示來替換它;如果您希望它計算實際百分比或小數值,則可以更改此值,但您必須檢查 Loss == 0 的情況以防止除以零錯誤

  • Do-While 循環應該涵蓋從開始到結束的所有游戲玩法,因此要求您再次玩的問題應該出現在該循環的開始或結束處(我將其放在開始處)


查看完整回答
反對 回復 2024-01-05
  • 1 回答
  • 0 關注
  • 146 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號