我有一个方法studentSummary,它扫描输入并创建三个数组examMark,courseworkMark和courseworkWeight。我需要将这些数组传递给另一个方法,因此我可以使用它们来计算moduleResult。
继承我的代码:
public static int[] studentSummary(int[] courseworkWeight2, int [] examMark2 , int [] courseworkMark2){
int examMark[] = { 0, 0, 0, 0, 0, 0 };
int courseworkMark[] = { 0, 0, 0, 0, 0, 0 };
Scanner resultInput = new Scanner(System.in);
int courseworkWeight[] = { 0, 0, 0, 0, 0, 0 };
for (int k = 1; k < 7; k++) {
System.out.print("Please enter exam marks for module " + k + ":");
examMark[k - 1] = resultInput.nextInt();
System.out.print("Please enter Coursework marks for module " + k
+ ":");
courseworkMark[k - 1] = resultInput.nextInt();
System.out.print("Please enter Coursework weighting for module "
+ k + ":");
courseworkWeight[k - 1] = resultInput.nextInt();
}
计算器方法:
public static int[] markCalculator() {
int[] courseworkWeight = new int [6];
int[] courseworkMark = new int [6];
int[] examMark = new int [6];
for (int i = 0; i < 6; i++) {
computedModuleMark = ((courseworkMark[i] * courseworkWeight[i]) + (examMark[i] * (100 - courseworkWeight[i]))) / 100;
if ((computedModuleMark) < 35) {
if (examMark[i]<35){
}
}
moduleMark[i] = computedModuleMark;
}
computeResult(moduleMark);
StudentChart.draw(moduleMark);
StudentChart.printSummary(moduleMark);
return moduleMark;
}
答案 0 :(得分:0)
将markCalculator()
替换为markCalculator(int[] courseworkWeight, int [] examMark , int [] courseworkMark)
,并从方法中删除这些变量的声明。
在markCalculator(courseworkWeight, examMark, courseworkMark)
studentSummary()
醇>
我猜这是学生总结,不应该接受任何争论。是否通过交换这些方法的签名使您的复制粘贴出错?
其他可能性是你的源将这些数组作为全局变量(在方法之外,在同一个文件中),并且两个方法都没有参数 - method()。
答案 1 :(得分:0)
你可以:
1.将数组声明为类字段,然后调用函数
markCalculator(int[] courseworkWeight2, int [] examMark2 , int [] courseworkMark2)
2.从studentSummary()发送markCalculator(courseworkWeight, examMark, courseworkMark)
3.将它们从函数studentSummary(...)的
List<Object> list = new ArrayList<>().addAll(courseworkWeight2,examMark2,courseworkMark2);
并通过调用markCalculator(List<Object>);