我使用哪一行代码来打印出用户输入的输入内容

时间:2018-09-02 06:51:19

标签: java eclipse

我是编码的新手,我一直在通过编写一个小型程序来测试自己的技能,以使自己变得更好。我正在尝试接受用户输入,然后将其打印回给他们。例如,如果有人在系统中键入他们的名字,并且程序以“有名字”作为响应,然后继续说下一步,那么我对程序进行了编程。因此,如果有人看到我遇到的问题,并且有任何有用的建议,那将是非常好的。感谢大家的帮助。

import java.util.Scanner;

public class Main {

public static void main(String[] args) {
    // Scanner
    Scanner scanner = new Scanner(System.in);

    //Taking input using Scanner 
    System.out.println("Hello, How may I assist you today?");
    String name1 = scanner.nextLine();
    System.out.println("where are you diving to today?");
    String name2 = scanner.nextLine();
    System.out.println("what is your name?");   
    String name3 = scanner.nextLine();
    System.out.println("Ok " + "new Scanner(System.in)");
    String name5 = scanner.nextLine();
    System.out.println("What time do you want to arrive to your destination?");
    String name6 = "new Scanner(System.in)";
    System.out.println(" + ", "new Scanner(System.in)" , " + " great, what time are you leaving? ");
    String name7 = scanner.nextLine();
    System.out.println(" + " "new Scanner(System.in)" ," + ", "Good deal.");

}

}

1 个答案:

答案 0 :(得分:1)

将下面的语句用于name1,

System.out.println("your string" + name1);

与其他字符串类似。

因此,您的最终代码将如下所示

System.out.println("Hello, How may I assist you today?");
String name1 = scanner.nextLine();
System.out.println("user input is: " + name1);
System.out.println("where are you driving to today?");
String name2 = scanner.nextLine();
System.out.println("User input is: " + name2);
 System.out.println("I apolgize but I forgot to get your name?");   
String name3 = scanner.nextLine();
 System.out.println("Thanks" + name3);