Computer Science, asked by aashi1357, 1 month ago

how to write a menu driven program in java? ​

Answers

Answered by Shivu516
0

The simplest way to write a menu-driven program is to use if-else statements.

For example:

___________________________________

import java.util.Scanner;

public class Area_Waala_Program{

  public static void main(String [] args){

      double a, b, c, s, area = 0;

      //Taking input from the user

      Scanner sc = new Scanner(System.in);

      System.out.println("Area Calculator for Simple Closed Figures");  

      System.out.println("Press 1 for Circle");

      System.out.println("Press 2 for Square");

      System.out.println("Press 3 for Rectangle");

      System.out.println("Press 4 for Triangle");

      double choice = sc.nextDouble();

      //Main part comes here

      //Circle

      if (choice == 1){

          System.out.println("Circle");

          System.out.print("Enter the Radius: ");

          a = sc.nextDouble();

          area = (22*a*a)/7;

          System.out.println("The area of this Circle is " + area + " sq.unit/s");

      }

      //Square

      if (choice == 2){

          System.out.println("Square");

          System.out.print("Enter the Side: ");

          a = sc.nextDouble();

          area = a*a;

          System.out.println("The area of this Square is " + area + " sq.unit/s");

      }

      //Rectangle

      if (choice == 3){

          System.out.println("Rectangle");

          System.out.print("Enter the Length: ");

          a = sc.nextDouble();

          System.out.print("Enter the Breadth: ");

          b = sc.nextDouble();

          area = a*b;

          System.out.println("The area of this Rectangle is " + area + " sq.unit/s");

      }

      //Triangle

      if (choice == 4){

          System.out.println("Triangle");

          System.out.print("Enter the measure of the First side: ");

          a = sc.nextDouble();

          System.out.print("Enter the measure of the Second side: ");

          b = sc.nextDouble();

          System.out.print("Enter the measure of the Third side: ");

          c = sc.nextDouble();

          boolean truth = (a+b) > c && (a + c) > b && (b + c) > a;

          if (truth == true)

          {

          s = (a + b + c)/2;

          area = Math.sqrt(s * (s - a) * (s - b) * (s - c));  

          System.out.println("The area of this Triangle is " + area + " sq.unit/s");

         }

         else if (truth == false){

             System.out.println("These sides cannot form a triangle *_*");

         }

      }

      if (choice > 4 || choice < 1){

          System.out.println("Valid choices are only 1-4");

      }

  }

}

___________________________________

Thanks for reading till here ˶ᵔ ᵕ ᵔ˶

Similar questions