一道简单java题,谢谢

来源:百度知道 编辑:UC知道 时间:2024/05/31 05:44:30
有劳看一下,不胜感激!!
import java.io.*;
public class WriteTenFile{

public static void main(String[] args) throws Exception{
byte buffer[]=new byte[128];
int i=1;
FileOutputStream f=new FileOutputStream("1.txt");
for(i=0;i<=9;i++){
int b=(int)(Math.random()*10);
buffer[i]=b;
// ileOutputStream f=new FileOutputStream("1.txt");
int n=System.in.read(buffer);
f.write(buffer,0,n);
f.write('\n');

}

f.close();
}

}

E:\java\WriteTenFile.java:11: 可能损失精度
找到: int
需要: byte
buffer[i]=b;
^
1 错误

工具以退出代码 1 完成

从int转换倒byte 需要强制类型转换 多看看关于隐式转换的那个图
例如
buffer[i]= (byte)b;
还有你后面的语句
f.write(buffer,0,buffer.length);
或者直接f.write(buffer);
如果你用System.in.read()方法的话 那么buffer中存放的只是从控制台读到的字符了

study