In: Computer Science
Can you assist me in understand and write the steps in this code?
Write a method named howMany that does not take in any arguments. Use the Scanner class to ask the user to enter a number between 1 and 5. Print One of the following based on their entry.
1 2 3 4 5 Anything Other Number
“Lonely Num” “Company” "Crowd" "Fun" "Party" "Only 1 to 5 Please"
import java.util.Scanner;
public class Example1 {
public static void main(String[] args) {
howMany();
}
private static void howMany() {
Scanner sc = new
Scanner(System.in);
System.out.println("Enter number
(1-5)");
int i=sc.nextInt();
//storing the messages in array so
that we can print directly
String arr[]= {"Lonely Num",
"Company", "Crowd" ,"Fun", "Party"};
//checking if it is between
1-5
if(i<1 || i>5)
System.out.println("Only 1 to 5 Please");
else
// printing i-1
as indexes starts from 0 to length-1 (0-4)
System.out.println(arr[i-1]);
}
}