gpt4 book ai didi

java - Java 中返回/传递变量

转载 作者:行者123 更新时间:2023-12-02 11:55:24 24 4
gpt4 key购买 nike

我是一名初级程序员,我们被分配将方法实现为代码。我的这个年级平均代码工作得很好,但是一旦我把它分解成方法,我就无法让返回函数工作。我尝试过移动括号并重新排列代码,但无济于事。我相信这可能与我的变量范围有关...提前致谢:)

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)- 行开始,我不断收到“找不到符号”错误消息。我的变量都没有被识别。

最佳答案

您需要声明getAverage的参数类型。喜欢

public static double getAverage(double numOfgrades)

对于其他方法(不是模块)也是如此。阅读thisthis获取提示。

关于java - Java 中返回/传递变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47639505/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com