尝试编写成绩计算器程序,并且遇到将返回变量从一种方法传递到另一种方法的问题。我是编码的新手,所以我确定这不是很漂亮,但是希望能得到一些帮助。 我有一种计算作业分数的方法,一种计算期中分数的方法以及一种计算最终分数的方法。我正在尝试在最后一个方法中调用返回变量以计算总体成绩。该错误是在调用CourseScore方法时显示的:GradeCalc类型的courseScore(int,int,int)方法不适用于参数()。任何帮助表示赞赏!
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);
}
}
答案 0 :(得分:0)
方法hwpoints
,midTerm
和finalScore
都返回一个您不保留的int值
您需要做类似的事情
int hwp = hwpoints ();
int mt = midterm ();
int fs = finalScoare ();
然后您可以将这些变量作为{p>传递给courseScore
courseScore (fs, mt, hwp);
注意
在此代码中
int testWeighted = (finalsScore * (testWeight / 100));
您将要进行整数除法。
参见Int division: Why is the result of 1/3 == 0?
答案 1 :(得分:0)
首先是要了解类和静态方法的概念。
类具有字段和方法。
静态实例(类,字段和方法)是单例
使用此方法,请考虑创建具有GradeCalc类的(大部分)内容的另一个类,并从它们中删除静态变量,并在GradeCalc中创建该类的实例。这样,类中的每个方法都可以访问您定义的字段。
这里是一个基于您已经写过的概念的示例。 注意:您的代码还有其他结构/实现问题,我将永远不会如图所示实现它;但是,我不想将其转变为Java课程。因此,我试图向您展示一些可以在您所写内容范围内起作用的东西。
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();
}