如果你得到了,我需要知道如何将我的平均函数推广到这个结果函数中?
这是主要课程:
import java.util.ArrayList;
import java.util.Scanner;
public class main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
ArrayList < Calculations > students = new ArrayList < Calculations > ();
for (int i = 0; i < 5; ++i) {
Calculations s = new Calculations();
System.out.print("Enter name : ");
s.name = scan.next();
System.out.print("Eter percentage : ");
s.percetage = scan.nextDouble();
students.add(s);
System.out.println("Enter Mrks Obtained");
}
for (Calculations s: students) {
s.result();
for (Calculations i: students) {
s.Average();
}
}
}
}
这是结果类:
import java.util.Scanner;
public class Calculations {
public String name;
public double percetage;
public int integer;
public void result() {
System.out.println((percetage >= 35.0 f ? (name + " Pass") : (name + " Fail")));
}
public void Average() {
// TODO Auto-generated method stub
int mark[] = new int[5];
int i;
float sum = 0;
float avg, perc;
Scanner scan = new Scanner(System.in);
System.out.print("Enter marks Obtained in 5 Subjects : ");
for (i = 0; i < 5; i++) {
mark[i] = scan.nextInt();
sum = sum + mark[i];
}
avg = sum / 5;
perc = (sum / 500) * 100;
System.out.print("Average Marks = " + avg);
System.out.print("\nPercentage = " + perc + "%");
}
}
答案 0 :(得分:0)
for(Calculations s : students){
s.result();
//for(Calculations i : students){s.Average(); }
s.Average();
}
你不必两次循环你的arraylist。尝试更改代码。
答案 1 :(得分:0)
很难完成你不完整的问题,这个问题也不具体。你的程序在我的日食中运行良好。我建议遵循:
1)检查计算类中的字段/拼写的名称,使其与主类中使用的完全匹配。 2)由于您正在输入字符串扫描程序,请尝试按如下方式读取行:
s.name = scan.nextLine();
我希望它有所帮助。