亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

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

java:帶開關的循環僅有時有效

java:帶開關的循環僅有時有效

墨色風雨 2021-11-17 16:55:09
我真的在聽這個。我是 Java 新手,遇到了最奇怪的事情。這是家庭作業,我一步一步來。我的問題是循環只是繼續并停止要求輸入,只是一直循環直到它終止。我的評論主要是針對我自己的。我試圖提取導致我的問題的原因并將其張貼在這里??纯础癶atColor”開關,您會注意到我確保用戶僅從我分配的選項中輸入的方式。我應該使用異常處理程序還是什么?無論如何,簡而言之,問題是如果我輸入帶有空格的內容,循環會跳過詢問我的下一個輸入。就像,如果我在第一次提示時向掃描儀輸入“yyyyy”,程序將終止并且不給我輸入其他內容的機會。拜托,任何理解這一點的人,我真的很感激你的幫助。import java.util.Scanner;public class Testing{    static String hatColor;    public static void main(String[] args) {    gameStart();        }public static void gameStart() {           Scanner userInput = new Scanner(System.in);    boolean keepLooping = true;    int loopCounter = 0;    System.out.println("The game begins. You must choose between 3 different colored hats."            + " You can type white, black, or gray.");    while (keepLooping == true)     {        hatColor = userInput.next();        switch(hatColor)        {        case "white":            System.out.println("You have chosen the path of well intentioned decisions.");            walletDrop();            //the two items below are only there in case the wallet drop somehow completes without calling another method            keepLooping = false; // stops the while loop from looping again.            break; // breaks out of the  switch        case "gray":             System.out.println("You have chosen the path of free will.");            walletDrop();            keepLooping = false;            break;        case "black" :            System.out.println("You have chosen the path of personal gain.");            walletDrop();            keepLooping = false;            break;
查看完整描述

1 回答

?
慕容3067478

TA貢獻1773條經驗 獲得超3個贊

所以我實際上在發布后就解決了這個問題。如果其他人需要在這里尋求幫助:

我遇到的問題是由于使用掃描儀方法

variableToAssign = 掃描儀名稱.next();

代替

variableToAssign = 掃描儀名稱.nextLine();


查看完整回答
反對 回復 2021-11-17
  • 1 回答
  • 0 關注
  • 123 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號