我得到 - java.io.IOException 通过导入 IO 无法解决错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24235273/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 10:59:55  来源:igfitidea点击:

I am getting - java.io.IOException cannot be resolved error though importing the IO

java

提问by user3743243

// Using a do-while to process a menu selection.

public class Menu {
    public static void main(String args[]) throws java.io.IOException {
        char choice;

        do {
            System.out.println("Help on: ");
            System.out.println("  1. if");
            System.out.println("  2. switch");
            System.out.println("  3. while");
            System.out.println("  4. do-while");
            System.out.println("  5. for\n");
            System.out.println("Choose one: ");
            choice = (char) System.in.read();
        } while (choice < '1' || choice > '5');

        System.out.println("\n");

        switch (choice) {
            case '1':
                System.out.println("The if:\n");
                System.out.println("if(condition) statement;");
                System.out.println("else statement;");
            case '2':
                System.out.println("The switch:\n");
                System.out.println("switch(expression) {");
                System.out.println(" case constant:");
                System.out.println("   statement sequence");
                System.out.println("   break;");
                System.out.println("  //...");
                System.out.println("}");
                break;
            case '3':
                System.out.println("The while:\n");
                System.out.println("while(condition) statement;");
                break;
            case '4':
                System.out.println("The do-while:\n");
                System.out.println("do {");
                System.out.println("  statement;");
                System.out.println("} while (condition);");
                break;
            case '5':
                System.out.println("The for:\n");
                System.out.println("for(initialization; condition; iteration)");
                System.out.println(" statement;");
                break;
        }
    }
}

When I run this program in Eclipse IDE I get this error

当我在 Eclipse IDE 中运行此程序时,出现此错误

Exception in thread “main” java.lang.Error: Unresolved compilation problem:
java.io.IOException cannot be resolved to a type  at Menu.main(Menu.java:3) 

I have rebuilt the project, cleaned it several times, refreshed still no luck..... :-(

我重建了项目,清理了几次,刷新仍然没有运气...... :-(

回答by King Julian

The code you provided here works perfectly.

您在此处提供的代码运行良好。

This error happens when you use Eclipse as IDE and try to run code that doesn't even compile. Check your Problems view in Eclipse, and fix the compilation errors before executing the application.

当您将 Eclipse 用作 IDE 并尝试运行甚至无法编译的代码时,就会发生此错误。在 Eclipse 中检查您的问题视图,并在执行应用程序之前修复编译错误。

回答by Kuku

the ioexcpetion is in rt.jar which should already be included in your jdk lib. Just make sure your project build path include your jdk or you can just include rt.jar in your build path

ioexcpetion 在 rt.jar 中,它应该已经包含在您的 jdk 库中。只要确保你的项目构建路径包含你的 jdk 或者你可以在你的构建路径中包含 rt.jar