package com.imooc;
import java.util.Scanner;
public class Bus extends Traffic {
Scanner input = new Scanner(System.in);
public void method(){
System.out.print("请输入运载人数:");
int num1 = input.nextInt();
System.out.print("请输入运载方式:");
String way1 = input.next();
System.out.println("巴士在"+way1+"运输"+num1+"人");
}
}
我在课后练习中尝试使用Scanner输入数据来获取不同交通工具的运输方式和运输人数,然后发现在输入字符串的过程中
如果使用System.out.print("请输入运载方式:");
String way1 = input.nextLine();
就会跳过运载方式的输入,结果如下:请输入运载人数:40
请输入运载方式:巴士在运输40人
如果使用System.out.print("请输入运载方式:");
String way1 = input.next();
才会正确的得到我想要的结果:请输入运载人数:40
请输入运载方式:陆地上
巴士在陆地上运输40人
请问这是为什么?