Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ProcessingFiles; //Does not catch any errors... May make a new version which utilises collections with try catch.
- import java.util.Scanner;
- public class App {
- public static void main(String[] args) {
- Scanner input = new Scanner(System.in);
- System.out.println("Enter your first value: ");
- String firstValue = input.nextLine();
- System.out.println("Enter the second value: ");
- String secondValue = input.nextLine();
- System.out.println("Enter a chosen operator between +, -, /, *: ");
- String op = input.nextLine();
- outPut(firstValue, secondValue, op);
- }
- //Bad recursion practice. May lead to stack overflow.
- public static void outPut(String val1, String val2, String op) {
- double result = calc(val1, val2, op);
- if (result == -9999) {
- System.out.println("You've entered an invalid operator");
- }
- else System.out.println("Result: " + result);
- Scanner input = new Scanner(System.in);
- System.out.println("Would you like to go again? Enter yes or no: ");
- String answer = input.nextLine();
- if(answer.equals("yes")) {
- System.out.println("Enter your first value: ");
- String firstValue = input.nextLine();
- System.out.println("Enter the second value: ");
- String secondValue = input.nextLine();
- System.out.println("Enter a chosen operator between +, -, /, *: ");
- String operator = input.nextLine();
- outPut(firstValue, secondValue, operator);
- }
- else System.out.println("Thank you.");
- }
- public static double calc(String val1, String val2, String op) {
- int firstValue = Integer.parseInt(val1);
- int secondValue = Integer.parseInt(val2);
- if (op.equals("+"))
- return firstValue + secondValue;
- else if (op.equals("-"))
- return firstValue - secondValue;
- else if (op.equals("*"))
- return firstValue * secondValue;
- else if (op.equals("/"))
- return firstValue / secondValue;
- else
- return -9999;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement