How to create a menu-driven program in java?
Answers
Answered by
1
ram for menu driven system in java.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//import Scanner as we require it.
import java.util.Scanner;
// the name of our class its public
public class MenuDrivenSystem {
//void main
public static void main (String[] args)
{
//declare float
float a,b,area = 0;
char choice;
//Declare input as scanner
Scanner input = new Scanner(System.in);
//Take inputs
System.out.println("Enter c for circle.");
System.out.println("Enter s for square.");
System.out.println("Enter r for rectangle.");
System.out.println("Enter t for triangle.");
String s = input.next();
choice = s.charAt(0);
//add a switch statement
switch(choice)
{
case 'c':
System.out.println("Enter radius:");
a = input.nextFloat();
area = 3.14f*a*a;
break;
case 's':
System.out.println("Enter side:");
a = input.nextFloat();
area = a*a;
break;
case 'r':
System.out.println("Enter length and breadth:");
a = input.nextFloat();
b = input.nextFloat();
area = a*b;
break;
case 't':
System.out.println("Enter base and height:");
a =input.nextFloat();
b =input.nextFloat();
area = 0.5f*a*b;
break;
default:
System.out.println("Error");
}
System.out.println("Area = "+area);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//import Scanner as we require it.
import java.util.Scanner;
// the name of our class its public
public class MenuDrivenSystem {
//void main
public static void main (String[] args)
{
//declare float
float a,b,area = 0;
char choice;
//Declare input as scanner
Scanner input = new Scanner(System.in);
//Take inputs
System.out.println("Enter c for circle.");
System.out.println("Enter s for square.");
System.out.println("Enter r for rectangle.");
System.out.println("Enter t for triangle.");
String s = input.next();
choice = s.charAt(0);
//add a switch statement
switch(choice)
{
case 'c':
System.out.println("Enter radius:");
a = input.nextFloat();
area = 3.14f*a*a;
break;
case 's':
System.out.println("Enter side:");
a = input.nextFloat();
area = a*a;
break;
case 'r':
System.out.println("Enter length and breadth:");
a = input.nextFloat();
b = input.nextFloat();
area = a*b;
break;
case 't':
System.out.println("Enter base and height:");
a =input.nextFloat();
b =input.nextFloat();
area = 0.5f*a*b;
break;
default:
System.out.println("Error");
}
System.out.println("Area = "+area);
}
Similar questions