为什么';t char=(char)System.in.read();用java工作?
这是我的密码为什么';t char=(char)System.in.read();用java工作?,java,exception,object,Java,Exception,Object,这是我的密码 package test.program; /** * * @author Justin */ public class TestProgram { /** * @param args the command line arguments */ public static void main(String[] args) { char ch; ch = (char)System.in.read()
package test.program;
/**
*
* @author Justin
*/
public class TestProgram
{
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
char ch;
ch = (char)System.in.read();
if(ch<'K')
System.out.println("This stuff is less than K " + ch);
else
System.out.println("Thats that stuff I don't like " + ch);
}
}
package test.program;
/**
*
*@作者贾斯汀
*/
公共类测试程序
{
/**
*@param指定命令行参数
*/
公共静态void main(字符串[]args)
{
char ch;
ch=(char)System.in.read();
如果(chSystem.in
是一个输入流
,和
抛出:
IOException-如果发生I/O错误
它是一个已检查的异常,因此您必须捕获它,或者让调用它的方法声明它抛出IOException
此部分工作ch=(char)System.in.read();
。您的问题在别处。
这是因为main
方法没有声明抛出IOException
试试这个:
publicstaticvoidmain(String[]args)抛出java.io.IOException{
在java中处理io时,大多数时候都必须处理IOException。在这种情况下,它可能发生在读取输入时
试着这样做:
public static void main(String[] args)
{
char ch;
try{
ch = (char)System.in.read();
}
catch(IOException e){
e.printStackTrace();
}
if(ch<'K')
System.out.println("This shit is less than K " + ch);
else
System.out.println("Thats that shit I don't like " + ch);
}
publicstaticvoidmain(字符串[]args)
{
char ch;
试一试{
ch=(char)System.in.read();
}
捕获(IOE异常){
e、 printStackTrace();
}
如果(ch-来自API,InputStream的read()方法抛出IOException,因此您需要捕获它或重新播放它,在示例中是否可以使用较少的过滤器触发文本