简体   繁体   中英

variable might not have been initialized in java

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

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

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.

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 .

这应该解决它,

int input = 0;

You must initialize a variable before you used it..

int input;

You can initialize this variable with some dummy value:

int input = -1;

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;

我想你可以将while和switch语句包含在try块中

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM