【问题标题】:Problems using Scanner class使用 Scanner 类的问题
【发布时间】: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 + ".");
    }
}

【问题讨论】:

标签: java input keyboard


【解决方案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();

希望这会有所帮助!

【讨论】:

    【解决方案2】:
    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 + ".");
        }
    }
    

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-08-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-07-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多