In: Computer Science
Nice shirt! In Java, write a program that uses a function that takes in the user's name and prints out compliments directed to them. The program should provide a sentinel value by asking if they've had enough compliments and will continue until they type "yes". Include 3 different compliments pulled at random and BE SURE TO USE A FUNCTION/METHOD to take in the user's name.
Sample output:
Enter your name: John Smith
John Smith, you have a great smile!
Would you like another compliment? [yes/no] yes
John Smith, you are tall and handsome!
Would you like another compliment? [yes/no] yes
John Smith, you are a quick learner!
Would you like another compliment? [yes/no] no
Have a nice day!
import java.util.Random;
import java.util.Scanner;
public class Compliments {
public static void main(String[] args) {
Scanner scan = new
Scanner(System.in);
Random rand = new Random();
String name =
getUserName(scan);
// Store 3 compliments in an
array
String comp[] = { "you have a great
smile!", "you are tall and handsome!", "you are a quick learner!"
};
String sentinel = "yes";
// Iterate loop till user enters
yes
while
(sentinel.equalsIgnoreCase("yes")) {
// Print the
compliment by generating a random number between 0-2
System.out.println(name + ", " + comp[rand.nextInt(3)]);
// Ask user
if they want another compliment
System.out.print("Would you like another compliment? [yes/no]
");
sentinel =
scan.nextLine();
}
System.out.println("Have a nice
day!");
}
public static String getUserName(Scanner scan)
{
// Ask user to enter a name and
then return name
System.out.print("Enter your name:
");
String name =
scan.nextLine();
return name;
}
}
OUTPUT