我坚持创建一个循环来一次读取一个文件的6行,将每一行保存到一个变量中,以供以后进行计算

该项目是分析文本数据文件并提供其统计信息。这是一个固定格式的文件。我们使用读取的数据编写一个新的固定格式文件。我们要做的是读取名字,姓氏,年龄,当前收入,当前储蓄和加薪。有606行代码。因此,我们必须读取前6行,然后循环读取6行并存储数据。然后,我们必须按几个年龄段(例如20-29岁,30-39岁等)找到人数。找到当前收入的最小最大值和平均值。总节省的最小值,最大值和平均值。退休收入(每月)最小最大和平均。退休年龄段的平均收入(例如20-29岁,30.39等)。 现在,我已经过去2个小时浏览了我的教科书(大型Java,后期对象),并完成了过去的作业,而从哪里开始我完全不知所措。我是大学一年级的Java编程学生。我们尚未使用数组。我从哪里开始呢?我了解我需要做什么,但我不了解以任何方式读取文件。根据我在互联网上所做的研究,人们做过很多不同的事情,这是我以前从未见过的。

当前代码(请注意,我对此很迷茫)

len(nums)
yk7732637 回答:我坚持创建一个循环来一次读取一个文件的6行,将每一行保存到一个变量中,以供以后进行计算

您想一次通读此文件六行,并根据每组的内容进行计算。首先,您需要一个放置值的地方,并且一个静态内部类将很有用:

private static class EmployeeData {
    final String firstName;
    final String lastName;
    final int age;
    final BigDecimal income;
    final BigDecimal savings;
    final BigDecimal raise;

    public EmployeeData(String firstName,String lastName,int age,BigDecimal income,BigDecimal savings,BigDecimal raise) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.age = age;
        this.income = income;
        this.savings = savings;
        this.raise = raise;
    }
}

现在,您有了一个有用的数据结构,可以在其中放入扫描仪正在读取的行。 (请注意,我们正在使用BigDecimals而不是double,因为我们正在进行财务计算。That matters。)

    Scanner in = new Scanner(new File("midterm.txt"));

    while (in.hasNext()){

        String firstName = in.nextLine();
        String lastName = in.nextLine();
        int age = in.nextInt();
        BigDecimal income = in.nextBigDecimal();
        BigDecimal savings = in.nextBigDecimal();
        BigDecimal raise = in.nextBigDecimal();

        EmployeeData employeeData = new EmployeeData(firstName,lastName,age,income,savings,raise);
        BigDecimal power = calculatePower(employeeData);
    }

您现在已准备好进行计算,我已将其放入适当命名的方法中:

private static BigDecimal calculatePower(EmployeeData employeeData){
    int ageDifference = RETIREMENT_AGE - employeeData.age;
    BigDecimal base = employeeData.raise.add(BigDecimal.valueOf(1L));
    BigDecimal baseRaised = BigDecimal.valueOf(Math.pow(base.doubleValue(),ageDifference));
    return employeeData.income.multiply(baseRaised);
}

您将需要指定要使用的常量:

private static final BigDecimal INTEREST = BigDecimal.valueOf(.07);
private static final int RETIREMENT_AGE = 70;
private static final BigDecimal WITHDRAWAL_PERCENT = BigDecimal.valueOf(.04);
private static final BigDecimal SAVINGS_RATE = BigDecimal.valueOf(.15);

那你怎么看?这是否给您足够的开始?到目前为止,这是整个程序;我添加了注释以更详细地说明发生了什么:

import java.math.BigDecimal;
import java.util.*;

// The file name will have to match the class name; e.g. Scratch.java
public class Scratch {

    // These are values that are created when your class is first loaded. 
    // They will never change,and are accessible to any instance of your class.
    // BigDecimal.valueOf 'wraps' a double into a BigDecimal object.
    private static final BigDecimal INTEREST = BigDecimal.valueOf(.07);
    private static final int RETIREMENT_AGE = 70;
    private static final BigDecimal WITHDRAWAL_PERCENT = BigDecimal.valueOf(.04);
    private static final BigDecimal SAVINGS_RATE = BigDecimal.valueOf(.15);

    public static void main(String[] args) {
        // midterm.txt will have to be in the working directory; i.e.,// the directory in which you're running the program 
        // via 'java -cp . Scratch'
        Scanner in = new Scanner(new File("midterm.txt"));

        // While there are still more lines to read,keep reading!
        while (in.hasNext()){

            String firstName = in.nextLine();
            String lastName = in.nextLine();
            int age = in.nextInt();
            BigDecimal income = in.nextBigDecimal();
            BigDecimal savings = in.nextBigDecimal();
            BigDecimal raise = in.nextBigDecimal();

            // Put the data into an EmployeeData object
            EmployeeData employeeData = new EmployeeData(firstName,raise);
            // Calculate power (or whatever you want to call it)
            BigDecimal power = calculatePower(employeeData);
            System.out.println("power = " + power);
        }
    }

    private static BigDecimal calculatePower(EmployeeData employeeData){
        int ageDifference = RETIREMENT_AGE - employeeData.age;
        // With big decimals,you can't just use +,you have to use 
        // .add(anotherBigDecimal)
        BigDecimal base = employeeData.raise.add(BigDecimal.valueOf(1L));
        BigDecimal baseRaised = BigDecimal.valueOf(Math.pow(base.doubleValue(),ageDifference));
        return employeeData.income.multiply(baseRaised);
    }
    // A static inner class to hold the data
    private static class EmployeeData {

        // Because this class is never exposed to the outside world,// and because the values are final,we can skip getters and 
        // setters and just make the variable values themselves accessible 
        // directly by making them package-private (i.e.,there is no '
        // private final' or even 'public final',just 'final')
        final String firstName;
        final String lastName;
        final int age;
        final BigDecimal income;
        final BigDecimal savings;
        final BigDecimal raise;


        public EmployeeData(String firstName,BigDecimal raise) {
            this.firstName = firstName;
            this.lastName = lastName;
            this.age = age;
            this.income = income;
            this.savings = savings;
            this.raise = raise;
        }
    }
}
本文链接:https://www.f2er.com/2539784.html

大家都在问