简体   繁体   中英

How to pass variables from one method to another?

Trying to write a grade calculator program and am running into problems passing return variables from one method to another. I'm new to coding so I'm sure this isn't very pretty, but was hoping to get some help. I have a method to calculate the Homework score, a method to calculate the midterm score, and a method to calculate the final score. I'm trying to call the return variables in the last method to calculate overall grade. The error is when calling the courseScore method, and displays as: The method courseScore(int, int, int) in the type GradeCalc is not applicable for the arguments (). Any help is appreciated!

import java.util.*;
public class GradeCalc {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int overAll;
        int midtermScore;
        int finalsScore;
        hwpoints();
        midTerm();
        courseScore();  

}

        public static int hwpoints() {
            int x;
            int hwTotal = 0;
            int pointsPossible = 0;
            Scanner input = new Scanner(System.in);
            System.out.println("Enter number of assignments:");
            x = input.nextInt();
                for(int i = 1; i <= x; i++) {
                    System.out.println("Enter assignment score:");
                    int hwScore = input.nextInt();
                    hwTotal += hwScore;
                    System.out.println("Enter total possible points:");
                    int points = input.nextInt();
                            pointsPossible += points;
                        }
                int overAll = (hwTotal / x);
                System.out.println("You got " + hwTotal + " out of " + pointsPossible + ". Your overall Homework grade is a: " + overAll);
                return overAll;
        }
        public static int midTerm() {
            int midtermPoints;
            int midtermPossible;
            int midtermScore;
            Scanner input = new Scanner(System.in);
            System.out.println("Enter Midterm Score:");
            midtermPoints = input.nextInt();
            System.out.println("Enter total possible Midterm Points:");
            midtermPossible = input.nextInt();
            midtermScore = (midtermPoints / midtermPossible);
            System.out.println("Your Midterm score is " + midtermScore);
            return midtermScore;

        }
        public static int finalScore() {
            int finalsPoints;
            int finalsPossible;
            int finalsScore;
            Scanner input = new Scanner(System.in);
            System.out.println("Enter Finals Score:");
            finalsPoints = input.nextInt();
            System.out.println("Enter total possible Finals Points:");
            finalsPossible = input.nextInt();
            finalsScore = (finalsPoints / finalsPossible);
            System.out.println("Your Finals score is " + finalsScore);
            return finalsScore;
    }
        public static void courseScore(int finalsScore, int midtermScore, int overAll) {
            Scanner input = new Scanner(System.in);
            System.out.println("What percent of your grade is the final?");
            int testWeight = input.nextInt();
            System.out.println("What percent of your grade is the midterm?");
            int midtermWeight = input.nextInt();
            System.out.println("What percent of your grade is the homework?");
            int hwWeight = input.nextInt();

            int testWeighted = (finalsScore * (testWeight / 100));
            int midtermWeighted = (midtermScore * (midtermWeight / 100));
            int hwWeighted = (overAll * (hwWeight / 100));
            int courseScore = ((hwWeighted + midtermWeighted + testWeighted) * 100);
            System.out.println("Your total course grade is " + courseScore);
        }

}

The methods hwpoints , midTerm and finalScore all return an int value which you are not keeping

You need to do some thing like

int hwp = hwpoints ();
int mt = midterm ();
int fs = finalScoare ();

then you can pass these variable into courseScore as

courseScore (fs, mt, hwp);

Note

In this code

int testWeighted = (finalsScore * (testWeight / 100));

you are going to be undertaking integer division.
See Int division: Why is the result of 1/3 == 0?

First thing is to understand the concept of a class and a static method.

  • Classes have fields and methods.

    • Fields (variables) hold data (state) that the Methods can operate on
    • Each instance of a class has its own values for these fields
    • Methods operate on the Fields
    • Methods can include call parameters
    • Methods can also return a value
  • Static instances (classes, fields, and methods) are singletons

    • There is only one copy of them (all instances of the class share the same one)
    • Static Methods cannot access (non-static) fields/methods

Using this, consider creating another class that has (most) of the content of your GradeCalc class and remove the statics from them and create an instance of that class in GradeCalc. That way each method in the class can access the fields you have defined.

Here is an example of the concept based on what you have already written. Note: You code has other structural/implementation issue and I would never implement it as shown; but, I don't want to turn this into a Java course; so, I tried to show you something that could work within the context of what you had written.

public class GradeCalc {
  public static void main(String[] args) {
      Scanner scanner = new Scanner(System.in);
  Calculator calc = new Calculator (calc);
  calc.hwpoints();
  calc.midTerm();
  calc.courseScore();
  int overAll = calc.OverAll;
  int midtermScore = calc.MidtermScore;;
  int overAll = calc.OverAll;
}
public class Calculator {
  public void OverAll;
  public void MidtermScore;
  public void FinalsScore;

  public void hwpoints()
  {
    ...
    OverAll = overAll
  }
  //Do the same for the methods below
  //public void midTerm();
  //public void courseScore();  
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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