我收到了 - 虽然导入IO但无法解决java.io.IOException错误

时间:2014-06-16 00:24:05

标签: java

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

当我在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) 

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

2 个答案:

答案 0 :(得分:1)

您在这里提供的代码非常有效。

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

答案 1 :(得分:1)

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