如何从用户输入后执行Print语句

时间:2013-09-13 15:27:05

标签: java

我的代码是:

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)`

5 个答案:

答案 0 :(得分: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
Enter your name: Joe
Your name is: Joe

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

答案 2 :(得分: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);
}

答案 3 :(得分:0)

这是你的代码:

        System.out.println("Enter your name: ");
        Scanner name = new Scanner(System.in);
        System.out.println("Your name is: "+name.nextLine());

答案 4 :(得分:0)

这里有很多解决方案,但您需要知道代码的问题,

未存储 input from the user

System.out.print("Enter your name: ");  
String name = name.nextLine();  // read input, store it