-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalc.java
More file actions
25 lines (24 loc) · 968 Bytes
/
Copy pathcalc.java
File metadata and controls
25 lines (24 loc) · 968 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
import java.util.Scanner;
class calc {
public static void main(String[] args)
{ float x, y;
Scanner sc = new Scanner(System.in);
System.out.println("Enter one number: ");
x=sc.nextFloat();
System.out.println("Enter the operator (+,-,*,/,^): ");
char op=sc.next().charAt(0);
if (op!='+' || op!='-' || op!='*' || op!='/' || op!='^');
{ System.out.println("Please enter a valid operator"); }
System.exit(0);
System.out.println("Enter another number: ");
y=sc.nextFloat();
switch (op) {
case '+' -> System.out.println(x + y); //enhanced switch case
case '-' -> System.out.println(x - y);
case '*' -> System.out.println(x * y);
case '/' -> System.out.println(x / y);
case '^' -> System.out.println(Math.pow(x, y));
default -> {
}
}
sc.close(); }}