gpt4 book ai didi

java - 如何重构此代码并获取数组中所有学生总分的平均值

转载 作者:行者123 更新时间:2023-12-01 10:51:33 26 4
gpt4 key购买 nike

客户类别

public class Assignment2Q1 {
public static void main(String[] args)
{
int sum;
String choice;
String option;
Scanner reader = new Scanner(System.in);

System.out.println("What is your option");
option = reader.next();
Student [] Studentlist ;
int numofStudent = 0;
while("Add".equalsIgnoreCase(option))
{
System.out.println("How many student would you like to enter: ");
numofStudent=reader.nextInt();
Studentlist=new Student [numofStudent];

for(int i=0;i<numofStudent;i++)
{
Studentlist[i] =new Student();
Studentlist[i].inputDetails();

}
System.out.println("What is your option");
choice = reader.next();
while("TotalAVG".equalsIgnoreCase(choice))
{
for(int i=0;i<Studentlist.length;i++)
{
Studentlist[i].avg();
}
System.out.println("What is your option");
choice = reader.next();
}


System.out.println("What is your option");
choice = reader.next();

}

//Input Method
public void inputDetails()
{
System.out.println("Please Enter The Score of Assignemt 1:");
ass01=reader.nextInt();
System.out.println("Please Enter The Score of Assignemt 2:");
ass02=reader.nextInt();
System.out.println("Please Enter The Total Mark of Weekly Pratice Work:");
weeklyWork=reader.nextInt();
System.out.println("Please Enter The Score of Final Exam:");
finalExam=reader.nextInt();
}

//Average method
void avg()
{
int count = 0;
int overallMark =0;
int sum = 0;
overallMark = (ass01/5)+(ass02/5)+weeklyWork+(finalExam/2);
sum = sum+overallMark;
count = count +1;
System.out.println("The average: "+(sum/count) );
}

I have the problem is when I try input TotalAVG,it show me two output

The average: 50

The average: 3

总体分数是在子类中构建的,

  1. 如何收集所有总体分数并求其平均值

  2. 如何折射此代码?

最佳答案

在你的avg()方法中:

  1. 您声明 int sum = 0;,然后向其中添加另一个变量:sum = sum+overallMark;,因此 sum 将始终包含总体评分
  2. 另一个类似的问题是 count:您在 avg() 中声明它:int count = 0; 然后添加 1:count = count +1; 因此它的值始终为 1

关于java - 如何重构此代码并获取数组中所有学生总分的平均值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33852765/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com