北邮java oj

该文章描述了一个Java程序,其任务是根据用户输入的整数读取并显示文本文件dict.dic中相应循环次数的数据。程序使用BufferedReader从文件中读取内容,包括整数、布尔值、双精度浮点数和字符串。

问题 A: 文本文件读取

[命题人 : 外部导入]

时间限制 : 1.000 sec  内存限制 : 128 MB

提交问题列表解决: 557提交量: 1546统计

题目描述

已知一个文本文件,文件名为dict.dic。它是由下边的代码生成。


    int a ;
    boolean b = true ;
    double c , f ;
    String d ;
    PrintWriter out = new PrintWriter("dict.dic");
    Scanner cin = new Scanner(System.in);

    for (int i = 0 ; i < 5 ; i ++ ) {
         a = cin.nextInt() ;
         f = Math.random() ;
         if ( f > 0.5 ) b = true ;
         else b = false ;
         c = cin.nextDouble() ;
         d = cin.next() ;
         out.println(a) ;
         out.println(b) ;
         out.println(c) ;
         out.println(d) ;
    }

    out.close(); // Remember this!
    cin.close();
  }




你的任务是写一段程序,将这些内容读出来。

输入

为一个整数,只可能是1,2,3,4,5之一。

输出

将dict.dic中的相关内容输出到屏幕上,如果输入是1,则输出那段代码第一次循环写入的内容,如果输入是2,则输出那段代码第二次循环写入的内容,依此类推。
形式如样例。double类型的不是保留一位小数,直接输出就可以了。

样例输入 Copy

1

样例输出 Copy

100
false
72.5
helloworld

import java.io.BufferedReader;

import java.io.FileReader;

import java.io.IOException;

import java.util.Scanner;

public class textread {

    public static void main(String[] args) {

        Scanner sc =new Scanner(System.in);

        int input = sc.nextInt();

        int i=0;

        int a=0;

        boolean b=true;

        double c=0.0;

        String d=" ";

        try (BufferedReader reader = new BufferedReader(new FileReader("dict.dic"))) {

              while(i!=input) {

                a = Integer.parseInt(reader.readLine());

                b = Boolean.parseBoolean(reader.readLine());

                c = Double.parseDouble(reader.readLine());

                d = reader.readLine();

                i++;

            }

        } catch (IOException e) {

            e.printStackTrace();

        }

       

        System.out.println(a);

        System.out.println(b);

        System.out.println(c);

        System.out.println(d);

    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值