I tried to make a menu that uses switch
and do while
, but it returns:
variable 'menu' might not have been initialized
} while (menu != 5);
Can anybody help me please? This is my goal:
import java.util.Scanner;
class HW {
public static void main(String[] args) {
Scanner entry = new Scanner(System.in);
int A, B, X, menu; // there
do {
System.out.println("Main Menu");
System.out.println("1. Plus");
System.out.println("2. Minus");
System.out.println("3. Multiply");
System.out.println("4. Division");
System.out.println("5. Exit");
System.out.println("Option :");
X = entry.nextInt();
switch (X) {
case 1:
System.out.println("Var A : ");
A = entry.nextInt();
System.out.println("Var B : ");
B = entry.nextInt();
System.out.println("Result : " + (A + B));
if (A > B)
System.out.println(A + ">" + B);
else if (A == B)
System.out.println(A + "=" + B);
else if (A < B)
System.out.println(A + "<" + B);
System.out.println("*press any key back to menu*");
menu = entry.nextInt(); // there
break;
case 2:
System.out.println("Var A : ");
A = entry.nextInt();
System.out.println("Var B : ");
B = entry.nextInt();
System.out.println("Result : " + (A - B));
if (A > B)
System.out.println(A + ">" + B);
else if (A == B)
System.out.println(A + "=" + B);
else if (A < B)
System.out.println(A + "<" + B);
System.out.println("*press any key back to menu*");
menu = entry.nextInt(); //there
break;
case 3:
System.out.println("Var A : ");
A = entry.nextInt();
System.out.println("Var B : ");
B = entry.nextInt();
System.out.println("Result : " + (A * B));
if (A > B)
System.out.println(A + ">" + B);
else if (A == B)
System.out.println(A + "=" + B);
else if (A < B)
System.out.println(A + "<" + B);
System.out.println("*press any key back to menu*");
menu = entry.nextInt(); //there
break;
case 4:
System.out.println("Var A : ");
A = entry.nextInt();
System.out.println("Var B : ");
B = entry.nextInt();
System.out.println("Result : " + (A / B));
if (A > B)
System.out.println(A + ">" + B);
else if (A == B)
System.out.println(A + "=" + B);
else if (A < B)
System.out.println(A + "<" + B);
System.out.println("*press any key back to menu*");
menu = entry.nextInt(); //there
break;
case 5:
break;
}
} while (menu != 5); //here it say not initialized.
System.out.println(" ");
}
}