使用Scanner类的问题

时间:2016-12-29 07:04:09

标签: java input keyboard

该代码适用于输入妻子的姓名和年龄,但无法打印儿子的姓名,但会正确显示他们的年龄。

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

3 个答案:

答案 0 :(得分:1)

您的代码中的额外keyboard.nextLine();位置错误。

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

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

在每个keyboard.nextLine();之后保留额外的keyboard.nextInt();行,您的程序应运行正常。

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

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

希望这有帮助!

答案 1 :(得分: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 + ".");
    }
}

答案 2 :(得分:0)

@ECHO OFF :START CLS ECHO=1 to quit or 2 to print hello and go back to this screen CHOICE /C 12 /N IF ERRORLEVEL 2 (CALL :HELLO & GOTO START) EXIT :HELLO ECHO=hello TIMEOUT 2 1>NUL 不是您想要的方法。 您希望改为使用nextLine(),如以下更正和简化的代码所示。你真的不想管理新行,只需让扫描程序将所有输入视为由空格分隔的一串标记,然后按顺序读取它们。使用next()方法输入字符串,使用next()输入整数...

nextInt()