英文:
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'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
</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("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!
专注分享java语言的经验与见解,让所有开发者获益!
评论