-
Notifications
You must be signed in to change notification settings - Fork 0
/
C5 Function if-switch-scanner.java
38 lines (31 loc) · 1.16 KB
/
C5 Function if-switch-scanner.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
// if-else-switch
import java.util.*;
class Program{
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
System.out.println("Area (A) or Permimeter (P) ? :");
String choice = scan.nextLine();
System.out.println("Enter Length :");
double length = scan.nextDouble();
System.out.println("Enter Width :");
double width = scan.nextDouble();
scan.close();
Calculate(length,width,choice);
}
public static void Calculate(double length,double width,String option){
if(length < 0 || width < 0){
System.out.println("Length or Width must be a positive value.");
System.exit(0); // shut everything. end the further code.
}
switch(option){
case "A":
System.out.println("Area: " + (length*width) + " unit sq.");
break;
case "P":
System.out.println("Perimeter: " + (2*(length+width)) + " unit sq.");
break;
default:
System.out.println("Invalid Choice!");
}
}
}