本文介绍了变量可能尚未在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);
}

我想在控制台中创建菜单,但出现错误.错误:变量输入可能尚未初始化.我知道为什么要接受错误,但不知道如何解决.我只会一点英语,所以我希望mod编辑我的主题以使其更适合谢谢大家

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

这篇关于变量可能尚未在Java中初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-09 23:04