0

我的代码是:

import java.util.Scanner;
class shoham
{
    public static void main (String args[])
    {
        System.out.println("Enter your name: ");
        Scanner name = new Scanner(System.in);
        System.out.println("Your name is: ");
        System.out.print(name.nextLine());
    }
}

当我执行这个命令时,我得到如下输出:

Enter your name:
Your name is: 
*****                               (user input)
*****                                (output)

任何人都可以给出一个给出如下输出的代码:

Enter your name: **** - (user input)
Your name is: **** - (output from user input)`
4

5 回答 5

1

好吧,您正在阅读打印语句之后的输入。你可能想要:

Scanner in = new Scanner(System.in);  // do this first, and give it a 
                                      // better name

System.out.print("Enter your name: ");  // use print to keep it on one line
String name = in.nextLine();  // read input, store it
System.out.print("Your name is: ");
System.out.println(name);  // print name
输入您的姓名:
你的名字是:乔
于 2013-09-13T15:30:01.823 回答
0

您可以尝试这样的事情,即尝试print仅使用而不是println:-

Scanner name = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = name.nextLine();
System.out.print("Your name is: ");
System.out.println(name);
于 2013-09-13T15:30:30.910 回答
0

试试这个

public static void main (String args[])
{
    System.out.print("Enter your name: ");
    Scanner scanner = new Scanner(System.in);
    String name = scanner.nextLine()
    System.out.println("Your name is: "+name);
}
于 2013-09-13T15:31:01.673 回答
0

这是你的代码:

        System.out.println("Enter your name: ");
        Scanner name = new Scanner(System.in);
        System.out.println("Your name is: "+name.nextLine());
于 2013-09-13T15:31:04.657 回答
0

你在这里有很多sloutions,但你需要知道你的代码的问题是,

没有input from the user存储.

System.out.print("Enter your name: ");  
String name = name.nextLine();  // read input, store it
于 2013-09-13T15:32:02.707 回答