【发布时间】:2013-09-17 13:25:30
【问题描述】:
你好 stackoverflow 社区。目前我是java新手,我正在尝试弄清楚如何模块化程序。无论出于何种原因,当我返回变量 averageScore 时,它都会在 main 方法中为该变量提供一个错误。任何帮助将不胜感激。
public class Program4
{
public static void main(String[ ] args)
{
EasyReader console = new EasyReader(); //Connect to EasyReader.class
System.out.print("Enter the score from game 1: ");
double score1 = console.readDouble(); //Reads the user input for game 1
System.out.print("Enter the score from game 2: ");
double score2 = console.readDouble(); //Reads the user input for game 2
System.out.print("Enter the score from game 3: ");
double score3 = console.readDouble(); //Reads the user input for game 3
average(score1, score2, score3);
displayScores(score1, score2, score3, averageScore);
}
public static void average(double score1, double score2, double score3)
{
double averageScore = ((double)score1+score2+score3)/3; //Divides miles by gallons
getAverage();
}
public static double getAverage()
{
return new averageScore;
}
public static void displayScores(double score1, double score2, double score3, double averageScore)
{
System.out.println("Game 1 Score: " + score1 + " points"); //Prints score 1
System.out.println("Game 2 Score: " + score2 + " points"); //Prints score 2
System.out.println("Game 3 Score: " + score3 + " points"); //Prints score 3
System.out.println("Average score: " + RoundDouble.roundDouble(averageScore, 2) + " points "); //Prints the average score and rounds it to 2 decimals
}
}
【问题讨论】:
-
考虑范围并检查返回值的方法。
-
我不确定这是什么意思,你能澄清一下吗?
-
当您直接拨打
getAverage();时,您期望会发生什么? -
将堆栈跟踪的相关部分与您的问题一起包含在内是一种很好的做法
标签: java methods return symbols