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

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

Java 為什么我輸入的每個輸入都會忽略我的 While 循環(huán)?

Java 為什么我輸入的每個輸入都會忽略我的 While 循環(huán)?

紅顏莎娜 2023-11-10 17:15:06
我試圖找出為什么我的 main 方法中的 while 循環(huán)被忽略,即使我沒有輸入“退出”。該程序應該接受用戶的輸入(轉換為 int),讓計算機生成一個隨機 int(石頭、布或剪刀),比較兩個 int,打印出答案,然后重復一遍,直到“退出” " 或輸入任何其他無法識別的輸入(不是石頭、剪刀、布)。例如:UserInput = rock; 輸出:再見?。。。m然循環(huán)被跳過,但直接進入 if 循環(huán),即使“userInput”(轉換為 int 1)不等于“quit”(轉換為 int 0)。嘗試修復它,以便 if UserInput = "rock"(轉換為1),計算機隨機生成“紙”(轉換為2),輸出將是“紙”獲勝。我?guī)讉€小時以來一直在努力解決這個問題,因此非常感謝您的幫助,謝謝。package rockPaperScissors;import java.util.*public class RockPaperScissors {    public static final int quit = 0;    public static final int rock = 1;    public static final int paper = 2;    public static final int scissors = 3;    static Scanner console = new Scanner(System.in);public static void main(String [] args) {    //gets userHand from user as int    int userHand = promptUserForHand();    while(userHand != quit) {        //gets computerHand from the computer(random) as int        int computerHand = generateRandomHand();        //compares userHand to computerHand, determines the winner        String winner = determineWinner(userHand, computerHand);        //prints out the winner        System.out.println("The winner is the: " + winner);        //starts the next round        userHand = promptUserForHand();    }    //if userHand equals quit, stop program and say goodbye    if(userHand == quit) {        System.out.println("Goodbye!!!");        console.close();            }}
查看完整描述

1 回答

?
慕容708150

TA貢獻1831條經(jīng)驗 獲得超4個贊

你不應該==在java中使用比較字符串。而是使用:equals()equalsIgnoreCase()方法。

例如:

代替:

?if(userInput?==?"rock"?||?userInput?==?"Rock"?||?userInput?==?"r")

使用:

if(userInput.equalsIgnoreCase("rock")?||?userInput.equalsIgnoreCase("r"))
查看完整回答
反對 回復 2023-11-10
  • 1 回答
  • 0 關注
  • 117 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號