gpt4 book ai didi

java - 如何在收到用户输入后执行打印语句

转载 作者:行者123 更新时间:2023-12-01 14:07:27 26 4
gpt4 key购买 nike

我的代码是:

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

最佳答案

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

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: JoeYour name is: Joe

关于java - 如何在收到用户输入后执行打印语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18789997/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com