0
import java.util.Scanner;
import java.util.InputMismatchException;
public class Demo
{
    public static void main(String [] agrs){
    Scanner keyBoard = new Scanner(System.in);
    int input;
    do{
        System.out.println("[ 1] Case 1.");
        System.out.println("[ 2] Case 2.");
        System.out.println("[ 3] Case 3.");
        System.out.println("[ 0] Case 0.");
        System.out.print("Your Choice: ");

        try{
            input = keyBoard.nextInt();
        }
        catch(InputMismatchException e){
            System.out.println("Error");
        }

        switch (input){
            default:
                  System.out.println("Default");
                  break;
            case 1:
                  System.out.println("One");
                  keyBoard.next();
                  break;
            case 2:
                  System.out.println("Two");
                  break;
            case 3:
                  System.out.println("Three");
                  break;
            case 0:
                  System.exit(0);
                  break;
        }
    }
    while(input != 0);
}

我想在控制台中创建一个菜单,但出错了。错误:变量输入可能尚未初始化。我知道为什么我会出错,但我不知道如何解决它。我只懂一点英语,所以我希望 mod 编辑我的主题以适应更多谢谢大家

4

6 回答 6

2

This should fix your problems with endless loops and invalid inputs, and your compiler error:

import java.util.Scanner;
import java.util.InputMismatchException;

public class Demo {

    public static void main(String[] agrs) {

        Scanner keyBoard = new Scanner(System.in);
        // This fixes the compiler error!
        int input = -1;

        do {
            System.out.println("[ 1] Case 1.");
            System.out.println("[ 2] Case 2.");
            System.out.println("[ 3] Case 3.");
            System.out.println("[ 0] Case 0.");
            System.out.print("Your Choice: ");

            try {
                input = keyBoard.nextInt();
            } catch (InputMismatchException e) {
                // This fixes the endless loops on invalid inputs!
                System.out.println("Invalid input " + keyBoard.next());
                input = -1;
            }

            switch (input) {
                default:
                    System.out.println("Default");
                    break;
                case 1:
                    System.out.println("One");
                    keyBoard.next();
                    break;
                case 2:
                    System.out.println("Two");
                    break;
                case 3:
                    System.out.println("Three");
                    break;
                case 0:
                    System.exit(0);
                    break;
            }
        } while (input != 0);
    }
}
于 2013-03-24T04:51:18.233 回答
0

Initialize int input with 0 or any value.Because it get input from user before the use of variable input.

int input=0;

It will solves your problem .

于 2013-03-24T04:52:37.743 回答
0

this should fix it,

int input = 0;

于 2013-03-24T04:52:48.707 回答
0

You must initialize a variable before you used it..

int input;

You can initialize this variable with some dummy value:

int input = -1;
于 2013-03-24T04:53:03.340 回答
0

在实际分配输入之前,可能会从“keyBoard.nextInt()”抛出异常。如果是这样的话,那么想想下面的 switch 语句会发生什么……或者更糟的是,你是如何退出 do-while 循环的。

于 2013-03-24T04:48:56.040 回答
0

您需要将输入变量初始化为某个默认值。它是一个局部变量,默认情况下不初始化局部变量。因此,您需要为它们显式分配一些默认值。

看一下这个 :

        int input;
    do{
      // Some statements
        try {
            input = keyBoard.nextInt();
        } catch(InputMismatchException e) {
            System.out.println("Error");
        }
        // What if the above try-catch block is bypassed. In that case input will not have any value assigned to it. 
        // Hence, you get the error
        switch (input){

因此,将第一条语句更改如下:

int input = -1;
于 2013-03-24T05:03:53.563 回答