该代码适用于输入妻子的姓名和年龄,但未能打印儿子的姓名,尽管他们正确显示其年龄。使用扫描仪类的问题
import java.util.Scanner;
public class Check2
{
public static void main(String[] args)
{
String wife;
String son1;
String son2;
int wifeAge;
int son1Age;
int son2Age;
Scanner keyboard = new Scanner(System.in);
System.out.println("Wife's name? ");
wife = keyboard.nextLine();
System.out.println("Her age? ");
wifeAge = keyboard.nextInt();
System.out.println();
System.out.println("First son's name? ");
son1 = keyboard.nextLine();
keyboard.nextLine();
System.out.println("His age? ");
son1Age = keyboard.nextInt();
System.out.println();
System.out.println("Second son's name? ");
son2 = keyboard.nextLine();
keyboard.nextLine();
System.out.println("His age? ");
son2Age = keyboard.nextInt();
System.out.println();
keyboard.nextLine();
System.out.println("My wife's name is " + wife + ". She is " +
wifeAge + " years old.\nOur first son is " +
son1 + ". He is " + son1Age + ".\nOur " +
"second son is " + son2 + ". He is " +
son2Age + ".");
}
}
阅读,将解决你的问题中,[扫描仪使用的next(),nextInt()或其他后跳过nextLine()可能的复制nextFoo()方法](http://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-nextint-or-other-nextfoo) –
了解如何使用调试器和调试程序 –
我提供了一个工作示例。如果您发现该解决方案能很好地解决您的问题,请对答案进行最后投票并点击√接受。谢谢 – clearlight