System.in
byte[] buf = new byte[1024];
byte chr = 0;
int count = 0;
while(chr != '\n'){
chr = (byte)in.read();
buf[count++] = chr;
}
String value = new String(buf);
int intValue = Integer.parseOf(value);
byte[] buf = new byte[1024];
byte chr = 0;
int count = 0;
while(chr != '\n'){
chr = (byte)in.read();
buf[count++] = chr;
}
String value = new String(buf);
int intValue = Integer.parseOf(value);
import java.io.*;
public class triangleclass{
public static void main(String[] args){
int sideA;
System.out.print("请输入sideA的长度:");
try{
sideA=(int)System.in.read();
System.out.print("第一条边的长度:"+sideA);
}catch(IOException e){};
}
}
结果确为:
请输入sideA的长度:5
第一条边的长度:53
char c = (char) sideA;
System.out.print("第一条边的长度:" + c);
这样取得是ascii码,所以是53,所以先当字符处理比如char什么的
这样得到的是ascii码
public class SystemIn{
public static void main(String args[]){
try{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String msg = in.readLine();
/* 可以从命令行中得到数值,放到变量中*/
int value = Integer.parseInt(msg);
in.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
BufferedReader in = new BufferedReader(new InputStreamReader(System.in)
这句话是关键,可我不明白的是BufferedReader和InputStreamReader都是character流中reader类族的,是同级关系,可为什么是这样调用呢,能不能就这句话做一个解释,谢谢。
InputStreamReader 是个转换类,把字节流转换为字符流。
BufferedReader 是字符流。
public class test{
public static void main(String args[]){
for(int i=0;i<3;i++){
try{
System.out.print("请输入"+i+"个数值:");
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
String msg=in.readLine();
/* 可以从命令行中得到数值,放到变量中*/
int value = Integer.parseInt(msg);
System.out.print("第"+i+"条边的长度:"+value);
}catch(IOException e){e.printStackTrace();}
}
}
}
这样得出的只是三个值,并不能带入到变量中,于是import java.io.*;
public class test{
public static void main(String args[]){
for(int i=0;i<3;i++){
try{
System.out.print("请输入"+i+"个数值:");
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
String msg=in.readLine();
/* 可以从命令行中得到数值,放到变量中*/
int value[i]= Integer.parseInt(msg);*/这句话不明白是什么意思?
System.out.print("第"+i+"条边的长度:"+value[i]);
}catch(IOException e){e.printStackTrace();}
}
}
}想得到键盘读入3个值,分别存入value0、value1、value2
可是程序不能通过编译,请指点