by CodeChum Admin
You have been cordially invited to partake in Operation: Operation. Your mission, should you choose to accept it, is to take the two numbers and the operator given then perform the operation successfully.
Instructions:
Instructions
Input
The first line contains the first number.
The second line contains the operator.
The third line contains the second number.
5
+
0.70
Output
A line containing a decimal/float containing two decimal places.
5.70
"The code should use If-Else-Elseif statement"
import java.io.IOException;
import java.util.Locale;
import java.util.Scanner;
public class Main {
public void Calcul(float firstNum, char operat, float secondNum){
if(operat == '+'){
System.out.printf("%.2f", firstNum+secondNum);
} else if(operat == '-'){
System.out.printf("%.2f", firstNum-secondNum);
} else if(operat == '*'){
System.out.printf("%.2f", firstNum*secondNum);
} else if(operat == '/'){
System.out.printf("%.2f", firstNum/secondNum);
}
}
public static void main(String[] args) throws IOException {
Scanner in = new Scanner(System.in);
in.useLocale(Locale.US);
Main cal = new Main();
float firstNum = in.nextFloat();
char operat = (char) System.in.read();
float secondNum = in.nextFloat();
cal.Calcul(firstNum, operat, secondNum);
}
}
Comments
Leave a comment