Question
I am learning Java in programming right now. I need to write a program that should have an output like this:
The program says "Type ten names". Then ten names sould be typed a person. Then the program chooses a random name from the list and displays it on screen. What should be the Java code for this task? Thank you.
The program says "Type ten names". Then ten names sould be typed a person. Then the program chooses a random name from the list and displays it on screen. What should be the Java code for this task? Thank you.
Answers
anonymous
Scanner scan = new Scanner(System.in);
//take 10 string values from user
System.out.println("Enter 10 names: ");
String n = scan.nextLine();
String [] names = new String{n};
//store the names in an array
for (int i = 0; i < 10; i++){
names[i] = scan.nextLine();
}
//sequentially print the names and upperCase them
for (String i : names){
System.out.println(i.toUpperCase());
}
scan.close();
}
//take 10 string values from user
System.out.println("Enter 10 names: ");
String n = scan.nextLine();
String [] names = new String{n};
//store the names in an array
for (int i = 0; i < 10; i++){
names[i] = scan.nextLine();
}
//sequentially print the names and upperCase them
for (String i : names){
System.out.println(i.toUpperCase());
}
scan.close();
}