简体   繁体   English

用Java返回/传递变量

[英]Returning/Passing Variables in Java

I am a beginning programmer and we were assigned to implement methods into a code. 我是一个初级程序员,我们被分配将方法实现为代码。 I had this grade average code working fine, but once I broke it up into methods, I could not get the return functions to work. 我有这个平均年级的代码可以正常工作,但是一旦将其分解为方法,就无法使返回函数正常工作。 I have tried moving brackets and rearranging the code but to no avail. 我试过移动括号并重新排列代码,但无济于事。 I believe it may have to do with the scope of my variables... Thanks in advance :) 我相信这可能与我的变量范围有关……在此先感谢:)

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+".");}

}

Beginning with the line -public static double getAverage(numOfgrades)-, I continuously get "cannot find symbol" error message. 从行-public static double getAverage(numOfgrades)-开始,我不断收到“找不到符号”错误消息。 None of my variables is being recognized. 我的变量都没有被识别。

You need to declare the type of the argument of getAverage . 您需要声明getAverage的参数类型。 Like 喜欢

public static double getAverage(double numOfgrades)

Similiarly for your other methods (not modules). 与您的其他方法类似(不是模块)。 Have a read of this or this for tips. 有读这个这个小费。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM