2016-12-29 65 views
0

该代码适用于输入妻子的姓名和年龄,但未能打印儿子的姓名,尽管他们正确显示其年龄。使用扫描仪类的问题

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 + "."); 
    } 
} 
+1

阅读,将解决你的问题中,[扫描仪使用的next(),nextInt()或其他后跳过nextLine()可能的复制nextFoo()方法](http://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-nextint-or-other-nextfoo) –

+0

了解如何使用调试器和调试程序 –

+0

我提供了一个工作示例。如果您发现该解决方案能很好地解决您的问题,请对答案进行最后投票并点击√接受。谢谢 – clearlight

回答

1

你必须在你的代码错了地方的额外keyboard.nextLine();

son1 = keyboard.nextLine(); 
keyboard.nextLine(); // you don't need this here. 

son2 = keyboard.nextLine(); 
keyboard.nextLine(); // nor here 

保持每个keyboard.nextInt();后额外keyboard.nextLine();线和你的程序应该运行正常。

wifeAge = keyboard.nextInt(); 
keyboard.nextLine(); // put it here 
... 
son1Age = keyboard.nextInt(); 
keyboard.nextLine(); // and here 

nextInt()只读取整数值而不是新行。如果您需要阅读新行,则每次从键盘读取整数值时都需要输入keyboard.nextLine();

希望这会有所帮助!

0
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? "); 
keyboard.nextLine(); 
son1 = keyboard.nextLine(); 
System.out.println("His age? "); 
son1Age = keyboard.nextInt(); 
System.out.println(); 

System.out.println("Second son's name? "); 
keyboard.nextLine();  
son2 = 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 + "."); 
    } 
} 
0

nextLine()不是你想要的方法。 您想要改为使用next(),如以下经过更正和简化的代码所示。你不需要管理换行符,只需让扫描器将所有输入视为由空格分隔的一串令牌,然后依次读取。使用next()方法用于输入字符串和nextInt()输入整数...

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.next(); 
     System.out.println("Her age? "); 
     wifeAge = keyboard.nextInt(); 
     System.out.println(); 

     System.out.println("First son's name? "); 
     son1 = keyboard.next(); 
     System.out.println("His age? "); 
     son1Age = keyboard.nextInt(); 
     System.out.println(); 

     System.out.println("Second son's name? "); 
     son2 = keyboard.next(); 
     System.out.println("His age? "); 
     son2Age = keyboard.nextInt(); 
     System.out.println(); 

     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 + "."); 
     } 
    }