我對(duì) Java 還是有點(diǎn)陌生,并且有一個(gè)實(shí)驗(yàn)室需要模擬生成 1-10 之間數(shù)字的彩票游戲。它首先詢問用戶想要購(gòu)買多少?gòu)埐势?,然后詢問他們是否希望?jì)算機(jī)為他們生成猜測(cè),如果是,那么它將生成猜測(cè)并顯示中獎(jiǎng)號(hào)碼。如果用戶拒絕,則用戶將自己輸入猜測(cè)并顯示中獎(jiǎng)號(hào)碼。我在弄清楚當(dāng)有人輸入“是”或“否”時(shí)如何執(zhí)行代碼時(shí)遇到問題。我應(yīng)該做一個(gè) do while 循環(huán)嗎?這是我現(xiàn)在的代碼。public static void main(String[] args) { Scanner input = new Scanner(System.in); double TICKET_PRICE = 2.00; System.out.println("Welcome to the State of Florida Play10 Lottery Game. Ticket Price: $" + TICKET_PRICE); System.out.println("How many tickets would you like to purchase?"); int ticketsPurchased = input.nextInt(); System.out.print("Please enter " + (ticketsPurchased) + " to confirm your credit carde charge: "); int creditCardCharge = input.nextInt(); if (ticketsPurchased != creditCardCharge) { System.out.println("Wrong number, please enter again: "); return; } if (ticketsPurchased == creditCardCharge) { System.out.println("Thank you. Your credit card will be charged $" + (ticketsPurchased * 2)); } int min = 1; int max = 10; int winner; winner = min + (int)(Math.random() * ((max - min) + 1)); System.out.print("Would you like the computer to generate your guesses? Enter 'Y' or 'N': "); String computerGeneratedGuess = input.nextLine(); int guess = 0; int winCtr = 0; String output = "";}算法如下: 1. 獲取要購(gòu)買的門票數(shù)量,計(jì)算并確認(rèn)信用卡費(fèi)用。2. 生成隨機(jī)獲勝整數(shù)并生成隨機(jī)猜測(cè)或提示用戶猜測(cè)。3. 報(bào)告中獎(jiǎng)號(hào)碼、中獎(jiǎng)彩票、總獎(jiǎng)金、總損失以及允許扣除的金額
1 回答

慕后森
TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超5個(gè)贊
一般來(lái)說(shuō),布爾值可以方便地控制這樣的循環(huán)。就像是:
boolean gameOver = false;
int theGuess = 0;
while (!gameOver) {
if (computerGeneratedGuess == 'Y') {
theGuess = //code to generate a random number
}
else {
theGuess = //code to for user to enter a guess
}
if (theGuess == winner) {
gameOver = true;
}
添加回答
舉報(bào)
0/150
提交
取消