0
我想設置一個簡單的文本樹,讀取單個字符(y/n)或對應於打印列表(1-4)的整數。我想知道最簡單的方法,使程序忽略不符合給定的選項,像這樣用戶輸入:Java:如何讓掃描器對象忽略不需要/無效的輸入?
import java.util.Scanner;
public class simpleMenu
{
Scanner sc = new Scanner(System.in);
String choicePick;
public static void main(String[] args)
{
System.out.println("Would you like to continue? (y/n)");
choicePick = sc.next();
if(choicePick.equals("y"))
{
// The program continues.
}
else if(choicePick.equals("n"))
{
// The program closes.
}
else
{
/*
The scanner ignores the input, ideally without having to restate the question.
The program does not quit or move on until "y" or "n" is entered.
*/
}
}
}
獎勵積分,如果你能幫助我實現「返回」選項,帶我去以前的選擇。
你不知道。一旦你調用了'sc.next()',你已經讀取了輸入。 – azurefrog
將學習部分放在其他人身上並沒有什麼好處。 – GhostCat
這不是一個學校作業。我的意思是獎勵積分;這只是一個表達。 – Danderson