0
import java.util.Scanner;

public class Words
{
    public static void main (String[] args)
{
    Scanner myScan = new Scanner(System.in);
    String s1;
    int myAge;
    int time = 6;

    System.out.print("What is your name? ");
    s1 = myScan.nextLine();

    System.out.print("How old are you? ");
    myAge = myScan.nextInt(); 

    System.out.println("Really? Cause I am " + (myAge+3) + ". " + "Lets's meet up! ");
    s1 = myScan.nextLine();

    }
}

//在最后一个命令之后,它不会让我在终端窗口中输入任何内容。请帮忙。

4

2 回答 2

3

nextLine()在中间加一个

System.out.print("How old are you? ");
myAge = myScan.nextInt(); 

myScan.nextLine(); // add this

System.out.println("Really? Cause I am " + (myAge+3) + ". " + "Lets's meet up! ");
s1 = myScan.nextLine();

这是必需的,因为nextInt()只使用int它读取的值,而不是它后面的任何行尾字符。

nextLine()消耗\r\n(或任何行尾/分隔符),并且下一个令牌将可供另一个 消耗nextLine()

于 2013-09-21T22:56:31.490 回答
0

当你在插入 int 之后输入 enter。nextline 将输入作为一行。您需要在 nextint 之后添加一个额外的 nextline 调用,如下所示:

import java.util.Scanner;

public class Words
{
    public static void main (String[] args)
    {
        Scanner myScan = new Scanner(System.in);
        String s1;
        int myAge;
        int time = 6;

        System.out.print("What is your name? ");
        s1 = myScan.nextLine();

        System.out.print("How old are you? ");
        myAge = myScan.nextInt(); 
        s1 = myScan.nextLine();

        System.out.println("Really? Cause I am " + (myAge+3) + ". " + "Lets's meet up! ");
        s1 = myScan.nextLine();
    }
}
于 2013-09-21T23:01:21.037 回答