java键盘输入的问题
答案:2 悬赏:0
解决时间 2021-02-08 09:13
- 提问者网友:孤笛钟情你
- 2021-02-07 13:42
java键盘输入的问题
最佳答案
- 二级知识专家网友:高冷不撩人
- 2021-02-07 14:18
第一段,你只有read一次过,第二次,你readLine是等着的,会readLine多次 int ch = is.read(); while(ch!=-1) 和while((line=bufr.readLine())!=null) 你自己看看什么区别
全部回答
- 1楼网友:废途浑身病态
- 2021-02-07 15:50
文件的读写操作是会抛出 ioexception 异常的
所以在做文件读写操作的时候应该用try...catch...语句包含起来.或者在方法最后添加 throws ioexception
//////////////////////////////////
import java.io.*;
public class ex_data {
string y;
int year;
string m;
int month;
string d;
int day;
ex_data() throws ioexception { //注意这里,我添加了throws ioexception
inputstreamreader reader = new inputstreamreader(system.in);
bufferedreader input = new bufferedreader(reader);
system.out.print("enter the year:");
y = input.readline();
year = new integer(y).intvalue();
system.out.print("enter the month:");
m = input.readline();
month = new integer(m).intvalue();
system.out.print("enter the day:");
d = input.readline();
day = new integer(d).intvalue();
}
void show() {
system.out.println(year + "/" + month + "/" + day);
system.out.println(month + "-" + day + "/" + year);
system.out.println(day + "/" + month + "/" + year);
}
public static void main(string args[]){
ex_data obj;
//这里我用try...catch子句把new ex_data包含了进来.
try {
obj = new ex_data();
obj.show();
} catch (ioexception e) {
e.printstacktrace();
}
}
}
//////////////////////////////////////////////
更改你的程序后,正常编译!
或者你在 ex_data()里添加try...catch...子句.在main方法里就不用try...catch...了,如下:
////////////////////////////////////////
import java.io.*;
public class ex_data {
string y;
int year;
string m;
int month;
string d;
int day;
ex_data() {
inputstreamreader reader = new inputstreamreader(system.in);
bufferedreader input = new bufferedreader(reader);
// 为避免重复cry...catch...我把所有的语句放到了一个cry...catch...子句中
try {
system.out.print("enter the year:");
y = input.readline();
year = new integer(y).intvalue();
system.out.print("enter the month:");
m = input.readline();
month = new integer(m).intvalue();
system.out.print("enter the day:");
d = input.readline();
day = new integer(d).intvalue();
} catch (ioexception e) {
e.printstacktrace();
}
}
void show() {
system.out.println(year + "/" + month + "/" + day);
system.out.println(month + "-" + day + "/" + year);
system.out.println(day + "/" + month + "/" + year);
}
public static void main(string args[]) {
ex_data obj;
obj = new ex_data();
obj.show();
}
}
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯