Advertisement
lcfr822

Repeating Menu Example

Nov 28th, 2020 (edited)
1,167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.62 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class MathMenu {
  4.     public static void main(String[] args) {
  5.         Scanner input = new Scanner(System.in);
  6.         //Geometry geometry = new Geometry();
  7.        
  8.         int choice = 0;
  9.        
  10.         // Loops indefinitely until the user enters an int between 1 and 4 (inclusive).
  11.         while(choice < 1 || choice > 4) {
  12.             System.out.println("1. Calculate the Area of a Circle");
  13.             System.out.println("2. Calculate the Area of a Rectangle");
  14.             System.out.println("3. Calculate the Area of a Triangle");
  15.             System.out.println("4. Quit");
  16.            
  17.             // This line will prevent instantaneous looping, aka the terrible Infinite Loop!
  18.             choice = input.nextInt();
  19.         }
  20.        
  21.         switch (choice)
  22.         {
  23.         case 1:
  24.             System.out.println("Radius of circle");
  25.             double r = input.nextDouble();
  26.             //System.out.println("Area: " + geometry.areaOfCircle(r));
  27.             break;
  28.         case 2:
  29.             double l, w;
  30.             System.out.println("Length of rectangle");
  31.             l = input.nextDouble();
  32.             System.out.println("Width of rectangle");
  33.             w = input.nextDouble();
  34.             //System.out.println("Area: " + geometry.areaOfRectangle(l, w));
  35.             break;
  36.         case 3:
  37.             double b, h;
  38.             System.out.println("Base of triangle");
  39.             b = input.nextDouble();
  40.             System.out.println("Height of triangle");
  41.             h = input.nextDouble();
  42.             //System.out.println("Area: " + geometry.areaOfTriangle(b, h));
  43.             break;
  44.         case 4:
  45.             System.out.println("Quitting");
  46.             input.close();
  47.             System.exit(0);
  48.             break;
  49.         default:
  50.             System.out.println("This should be unreachable, if this prints... well oh dear!");
  51.             input.close();
  52.             System.exit(-1);
  53.             break;
  54.         }
  55.        
  56.         input.close();
  57.     }
  58. }
  59.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement