11. Inside class StaticMethods, create the static method menuInteger that displays a menu for the user to select 1000 , 2000 or 3000 The method returns the INTEGER( int) entered, ONLY when the user types 1000, 2000 or 3000, otherwise prints a message and stays in a loop. Test it from main().
Java Netbeans
PROGRAM CODE:
// include the required libraries
import java.util.*;
class StaticMethods
{
// start definition of menuInteger function
public static int menuInteger()
{
// create object of scanne class
Scanner scan = new Scanner(System.in);
int ch;
// display the Menu
System.out.println("Menu: ");
System.out.println("1. 1000");
System.out.println("2. 2000");
System.out.println("3. 3000");
// use loop to iteration
while(true)
{
System.out.println("Enter the integer from menu: ");
// take the integer value from the user
ch = scan.nextInt();
// condition for valid input
if(ch==1000 || ch==2000 || ch==3000)
{
return ch;
}
// condition for invalid input
else
{
System.out.println("Invalid choice");
continue;
}
}
}
}
public class Main
{
// start definition of main function
public static void main(String[] args)
{
// declare the required variables
int result;
// create object of the required class
StaticMethods obj = new StaticMethods();
// call the function and store the result returned by the function
result = obj.menuInteger();
System.out.println("The user entered: "+result);
}
}
Step by step
Solved in 2 steps with 1 images