查询char / int的相关输入

时间:2016-01-17 09:05:34

标签: java

当我在这个程序中将char更改为int时,我无法理解为什么编译器只显示正在运行(这是永远的)。通过改变我的意思是使用int来获取选项号,因此在switch中使用int数字。

这是正常的char 1正在运作 -

public static void main(String args[])
    throws java.io.IOException{
    char option; int i=0;

    do{
        if(i==1)
            System.out.println("\nNotice: Wrong option chosen, pick again.");
        i=1;
        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");
        System.out.println("Pick any option for brief informatrion.");
        option= (char)System.in.read();
    }while(option<'1' || option>'5');

    switch(option){
        case '1':
            System.out.println("The If:\n");
            System.out.println("If(condition) statement;");
            System.out.println("else statement;");
            break;
        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(init; condition; iteration){");
            System.out.println("    statement;");
            System.out.println("}");
            break;
    }
}

}

这是int 1,只是继续运行

public static void main(String args[])
    throws java.io.IOException{
    int option; int i=0;

    do{
        if(i==1)
            System.out.println("\nNotice: Wrong option chosen, pick again.");
        i=1;
        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");
        System.out.println("Pick any option for brief informatrion.");
        option= System.in.read();
    }while(option<1 || option>5);

    switch(option){
        case 1:
            System.out.println("The If:\n");
            System.out.println("If(condition) statement;");
            System.out.println("else statement;");
            break;
        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(init; condition; iteration){");
            System.out.println("    statement;");
            System.out.println("}");
            break;
    }
}

}

1 个答案:

答案 0 :(得分:0)

请参阅https://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html#read%28%29

System.in.read返回int代表角色。

我们假设您输入1,它将显示为49

如果您在stdin达到EOF时使用-1,则会返回char

即使是@Override public void onBindViewHolder(ViewHolder viewHolder, int position) { Model model = getModelObject(position); ((Model) viewHolder.itemView).showItem(position); } 版本,也会发生同样的错误。

请参阅此演示:https://ideone.com/Q83qxn