gpt-3.5-turbo-0301· openai
Suzhen/CodeChat
Write a piece of java code
- User
Write a piece of java code
- Assistant
Here is an example Java code that prompts the user for their name and greets them with a personalized message:
import java.util.Scanner; public class Greeting { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("What is your name? "); String name = input.nextLine(); System.out.println("Hello " + name + "! Nice to meet you."); } }This code prompts the user for their name using the
Scannerclass and stores it in aStringvariable calledname. It then prints out a personalized greeting using string concatenation, where the user's name is inserted between "Hello" and "Nice to meet you."