class Test{
   public static void main(String args[]){
             int a=90,i;
             if(a>60) i=1;
             else  i=2;
             switch(i)
               {
              case 1:System.out.println("you pass the test!");
              case 2:System.out.println("You not pass!");
                }
}
看上去沒有语法错误,爲啥不能输出對应的you pass the test!
而是java.lang.......一连串的错誤
請各位大侠帮忙,小弟是新手...謝过了

解决方案 »

  1.   

    class Test{
       public static void main(String args[]){
                 int a=90,i;
                 if(a>60) i=1;
                  else  i=2;
                 switch(i)
                   {
                  case 1:System.out.println("you pass the test!"+i);break;
                  case 2:System.out.println("You not pass!"+i);break;
                    }
    }
    }
    改了一点东西,你本来的程序怎么连}也少了啊,我能输出,可不知道为什么它把1和2的情况都输出,所以加了个break
      

  2.   

    import java.io.*;
    public class Test { /**
     * @param args
     */
    public static void main(String[] args) throws IOException
    {
    // TODO Auto-generated method stub
     int i;
     int a;
     a=(int)System.in.read();
             if(a>60) i=1;
              else  i=2;
             switch(i)
               {
              case 1:
               System.out.println("you pass the test!"+i);break;
              case 2:
               System.out.println("You not pass!"+i);break;
               default:System.out.println("error");
               }
    }}
    输入
    80
    You not pass!2
    爲啥输出這样結果請问,求问改正的。谢謝
      

  3.   

    因为a=80 > 60,所以 i=2,所以执行System.out.println("You not pass!"+i);break;
      

  4.   

    输多少都You not pass!2
    爲啥输出這样結果請问,求问改正的。谢謝
      

  5.   

    你用
    BufferedReader buff = new BufferredReader(new InputStreamReader(System.in));
    String readString = buff.readLine();
    来接收你输入的数据.在把readstring转换成int就成了.
      

  6.   

    按wylsx(小小de鸟) 說的可以,如过非按我的方法做該怎办
      

  7.   

    a=(int)System.in.read();读入一个字节,unicode码,当输入“80”是,第一个字节是“8”,码是56,小于60,输入大写字母A,码是65,大于60,情况就不一样了。把下面两行里的 i 改成 a ,就看明白了。
    System.out.println("you pass the test!"+i);break;
    System.out.println("You not pass!"+i);break;
      

  8.   

    a=(int)System.in.read();
    System.in.read()这条语句默认读出的数据是char的,然后就转换成int数据,所以你的(int)转型不用都可以,写成:a=System.in.read(); 结果还是一样的。
    因为默认接收到的是char型的,然后才转换成int型,所以你应该要知道char和int的对应关系
    char '0' 对应 int 48 
    char '1' 对应 int 49
    ……如此类推
    char '9' 对应 int 57char 'a' 对应 int 97
    char 'b' 对应 int 98
    …………
    你自己查查资料。你还要了解的是System.in.read();每次只读取一个char。就是说如果你输入102,你第一次使用System.in.read();读到的char 1 ,然后转换成int 就是49 ,如果你再使用System.in.read();读取,就会读到 char 0,结果转换成int 就是 48,再使用System.in.read();读取就是 char 2 ,转换成 int 50。如果你输入a,就会得到int 97。因此,你无论输入什么数字,因为都是只读了第一个数字,所以都会比58小。但是如果你输入a,就会比60打,因为其实得到的是int 97。部分修改:
    int i;
     int a;
     a=System.in.read();
     i=System.in.read();
     System.out.println(a);
     System.out.println(i);
             if(a>60) i=1;
              else  i=2;
             switch(i)
               {
              case 1:
               System.out.println("you pass the test!"+i);break;
              case 2:
               System.out.println("You not pass!"+i);break;
               default:System.out.println("error");
               }你看看,当你输入1a时,看看输出的内容,你就会明白为什么没有得到你想得到的结果了
      

  9.   

    真长知识,我从来还没用过System.in.read();方法呢
    从来都是a=Integer.parseInt(JOptionPane.showInputDialog(""));
    不过要先import javax.swing.*;
    这个不会出问题哦
      

  10.   

    我运行了楼主的程序,也出现了以下错误:
    Exception in thread "main" java.lang.NoClassDefFoundError:Text
    各位高手快说说程序那里错了!!!!!
      

  11.   

    fank回复得太详细了,学到了不少
      

  12.   

    import java.io.*;
    public class Testswitch1 {
    public static void main(String[] args) throws java.io.IOException
    {
     int i;
     int a;
     String a1;
     DataInputStream in=new  DataInputStream(new BufferedInputStream(System.in));
     System.out.print("input(a): ");
     a1=in.readLine();
     a=new Integer(a1).intValue();
     //a=(int)System.in.read();
     System.out.println("a= "+a);
             if(a>60) i=1;
             else  i=2;
             switch(i)
               {
              case 1:
              System.out.println("you pass the test!"+i);break;
              case 2:
              System.out.println("You not pass!"+i);break;
               default:System.out.println("error");
               }
    }}
    楼主可以用下我的方法试一下