我真的在聽這個。我是 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();
添加回答
舉報
0/150
提交
取消