How do I convert this Case Switch to a proper if else my case switch goes as it follows below
** ***package com.company.CalculatorProject;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
boolean running = true;
Scanner scanner = new Scanner(System.in);
Calculator calculator = new Calculator();
while(running)
{
System.out.println("CHOOSE AN OPTION");
System.out.println("\t1. Add");
System.out.println("\t2. Subtract");
System.out.println("\t3. Multiply");
System.out.println("\t4. Divide");
System.out.println("\t5. Reset");
System.out.println("\t6. Quit");
System.out.print("Enter Option: ");
int choice = scanner.nextInt();
double a, b, result;
switch(choice)
{
case 1:
System.out.print("Enter first number: ");
a = scanner.nextDouble();
System.out.print("Enter second number: ");
b = scanner.nextDouble();
result = calculator.Add(a, b);
System.out.println(a + " + " + b + " = " + result);
break;
case 2:
System.out.print("Enter first number: ");
a = scanner.nextDouble();
System.out.print("Enter second number: ");
b = scanner.nextDouble();
result = calculator.Subtract(a, b);
System.out.println(a + " - " + b + " = " + result);
break;
case 3:
System.out.print("Enter first number: ");
a = scanner.nextDouble();
System.out.print("Enter second number: ");
b = scanner.nextDouble();
result = calculator.Multiply(a, b);
System.out.println(a + " * " + b + " = " + result);
break;
case 4:
System.out.print("Enter first number: ");
a = scanner.nextDouble();
System.out.print("Enter second number: ");
b = scanner.nextDouble();
result = calculator.Divide(a, b);
System.out.println(a + " / " + b + " = " + result);
break;
case 5:
System.out.print("Are you sure you want to reset? [0 for NO or 1 for YES]: ");
if(scanner.nextInt() == 1)
calculator.Reset();
break;
case 6:
System.out.print("Are you sure you want to quit? [0 for NO or 1 for YES]: ");
if(scanner.nextInt() == 1)
running = false;
break;
}
System.out.println("The calculator has performed " + calculator.GetCount() + " operations.\n");
}
}
}
How do I convert this Case Switch to a proper if else my case switch goes as it follows belowHow do I convert this Case Switch to a proper if else my case switch goes as it follows belowHow do I convert this Case Switch to a proper if else my case switch goes as it follows belowHow do I convert this Case Switch to a proper if else my case switch goes as it follows below
Aucun commentaire:
Enregistrer un commentaire