简体   繁体   English

我想在 Java 中的另一个 class 上使用来自一个 class 的变量

[英]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.我一直在尝试从 GradesApp.java 中的用户那里获取输入并将其存储在我的变量“gradesPossible”中,然后将其传递给另一个 class Grades.Z93F725A07423FE1C8849F448B33D21。 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.我试图使用 setter 或 getter 方法,但我不是很熟悉。

GradesApp.java:等级App.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 中的“gradesPossible”

Grades.java:等级.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?你想用 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这是针对等级 class

public class Grades {

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

Hope this helps!希望这可以帮助!

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

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