A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

1.键盘输入10个数,放到数组中
        a.        去除该数组中大于10的数
        b.        将该数组中的数字写入到本地文件number.txt中
  1. public class Test1 {

  2.         static int[] arr = new int[10];
  3.         public static void main(String[] args) throws IOException {
  4.                 saveNumber();
  5.                 writeTxt();
  6.         }
  7.         private static void writeTxt() throws IOException {
  8.                 BufferedWriter bufw = new BufferedWriter(new FileWriter("number.txt"));
  9.                 for (int i = 0; i < 10; i++) {
  10.                         if(arr[i]<10){
  11.                                 bufw.write(arr[i]);
  12.                                 bufw.flush();
  13.                         }
  14.                 }
  15.         }

  16.         public static void saveNumber() {
  17.                 Scanner sc = new Scanner(System.in);
  18.                 for (int i = 0; i < arr.length; i++) {
  19.                         System.out.println("请您输入第"+(i+1)+"个数字");
  20.                         arr[i] = sc.nextInt();
  21.                 }
  22.                 System.out.println("输入完毕!");
  23.         }
  24. }
复制代码

运行结果txt文件上是乱码

5 个回复

正序浏览
同意楼上的!
回复 使用道具 举报
你写的应该不是乱码,而是arr[i]的地址值,bufw.write(arr[i]),你这么写入的应该是arr[i]的地址值,如果我猜的没错的话
回复 使用道具 举报
编码问题吧 右击java文件 还有右击TXT文件 点击properties  选择UTF-8 或者相同的格式   运行试试
回复 使用道具 举报
大神666.能打点注释吗? 有些地方不太明白。
回复 使用道具 举报
沙发,我也不会,知道我告诉我
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马