简体   繁体   中英

I want to use my variable from one class on another class in Java

I've been going back and forward on how to deal with this. I have been trying to take input from user in GradesApp.java and store in my variable "gradesPossible" then pass it to another class Grades.java. I don't mind if it is the same name variable or different. I was trying to use setter or getter method, but I am not very familiar with it.

GradesApp.java:

public class GradesApp {

    public static void main(String[] args) {

System.out.print("Enter the total points possible for the class: ");
int gradesPossible = Integer.parseInt(s.nextLine());

I want to access "gradesPossible" in Grades.java

Grades.java:

public class Grades {

public double gradesFor3Classes()
    {
    double grade1 = (gradesPossible*3);
    return grade1;
    }

Edited typos

what do you want to do with value of the gradesPossible? If you want to calculate the value, maybe it's something like this:


public class GradesApp {

    public static void main(String[] args) {

        System.out.print("Enter the total points possible for the class: ");
        int gradesPossible = Integer.parseInt(s.nextLine());

        Grades grades = new Grades();
        double gradeResult = grades.calculateGrade(gradesPossible);

        System.out.print("Value Grade: " + gradeResult);

    }

}

And this is for Grades class

public class Grades {

    public double calculateGrade(int gradesPossible) {
        double grade1 = (gradesPossible*3);
        return grade1;
    }
}

Hope this helps!

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