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.......一连串的错誤
請各位大侠帮忙,小弟是新手...謝过了
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.......一连串的错誤
請各位大侠帮忙,小弟是新手...謝过了
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
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
爲啥输出這样結果請问,求问改正的。谢謝
爲啥输出這样結果請问,求问改正的。谢謝
BufferedReader buff = new BufferredReader(new InputStreamReader(System.in));
String readString = buff.readLine();
来接收你输入的数据.在把readstring转换成int就成了.
System.out.println("you pass the test!"+i);break;
System.out.println("You not pass!"+i);break;
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时,看看输出的内容,你就会明白为什么没有得到你想得到的结果了
从来都是a=Integer.parseInt(JOptionPane.showInputDialog(""));
不过要先import javax.swing.*;
这个不会出问题哦
Exception in thread "main" java.lang.NoClassDefFoundError:Text
各位高手快说说程序那里错了!!!!!
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");
}
}}
楼主可以用下我的方法试一下