【发布时间】:2015-10-12 03:16:26
【问题描述】:
我在这段代码中遇到了两个问题,还有一个有损转换错误
对于double 到int。不知道如何处理。 (代码直接在以下行下方)
当我输入带小数的数字时,我的平均值或平均值查找器总是给我错误的答案。任何帮助将不胜感激。
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 方法。