SRY我的英語:)
進出口新的Java編程和我有掃描儀的一個問題。我需要讀一個Int,顯示一些東西,然後閱讀一個字符串,所以我使用sc.nextInt(); show me stuff showMenu();然後嘗試讀取的字符串言論報= sc.nextLine();Java掃描程序問題sc.nextLine();
有些人告訴我,我需要使用sc.nextLine();在sc.nextInt()之後;但我不明白爲什麼你必須這樣做:(
這裏是我的代碼:
public static void main(String[] args) {
// TODO code application logic here
Scanner sc = new Scanner(System.in);
int respuesta = 1;
showMenu();
respuesta = sc.nextInt();
sc.nextLine(); //Why is this line necessary for second scan to work?
switch (respuesta){
case 1:
System.out.println("=== Palindromo ===");
String palabra = sc.nextLine();
if (esPalindromo(palabra) == true)
System.out.println("Es Palindromo");
else
System.out.println("No es Palindromo");
break;
}
}
泰這麼多的時間和幫助:d
:O型TY這麼多,我想知道它太多時間:) – 2010-03-15 00:06:19
我第一次遇到了這個問題,這難倒我太多,但一旦你知道爲什麼它是有道理的! – 2010-03-15 00:16:46
很高興知道我不是唯一的一個:爲偉大的解釋d – 2010-03-15 05:39:08