variable might not have been initialized in java

℡╲_俬逩灬. 提交于 2019-12-12 05:28:50

问题


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

I want to make a menu in the console but taked a error. Error: variable input might not have been initialized. I know why i take the error but I don't know how to fix it. I just know a little English so I expect mod edit my topic to fit more Thank everybody


回答1:


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



回答2:


Its possible that an exception may be thrown from 'keyBoard.nextInt()' before input is actually assigned. If thats the case then think about what happens in the switch statement below... or even worse, how do you ever exit from the do-while loop.




回答3:


this should fix it,

int input = 0;




回答4:


You must initialize a variable before you used it..

int input;

You can initialize this variable with some dummy value:

int input = -1;



回答5:


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 .




回答6:


You need to initialize the input variable to some default value. It is a local variable, and local variables are not initialized by default. So you need to explicitly assign some default value to them.

Check this out :

        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){

So change the first statement as follows :

int input = -1;



回答7:


I think you can just include the while and switch statements into the try block



来源:https://stackoverflow.com/questions/15595259/variable-might-not-have-been-initialized-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!