Home > Java > How do I add '>' where I want the user to enter text? (Java)

How do I add '>' where I want the user to enter text? (Java)

PHPz
Release: 2024-02-22 14:50:08
forward
986 people have browsed it

php editor Xigua brings you a java question and answer today: How to add ">" where you want the user to input text? This problem is often encountered in Java programming and is a practical and common requirement. In Java, through simple code operations, you can add ">" where the user inputs text, making the program more friendly and intuitive. Next, we will introduce the solution to this problem in detail, hoping to help everyone.

Question content

If I have a System.out.println("what's your name"), I want the next line to say ">" so it's more obvious that it's the user's turn Something has been entered.

So far I've just put "\n >" at the end of my question and that works fine, but I have to do it every time.

Solution

Add a prompt method to return a string

Create a scanner as a field in the class

scanner keyboard = new scanner(system.in);
Copy after login

Create a method to display a prompt and return a string from scanner. You may need to add static.

public string prompt(string message) {
    system.out.print(message + "\n>");
    return keyboard.nextline();
}
Copy after login

Calling method

String name = prompt("Enter your name");
Copy after login

The above is the detailed content of How do I add '>' where I want the user to enter text? (Java). For more information, please follow other related articles on the PHP Chinese website!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template