java 输入出错了

来源:百度知道 编辑:UC知道 时间:2024/05/13 11:33:19
public class ee
{
void ee(){
char input=(char)System.in.read();

}
public static void main(String args[]){
ee v=new ee();
v.ee();
}
}
编译时候出现
ee.java:4: unreported exception java.io.IOException; must be caught or declared to be thrown
char input=(char)System.in.read();
^
1 error

try{
char input=(char)System.in.read();
}catch(IOException e){
System.out.println(e.toString());
}

需要导入io包,程序第一行加入import java.io.IOException;

原因是没有捕获io异常,需要添加try{}catch语句,您可以这样改:
public class ee
{
void ee(){
try {
char input=(char)System.in.read();
} catch (IOException e) {
}
}
public static void main(String args[]){
ee v=new ee();
v.ee();
}
}

既然是读入数据,应该有个异常捕获^-^-^-^

package com.hehe;

import java.io.IOException;

public class Ee {

/**
* @param zeffy
* @throws IOException
*/
public char hehe;
void ee() throws IOException{
hehe=(char)System.in.read();

}

public static void main(String[] args) throws IOException {
Ee v=new Ee();
v.ee();
System.out.println(v.getHehe());
}

public char getHehe() {
return hehe;