对不起我的英语 :)
我是 Java 编程新手,我对 Scanner 有疑问。我需要读取一个 Int,显示一些东西,然后读取一个字符串,所以我使用sc.nextInt(); 显示我的东西showMenu(); 然后尝试读取一个字符串palabra=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