gpt4 book ai didi

java - 计算总和和平均值

转载 作者:行者123 更新时间:2023-12-02 04:18:53 25 4
gpt4 key购买 nike

我一直在试图解决这个问题,要么我想得太多,要么我只是错过了一些东西。我需要用户输入学生人数,明白了。然后我需要根据用户输入的数字为每个学生输入一个测试成绩。这就是我被困住的地方。然后我需要根据这些测试成绩计算总和,并计算平均值。任何帮助将不胜感激。到目前为止我有这个:

import java.util.*;

public class computeSumAverage {

public static void main(String[] args) {

Scanner kbd = new Scanner(System. in );

System.out.print("Please enter the number of students = ");

int student = kbd.nextInt();

System.out.print("Please enter " + student + " test grades = ");

int count;
double grades = kbd.nextDouble();
double sum = kbd.nextDouble();
double average;

for (count = 0; count <= 100; count++);

sum = (grades++);

average = (sum / student);

System.out.println("The sum of the numbers is = " + sum + "." + "\n");
System.out.println("The average of the numbers is = " + average + ".");
}
}

最佳答案

试试这个。它应该可以工作。

public static void main (String[] args) {

Scanner kbd = new Scanner (System.in);

System.out.print ("Please enter the number of students = ");

int count = kbd.nextInt();

double sum = 0;
double average = 0;;

for (int student = 0; student < count; student ++){

System.out.print ("Please enter Student: " + String.valueOf(student +1) + " test grades = ");

double grades = kbd.nextDouble();

sum += grades;

}

average = (sum / count);

System.out.println ("The sum of the grades is = " + sum + "." + "\n");
System.out.println ("The average of the grades is = " + average + ".");

}

你的代码有什么问题。

  1. 在 for 循环中是没有用的,因为它不能帮助你实现你想要做的事情,而 ;立即停止它。循环没有核心。您应该循环计算您拥有的学生数量。

  2. 在循环内,您应该读取成绩并提示用户输入每个学生的成绩。

  3. 我猜你并没有真正尝试过。

  4. 在编码之前您应该思考您正在做的事情。

关于java - 计算总和和平均值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32997768/

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