-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcalculator.java
More file actions
70 lines (55 loc) · 2.49 KB
/
calculator.java
File metadata and controls
70 lines (55 loc) · 2.49 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/* Explaining one last time here, this is our scanner package used for Scanner class */
import java.util.Scanner;
class Calculator {
public static void main(String args[]) {
int choice; /* This will store the user choice */
int a,b,result; /* This will be used for user input and calculation */
Scanner sc = new Scanner(System.in);
System.out.println("Welcome to JAVA calculator. \n Press 1 for Addition , 2 for Subtraction , 3 for multiplication and 4 for division");
choice = sc.nextInt();
/*************************************************************************************************************
Switch statement -
So basically switch statement is like whatever user inputs, it will go into that piece of code and will execute it
and will not touch the rest of the code.
If the case below if user input 1, the switch case will jump to case 1: that is our addition section.
break' statements are used after every case to stop the program execution and limit it only to that case.
Let us suppose we forgot to add the break; statement and user want addition, the control will jump on addition, will add
two numbers, then it will show the output will come to the end and search for break; like "oh here is no break so i must
go on till i find a break" and it will jump to our case 2 that is subtraction and so on and as a result everything
will be shown as an output which we do not want so break is necessary when we use switch statements
**************************************************************************************************************/
switch(choice) {
case 1:
System.out.println("Enter the two numbers which you want to ADD");
a = sc.nextInt();
b = sc.nextInt();
result = a+b;
System.out.println("The Addition is : " +result);
break;
case 2:
System.out.println("Enter the two numbers which you want to SUBTRACT");
a = sc.nextInt();
b = sc.nextInt();
result = a-b;
System.out.println("The Subtraction is : " +result);
break;
case 3:
System.out.println("Enter the two numbers which you want to MULTIPLY");
a = sc.nextInt();
b = sc.nextInt();
result = a*b;
System.out.println("The Multiplication is : " +result);
break;
case 4:
System.out.println("Enter the two numbers which you want to DIVIDE");
a = sc.nextInt();
b = sc.nextInt();
result = a/b;
System.out.println("The Division is : " +result);
break;
default:
System.out.println("INVALID CHOICE");
break;
}
}
}