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

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

我不明白為什么我的代碼的“再玩一次”部分不起作用

我不明白為什么我的代碼的“再玩一次”部分不起作用

開心每一天1111 2023-09-13 10:55:32
我已經(jīng)使用 java 幾個(gè)星期了,想嘗試制作我的第一個(gè)小游戲。我所擁有的一切都工作正常,直到我嘗試添加再次播放功能??雌饋硭皇翘^了底部的一小部分代碼,我不明白為什么。我嘗試過移動(dòng)代碼的不同部分,但無法弄清楚為什么它不起作用。public static void main(String[] args) {    Scanner scan = new Scanner(System.in);    System.out.println("You have 10 tries to correctly guess a random number between 1 and 10. Good luck! \n"            + "After you have entered a number, press ENTER to see if you won.");    boolean play;       play = true;   do    {            int tries = 10, triesLeft = 0, triesUsed = 0;       String YorN;     do    {        System.out.print("\nEnter A Number: ");        int numInp = scan.nextInt();            int randomNumber = (int)(Math.random()*9) + 1;        System.out.println("The number was " + randomNumber);        if(numInp == randomNumber) {            System.out.println("\nYou Win! Great job.");            triesLeft = --tries;            triesUsed = 10 - triesLeft;            tries = -1;        } else {            System.out.println("Try Again...");            tries --;        }    }    while( tries > 0);    String playAgain = (" Feel free to play again soon!");    if(tries == 0) {        System.out.println("\nOut of tries :(   You lose loser  " + playAgain);    }    if(tries == -1) {    System.out.println("\nYou used " + triesUsed + " tries and had "            + triesLeft + " tries left! :)" + playAgain);    }    System.out.print("\nDo you want to play again? Y or N?");    YorN = scan.nextLine();    if (YorN == ("Y") || YorN == ("y"))    {        play = true;    }    else if (YorN == ("N") || YorN == ("n"))    {        play = false;    }    else {}   } while(play = true);   if (play = false){       System.out.println("\nGood Bye");    }}當(dāng)我運(yùn)行代碼時(shí),這個(gè)系統(tǒng)似乎沒有做任何事情:YorN = scan.nextLine();    if (YorN == ("Y") || YorN == ("y"))    {        play = true;    }    else if (YorN == ("N") || YorN == ("n"))    {        play = false;    }    else {}這是我運(yùn)行時(shí)看到的內(nèi)容:你贏了!做得好。您已經(jīng)嘗試了 5 次,還剩 5 次嘗試!:) 很快就可以再次玩了!你想再玩一次嗎?是還是否?輸入號(hào)碼:它沒有給我一個(gè)應(yīng)該輸入 y 或 n 的位置。如果有人知道為什么我會(huì)非常感謝你的幫助
查看完整描述

1 回答

?
白板的微信

TA貢獻(xiàn)1883條經(jīng)驗(yàn) 獲得超3個(gè)贊

nextInt() 或 next() 方法不讀取 Enter。通常在數(shù)字之后您必須按 Enter 鍵,而上述方法尚未準(zhǔn)備好。避免這種情況的排序方式是保留 1 個(gè)額外的 sc.nextLine() 方法調(diào)用。它將讀取并丟棄輸入。


scan.nextLine();

YorN = scan.nextLine();


    if (YorN == ("Y") || YorN == ("y"))

    {

        play = true;

    }

    else if (YorN == ("N") || YorN == ("n"))

    {

        play = false;

    }

    else {}


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

添加回答

舉報(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)