Upload files to "OOP/Java/Lab/Week2"
This commit is contained in:
parent
40f695b27c
commit
24683dac5a
3 changed files with 85 additions and 0 deletions
47
OOP/Java/Lab/Week2/Calculator.java
Normal file
47
OOP/Java/Lab/Week2/Calculator.java
Normal file
|
@ -0,0 +1,47 @@
|
|||
import java.util.Scanner;
|
||||
|
||||
public class Calculator {
|
||||
|
||||
public static void main(String args[]){
|
||||
Scanner sc = new Scanner(System.in);
|
||||
char ch = 'y',f;
|
||||
do{
|
||||
System.out.println("Enter the first number: ");
|
||||
float a = sc.nextFloat();
|
||||
|
||||
System.out.println("Enter the operator (+,-,/,*): ");
|
||||
char b = sc.next().charAt(0);
|
||||
|
||||
if((b != '+') && (b != '-') && (b != '/') && (b != '*') && (b != 'x')){
|
||||
System.out.println("Invalid operator.");
|
||||
}
|
||||
|
||||
System.out.println("Enter the second number: ");
|
||||
float c = sc.nextFloat();
|
||||
|
||||
switch(b){
|
||||
case '+':
|
||||
float d = a+c;
|
||||
System.out.println(+a+" + "+c+" = "+d);
|
||||
break;
|
||||
case '-':
|
||||
float e = a-c;
|
||||
System.out.println(+a+" - "+c+" = "+e);
|
||||
break;
|
||||
case '/':
|
||||
float g = a / c;
|
||||
System.out.println(+a+" / "+c+" = "+g);
|
||||
break;
|
||||
case 'x':
|
||||
case '*':
|
||||
float h = a*c;
|
||||
System.out.println(+a+" * "+c+" = "+h);
|
||||
break;
|
||||
}
|
||||
|
||||
System.out.println("Do you want to perform another calculation (y/n)?");
|
||||
f = sc.next().charAt(0);
|
||||
} while(f == ch);
|
||||
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue