" so it's more obvious that it's the user's turn to enter something. So far I just put "\n>" at the end of my question and that works fine, but I have to do it every time."/> " so it's more obvious that it's the user's turn to enter something. So far I just put "\n>" at the end of my question and that works fine, but I have to do it every time.">

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
PHPzforward
2024-02-22 14:50:081030browse

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

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();
}

Calling method

String name = prompt("Enter your name");

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!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete