1 回答

TA貢獻(xiàn)1946條經(jīng)驗(yàn) 獲得超4個(gè)贊
我已經(jīng)編輯了你的代碼。有些錯(cuò)誤與語法有關(guān),有些可能與邏輯流程有關(guān)。這應(yīng)該作為基礎(chǔ),您可以根據(jù)需要修改和改進(jìn)它:
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"));
}
}
需要注意的幾點(diǎn):
只要用戶輸入“y”,游戲就會(huì)繼續(xù)運(yùn)行,因?yàn)檫@是您的條件:answer.equalsIgnoreCase(“y”)。
我更改了獲勝條件邏輯以使用邏輯 OR 運(yùn)算符檢查至少一對(duì)
我刪除了贏/輸比率結(jié)果的除法運(yùn)算符,只是用贏:輸?shù)娘@示來替換它;如果您希望它計(jì)算實(shí)際百分比或小數(shù)值,則可以更改此值,但您必須檢查 Loss == 0 的情況以防止除以零錯(cuò)誤
Do-While 循環(huán)應(yīng)該涵蓋從開始到結(jié)束的所有游戲玩法,因此要求您再次玩的問題應(yīng)該出現(xiàn)在該循環(huán)的開始或結(jié)束處(我將其放在開始處)
添加回答
舉報(bào)