变量“菜单”可能尚未初始化

时间:2015-09-19 02:47:34

标签: java

我尝试制作一个使用switchdo while的菜单,但它会返回:

  

变量'menu'可能尚未初始化

} while (menu != 5);
是的,有人可以帮帮我吗?这是我的目标:

pic

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(" ");
  }
}

4 个答案:

答案 0 :(得分:1)

您需要在上次default之后创建case语句。这是因为如果没有一个案例属实,它仍然需要一个“备份计划”。它是else语句的反对部分。

case: 5之后添加此内容:

default:
    menu = desiredNumberHere;
    break;

答案 1 :(得分:1)

while (menu != 5);

看看,如果没有满足的情况怎么办?
这就是你需要初始化的原因。

您需要在交换机中初始化或提供default个案例。

所以,这就是为什么始终保证初始化。

答案 2 :(得分:0)

如果您的唯一目标是让错误消失,请初始化menu变量:

int A;
int B;
int X;
int menu = -1; // Initialize menu to -1

但是,您的switch语句需要确保处理所有可能的情况。您可以通过添加默认案例来确保它。

答案 3 :(得分:0)

您只声明了变量int A, B, X, menu;但没有初始化这些。因此,您需要做的是根据您的要求将任何值放到manu变量中。