我想在Java中从一个类中使用另一个类中的变量。

huangapple 未分类评论45阅读模式
英文:

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

问题

我一直在思考如何处理这个问题我一直在尝试从 GradesApp.java 中获取用户的输入并存储在我的变量 "gradesPossible"然后将它传递给另一个类 Grades.java无论这个变量是同名还是不同名我都无所谓我尝试过使用 setter 或者 getter 方法但我对它不是很熟悉

GradesApp.java:

```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());
	}
}

我想在 Grades.java 中访问 "gradesPossible"。

Grades.java:

public class Grades {

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

修正了拼写错误。


<details>
<summary>英文:</summary>

I&#39;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 &quot;gradesPossible&quot; then pass it to another class Grades.java. I don&#39;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 &quot;gradesPossible&quot; in Grades.java

Grades.java: 

public class Grades {

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


Edited typos

</details>


# 答案1
**得分**: 0

以下是您提供的代码的翻译部分:

```java
public class GradesApp {

    public static void main(String[] args) {

		System.out.print("请输入课程的总分可能性:");
		int gradesPossible = Integer.parseInt(s.nextLine());

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

		System.out.print("得分等级: " + gradeResult);

	}

}

以下是Grades类的内容:

public class Grades {

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

希望这对您有所帮助!

英文:

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(&quot;Enter the total points possible for the class: &quot;);
		int gradesPossible = Integer.parseInt(s.nextLine());

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

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

	}

}

And this is for Grades class

public class Grades {

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

Hope this helps!

huangapple
  • 本文由 发表于 2020年4月7日 12:05:15
  • 转载请务必保留本文链接:https://java.coder-hub.com/61072652.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定