Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.IOException;
- import java.util.Scanner;
- public class SwitchCalc {
- static int resultOfOperation (int firstDigit,int secondDigit, String operation) {
- int result = 0;
- switch (operation){
- case "+":
- result = firstDigit + secondDigit;
- break;
- case "-":
- result = firstDigit - secondDigit;
- break;
- case "*":
- result = firstDigit * secondDigit;
- break;
- case "/":
- if (secondDigit==0)
- result = 0;
- else
- result = firstDigit / secondDigit;
- break;
- default:
- System.out.println("Wrong operation");
- }
- return result;
- }
- static double resultOfOperation (double firstDigit,double secondDigit, String operation) {
- double result = 0;
- switch (operation){
- case "+":
- result = firstDigit + secondDigit;
- break;
- case "-":
- result = firstDigit - secondDigit;
- break;
- case "*":
- result = firstDigit * secondDigit;
- break;
- case "/":
- if (secondDigit==0)
- result = 0;
- else
- result = firstDigit / secondDigit;
- break;
- default:
- System.out.println("Wrong operation");
- }
- return result;
- }
- public static void main(String[] args) throws IOException {
- double sum;
- Scanner reader = new Scanner(System.in);
- System.out.println("Entering value: ");
- System.out.print("Enter the first value: ");
- String str = reader.nextLine();
- String strParse1 = str.replace(',', '.');
- double inputNum1 = Double.parseDouble(strParse1);
- System.out.print("Enter the second value: ");
- String str3 = reader.nextLine();
- String strParse2 = str3.replace(',', '.');
- double inputNum2 = Double.parseDouble(strParse2);
- System.out.println("Result: ");
- System.out.println("A comma in the first value is in position: " + str.indexOf(','));
- System.out.println("A comma in the second value is in position: " + str3.indexOf(','));
- sum = SwitchCalc.resultOfOperation (inputNum1,inputNum2, "+");
- System.out.println(strParse1 + " + " + strParse2 + " = " + sum);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement