如何检查超出和低于某些预定限制的值

时间:2019-04-03 19:15:48

标签: java arrays list loops printf

首先,我想感谢这个社区,尽管我来这里已经很久了,但我非常感谢。

我正在做一个学校项目,我认为除了最后一点,我大部分时间都明白了。当然,您可以随意提出任何建议,但以下是我的主要问题。

我很难将其打印出来: 我应该使用布尔值之类的循环来测试发布值是否正确吗?

投资绩效指标数量: 一种。数量:价值$ 1250或更高 b。 B的数目:值$ 1100或更高且少于$ 1500 C。 C的数量:值$ 900或更高且少于$ 1100 d。 D的数量:值750美元或更高且低于900美元 e。 F的数量:价值少于$ 750

最后,再次感谢大家的耐心和谅解。我真的很想了解这一切的工作原理,因此,如果您能解释一下它的工作原理,我将非常感激。

程序需要从文本文件中获取最小值,最大值和均值。只是需要帮助的一点信息。(我已经完成了)

到目前为止,我已经尝试过使用循环来测试值,但不确定是否正确,但是对我而言无效。

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Scanner;
public class Prog07_InvestmentManager {

    public static void main(String[] args) {

    Scanner in = new Scanner(System.in);    
    Scanner inFile=null;;
    File file=null;
    boolean flag=true;
    while(flag) {
        try {
            flag=false;
            System.out.println("Please enter the file name to import, including file extension."); //prompt user
            file = new File(in.nextLine()); //store user input in variable
            inFile = new Scanner(new FileReader(file)); //read file
        } 
        catch (FileNotFoundException e1) {
            flag=true;
        }
    }

    double min = Integer.MAX_VALUE;
    double max = 0;
    double mean = 0;
    inFile.nextLine();

    double line = 0;
    int sum = 0;
    int count = 0;

    while (inFile.hasNextLine()) {

        line=inFile.nextDouble();
        sum+=line;
        count++;

        if(line>max)
            max=line;

        if(line<min)
            min=line;
    }
    mean = (sum/count);
    System.out.printf("Max: %-4.2f\n", max);
    System.out.printf("Min: %-4.2f\n", min);
    System.out.printf("Mean: %-4.2f\n", mean);
    System.out.println();

    if (in.hasNextDouble()) {

    double[] values = new double [(int) in.nextDouble()];

    }

    try {
        Scanner inputFile = new Scanner(file);

        double[] arr = new double[(int) in.nextDouble()];

        for (int i = 0; in.hasNextDouble(); i++) {
            arr[i] = in.nextDouble();
        }

    } catch (FileNotFoundException e) {
        file = new File("investments.txt");
        System.out.print("File not found.\nPlease enter the correct path if needed.");
        file = new File(in.nextLine());                 
        }
    in.close();
    }
}

1 个答案:

答案 0 :(得分:1)

如果您只是想打印每个类别(A,B,...)的频率,我可能会使用Java的HashMap创建一个频率图。您可以为每个类别分配键,然后为每次出现值的每个键分配值。 Java 8+可以让您大开眼界

您可能会执行以下操作:

String key;
HashMap<String, Integer> freqMap = new HashMap<>();

while(inFile.hasNextLine()) {
    if(inFile.hasNextDouble()) {
        Double nextDouble = inFile.nextDouble();
        if(nextDouble >= 1250.0) {
            key = A;
            //either create a new key in the HashMap for A, B, etc. or increment the  
            //value associated with the existing key by one
            freqMap.merge(key, 1, Integer::sum);  
        }
        //we are going to use separate if statements as it appears that values can be in 
        //more than one category
        if(nextDouble < 1500.0 && nextDouble >= 1100.0) {
            key = B;
            freqMap.merge(key, 1, Integer::sum);  
        }
        and so on...
    }
}

最后,您将遍历HashMap并打印键和值。让我知道您是否需要更多细节。

编辑:此方法不需要数组结构。但是,如果要遍历数组以构建映射而不是从文件中接收数据,请将while循环更改为遍历数组的for循环。