Hello, I have this calculator program that compiles and runs just fine, but my loop will not break as intendend too, when I hit 5 it it still runs the options to enter a number, here is my code:
import java.util.Scanner;
public class calculator
{
public static void main(String[] args)
{
int i = 0;
do
{
Scanner s = new Scanner(System.in);
System.out.println("-Easy Calculator-");
System.out.println("1) Add");
System.out.println("2) Subtract");
System.out.println("3) Multiply");
System.out.println("4) Divide");
System.out.println("5) Exit");
System.out.println(" ");
System.out.println("Enter Option: ");
i = s.nextInt();
System.out.println("First Number: ");
int a = s.nextInt();
System.out.println(" ");
System.out.println("Your first number number is " +a);
System.out.println(" ");
System.out.println("Second Number: ");
int b = s.nextInt();
System.out.println(" ");
System.out.println("Your second number number is " +b);
System.out.println(" ");
double result = 0;
switch (i)
{
case 1:
result = a + b;
System.out.println("Answer = " +result);
System.out.println(" ");
break;
case 2:
result = a - b;
System.out.println("Answer = " +result);
System.out.println(" ");
break;
case 3:
result = a * b;
System.out.println("Answer = " +result);
System.out.println(" ");
break;
case 4:
if (b == 0)
{
System.out.println("Cannot Divide By Zero");
System.out.println(" ");
break;
}
else
result = a / b;
System.out.println("Answer = " +result);
System.out.println(" ");
break;
case 5:
System.out.println("Thank you for using easy calculator. Goodbye");
break;
default:
System.out.println("You have entered a wrong choice");
System.out.println(" ");
break;
}
}while(i != 5);
}
}
I do not know exactly what I need to do, i tried changing my do while statements, but nothing