import java.util.*;
public class SwitchTest {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("请输入数字: ");
int choice = in.nextInt();
switch(choice)
{
case 1:
System.out.println("A");
break;
case 2:
System.out.println("B");
break;
case 3:
System.out.println("C");
break;
case 4:
System.out.println("D");
break;
default:
System.out.println("输入错误");
break;
}
System.out.print("是否要继续操作? (Y/N): ");
String name = in.nextLine();
}
}输出结果: 请输入数字: 4
D
是否要继续操作? (Y/N):
import java.util.*;
public class SwitchTest {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("请输入数字: ");
int choice = in.nextInt();
switch(choice)
{
case 1:
System.out.println("A");
break;
case 2:
System.out.println("B");
break;
case 3:
System.out.println("C");
break;
case 4:
System.out.println("D");
break;
default:
System.out.println("输入错误");
break;
}
System.out.print("是否要继续操作? (Y/N): ");
String name = in.nextLine();
name = in.nextLine();
}
}}输出结果: 请输入数字: 4
D
是否要继续操作? (Y/N): Y
问题一:
我想问一下为什么在第一个程序例子中,String name = in.nextLine();为什么不能继续输入Y呢问题二:
在第一个程序例子中如果把后面一句改为String name = in.next();为什么又能输入呢问题三:
在第二个程序例子中,只是加多一句 name = in.nextLine(); 就能继续输入y,
难道一定要这样调用才行的吗?
public class SwitchTest {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("请输入数字: ");
int choice = in.nextInt();
switch(choice)
{
case 1:
System.out.println("A");
break;
case 2:
System.out.println("B");
break;
case 3:
System.out.println("C");
break;
case 4:
System.out.println("D");
break;
default:
System.out.println("输入错误");
break;
}
System.out.print("是否要继续操作? (Y/N): ");
String name = in.nextLine();
}
}输出结果: 请输入数字: 4
D
是否要继续操作? (Y/N):
import java.util.*;
public class SwitchTest {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("请输入数字: ");
int choice = in.nextInt();
switch(choice)
{
case 1:
System.out.println("A");
break;
case 2:
System.out.println("B");
break;
case 3:
System.out.println("C");
break;
case 4:
System.out.println("D");
break;
default:
System.out.println("输入错误");
break;
}
System.out.print("是否要继续操作? (Y/N): ");
String name = in.nextLine();
name = in.nextLine();
}
}}输出结果: 请输入数字: 4
D
是否要继续操作? (Y/N): Y
问题一:
我想问一下为什么在第一个程序例子中,String name = in.nextLine();为什么不能继续输入Y呢问题二:
在第一个程序例子中如果把后面一句改为String name = in.next();为什么又能输入呢问题三:
在第二个程序例子中,只是加多一句 name = in.nextLine(); 就能继续输入y,
难道一定要这样调用才行的吗?
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货