【发布时间】:2017-05-13 11:39:21
【问题描述】:
该代码可用于输入妻子的姓名和年龄,但无法打印儿子的姓名,尽管它正确显示了他们的年龄。
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 + ".");
}
}
【问题讨论】:
-
了解如何使用调试器和调试程序
-
我提供了一个工作示例。如果您发现解决方案很好地解决了您的问题,请对答案进行投票并单击√接受。谢谢