gpt4 book ai didi

java - 如何将 int 转换为 double?

转载 作者:行者123 更新时间:2023-12-02 02:33:32 24 4
gpt4 key购买 nike

该程序的目的是从用户处获取 5 个值(测试分数),然后输出平均分数。我对数组不熟悉,所以我真的不知道我做错了什么。我所知道的是双“sum”不能设置为等于int“total”。抱歉我很蠢,但我正在尝试:)

import java.util.Scanner;

public class Main
{

public static void main (String [] args)
{

int x = 0;
double testScore[] = new double[5];
double sum[] = new double[5];
double total;
int avg;

Scanner keys = new Scanner(System.in);

System.out.println("Enter the values of 5 separate test scores that you have received: \n");

for (int i = 0; i < testScore.length; i++)
{
x++;
System.out.println("Enter your grade for test number " +1);
double score = keys.nextDouble();

score = testScore[i];
sum = testScore;
sum = (int)total;
avg = ((total) / 5);


System.out.print("The sum of your grades is " +avg +"\n");
}

}
}

最佳答案

double sum = 0;
for (double score: testScore) {
sum += score;
}
double avg = sum / testScore.length;

关于java - 如何将 int 转换为 double?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46739214/

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