我是一名初学程序员,我们被分配到代码中实现方法。我有这个等级平均代码工作正常,但一旦我把它分解成方法,我无法让返回功能工作。我试过移动括号并重新排列代码,但无济于事。我相信它可能与我的变量范围有关...提前谢谢:)
package milsteadgrades;
import java.util.Scanner;
public class MilsteadGrades {
public static void main(String[] args)
{
//Call methods to execute program.
displayInfo();
double numOfgrades = getInput();
double average = getAverage(numOfgrades);
char letgrade = determineLetterGrade(average);
displayGrades(average, letgrade);
}
public static void displayInfo()
{
System.out.println("Mallory Milstead");
System.out.println("This program will prompt the user for a number of
grades"
+ " and each grade. Then the program calculates and displays the average and
letter"+" grade.");
}
public static double getInput()
{
//Prompt user to enter number of grades and assign that number to
numOfgrades.
System.out.print("How many grades would you like to average? ");
Scanner keyboard = new Scanner(System.in);
double numOfgrades = keyboard.nextDouble();
return numOfgrades;
}
public static double getAverage(numOfgrades)
{
//Prompt the user to enter grades.
System.out.println("Enter exam scores : ");
Scanner keyboard = new Scanner(System.in);
double total = 0;
for (double i = 0; i < numOfgrades; i++) {
double grade = keyboard.nextDouble();
total+=grade;}
double average = total/numOfgrades;
return average;
}
public static char determineLetterGrade(average)
{ double testscore = average;
char letgrade;
if (testscore >= 90)
{
letgrade = 'A';
} else if (testscore >= 80)
{
letgrade = 'B';
} else if (testscore >= 70)
{
letgrade = 'C';
} else if (testscore >= 60)
{
letgrade = 'D';
} else
{
letgrade = 'F';
}
return letgrade;
}
public static void displayGrades(average, letgrade)
{
System.out.println("The average of the grades is "+average+ " and the
letter grade"+ " is " + letgrade+".");}
}
从行-public static double getAverage(numOfgrades)开始 - 我不断收到“找不到符号”错误消息。我的变量都没有被识别出来。