-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalculator.java
More file actions
26 lines (24 loc) · 903 Bytes
/
Copy pathCalculator.java
File metadata and controls
26 lines (24 loc) · 903 Bytes
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
import java.util.Scanner;
public class Calculator {
public static float cal() {
Scanner read = new Scanner(System.in);
System.out.println("Enter the first number");
float first = read.nextFloat();
System.out.println("Enter the symbol of operation");
String op = read.next();
System.out.println("Enter the second number");
float second = read.nextFloat();
switch(op){
case "+": return first + second;
case "-": return first - second;
case "*": return first * second;
case "/": return first / second;
}
return 0; // here we use "return 0" only because it's necessary to return a final value for the function
}
static float result;
public static void main(String[] args){
result = cal();
System.out.println("The result is: " + result);
}
}