【问题标题】:Cannot find symbol in return statement and average issue在返回语句和平均问题中找不到符号
【发布时间】:2015-10-12 03:16:26
【问题描述】:

我在这段代码中遇到了两个问题,还有一个有损转换错误 对于doubleint。不知道如何处理。 (代码直接在以下行下方) 当我输入带小数的数字时,我的平均值或平均值查找器总是给我错误的答案。任何帮助将不胜感激。

public static double findMaxNumber(double maxNumber, double [] profit) {


        double theLowestValue = profit[maxNumber];

import java.util.Arrays;
import java.util.Scanner;

class Business
{
    public static void main(String[] args) {
        Scanner inputScanner;
        inputScanner = new Scanner (System.in);
        System.out.println("Welcome to the profit-calculation program.");
        System.out.println("how many days of data do you have?");
        int n = Integer.parseInt (inputScanner.nextLine());

    //call upon a function to store the profits into an array for further use.
        double[] dayProfitList = inputProfit(n);
    //call upon a function to calculate average profit 

        double averageValue = calcAverageProfit(dayProfitList);

        System.out.println("the average of these values is " +calcAverageProfit(dayProfitList) + "."); //print out devised average

    //call upon a function to calculate standard devation
        double standardDeviation = calcStandardDeviation(averageValue, dayProfitList);
        System.out.println("the standard deviation is plus or minus " +calcStandardDeviation(averageValue, dayProfitList));

    //find the most profitable day
        double theMax = findMax(dayProfitList);
        System.out.println("the most profitable day was " + findMax(dayProfitList));

        findMaxNumber(theMax, dayProfitList);
        System.out.println("and the value earned that day was " + findMaxNumber(theMax, dayProfitList));


        findLeast(dayProfitList);
        System.out.println("the least profitiable day was " + findLeast(dayProfitList)); 


    }
//function to store each days profits within an array
    public static double[] inputProfit(int days) {
    Scanner inputScanner;
    inputScanner = new Scanner (System.in);

    System.out.println("input the profit on..");
    double[] profits = new double [days+1];

    for(int i = 1; i<days + 1; i++) {
        System.out.println("day " + i + "?");
        double storedDays = Double.parseDouble (inputScanner.nextLine());
        profits[i] = storedDays;
    }
    return profits;
}
//fuction to calculate the profit of said days.
public static double calcAverageProfit(double [] profit){
    double sum = 0;
    double average = 0;
    for(int i = 1; i<profit.length; i++){
        sum = profit[i] + sum;
    }
    average = sum/profit.length;
    return average;

}

public static double calcStandardDeviation(double average, double[] profit){
    double stepThree = 0;
    double sum = 0;
    double product = 0;
    for(int i = 1;i<profit.length; i++) {
        sum = profit[i] - average;
        product = sum * sum;
        stepThree = product + stepThree; 
    }
    double standardDeviation =  stepThree/profit.length;
    java.lang.Math.sqrt(standardDeviation);
    return standardDeviation;
}

public static double findLeast(double [] profit){

    double least = profit[0];
    for (int i = 1; i<profit.length; i++) {
        if (profit[i]>least)       
            least = profit[i]; }
    return least;
}

public static double findMaxNumber(double maxNumber, double [] profit) {

    int MaxNumberInt = (int) maxNumber;
    double theLowestValue = profit[maxNumber];
    return theLowestValue;  }

【问题讨论】:

  • 检查代码的前两行,它会在类内
  • 在给定代码中找不到 findMax 方法。

标签: java indexing average


【解决方案1】:

要修复 double 到 int 的转换错误,您必须更改您的代码,如下所示。

来自

double theLowestValue = profit[maxNumber];

double theLowestValue = profit[MaxNumberInt];

修改后应该如下图。

public static double findMaxNumber(double maxNumber, double[] profit) {

    int MaxNumberInt = (int) maxNumber;
    double theLowestValue = profit[MaxNumberInt];
    return theLowestValue;
}

【讨论】:

  • 它说找不到变量
  • 我可以看到您在代码中调用了 findMax 方法,但找不到该方法的实现。它在哪里?
  • 其实就是我的变量名。我不认为它与平均水平有很大关系
  • 这是一种方法。看到这条线。 double theMax = findMax(dayProfitList);
【解决方案2】:

在您的平均/平均值查找器中从第 0 个索引迭代利润 [] 数组。

public static double calcAverageProfit(double [] profit) {
  ...
  for (int i = 0; i < profit.length; i++) {
    ...
  }
}

【讨论】:

    猜你喜欢
    • 2015-08-29
    • 1970-01-01
    • 2023-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多