第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何隨機(jī)生成猜測(cè)

如何隨機(jī)生成猜測(cè)

GCT1015 2023-08-16 15:58:17
我對(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;

}


查看完整回答
反對(duì) 回復(fù) 2023-08-16
  • 1 回答
  • 0 關(guān)注
  • 139 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)