这个JAVA编程错在哪里了高手帮忙看一哈

来源:百度知道 编辑:UC知道 时间:2024/06/07 10:44:37
public class If{
public static void main(String[] args) {
char c=0;
System.out.print("Enter a character: ")
try{
c=(char)System.in.read();
}catch(IOException e){};
if(c='l')
System.out.println("left");
else
System.out.println("not konw");
if(c='r')
System.out.println("right");
else
System.out.println("not konw");
}
}
就是输入L显示的是left输入R显示的是right不是这2个的话就显示NOT KONW 先这里谢谢大家拉 分不多了不好意思

首先,从语法上讲,你的错误在于,程序的头部要导入IO的Exception包。如下:
import java.io.IOException;
其次,if(c='l') 这句中,单个等号在JAVA语言里面是赋值语句,不是判断语句,判断语句是两个的等号因此需要修改为:
if(c=='l') 和if(c=='r')

然后就可以运行了。但是运行是可以运行的,却觉的你的思路有错误,其实你可以考虑使用switch来做。附完整的程序块如下:

import java.io.IOException;

public class If{
public static void main(String[] args) {
char c=0;
System.out.print("Enter a character: ");
try{
c=(char)System.in.read();
}catch(IOException e){};
switch(c){
case 'l':System.out.print("left");break;
case 'f':System.out.print("right");break;
default:System.out.print("Not Know");
}
}
}