0

Program is still taking input after System.exit().

Still it is taking two numbers as an input after using System.exit(0). public class Calci { private static double add(double num1,double num2) { return num1+num2; } private static double sub(double num1,double num2) { return num1-num2; } private static double multiply(double num1,double num2) { return num1*num2; } private static double divide(double num1,double num2) { return num1/num2; } public static Scanner sc = new Scanner(System.in); public static void main(String[] args) { while(true) { System.out.println("This is a simple calculator.\n\nFunctions :-" + "\n1. Addition." + "\n2. Subtraction." + "\n3. Multiplication." + "\n4. Division." + "\n5. Exit." + "\nI want to do "); int ch = sc.nextInt(); System.out.println("Enter two numbers."); double num1 = sc.nextDouble(); double num2 = sc.nextDouble(); switch(ch) { case 1: System.out.println(add(num1, num2)); break; case 2: System.out.println(sub(num1, num2)); break; case 3: System.out.println(multiply(num1, num2)); break; case 4: System.out.println(divide(num1, num2)); break; case 5: System.exit(0); break; default: System.out.println("Invalid Input. Pls try again."); } } } }

19th Nov 2020, 3:40 PM
VAIBHAV PAL
VAIBHAV PAL - avatar
1 Respuesta
0
double num1=0, num2=0; if (ch != 5) { System.out.println("Enter two numbers."); num1 = sc.nextDouble(); num2 = sc.nextDouble(); }
19th Nov 2020, 5:32 PM
zemiak