如何将int转换为double?

时间:2017-10-13 23:18:53

标签: java casting

该程序的目的是从用户获取5个值(测试分数),然后输出平均分数。我不熟悉阵列,所以我真的不知道我做错了什么。我所知道的只是双倍的总和。不能设置相当于int'总&#39 ;.抱歉愚蠢,但我在这里尝试:)

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");
    }

  }
}

4 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

在这里,我试着不要改变你的大部分代码,这样你仍然可以理解我改变了什么!

public static void main(String[] args) {
    double testScore[] = new double[5];
    double sum = 0;
    double 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++) {
        System.out.println("Enter your grade for test number " + 1);
        double score = keys.nextDouble();

        sum += score;
        avg = sum / 5;

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

基本上,您只需要sum变量,就可以从中获取avg

答案 2 :(得分:0)

首先我会声明你的变量:

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

要做出一些改变:
你不想把分数设置为testscore [i],因为它的null是如此。如果要将双精度转换为整数,请使用Integer.valueOf()。您还应将它们放在for循环之外,并在for循环中计算sum,如下所示:

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

    testScore[i] = score;
    sum += Integer.valueOf(score);
    total += score;

}
avg = Integer.valueOf(total / testScore.length);

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

我没有测试过这段代码,但我希望它有所帮助。

答案 3 :(得分:0)

为了得到数组所有元素的总和,你需要迭代它:

Scanner keys = new Scanner(System.in);
double[] testScore = new double[5];
double sum = 0.0;

for (int i = 0; i < testScore.length; i++) {
    // We don't need to use x, we already have i
    System.out.println("Enter your grade for test number " + (i + 1));

    double score = keys.nextDouble();
    testScore[i] = score; // Store the grade into the element #i of the array

    // We can instantly process the data we have to collect. Otherwise we must
    // walk a second time over the elements
    sum = sum + score; // Or the shorthand notation: sum += score
}

double avg = sum / testScore.length;
System.out.println("The average of your grades is " + avg + "\n");

我将以下内容修改为您的代码:

  • 您正在使用具有相同功能的两个数组(testScoresum)。两者都是为了存储成绩。我删除了其中一个。
  • 我删除了x,因为i已经履行了它的功能。
  • 我将变量avg的类型更改为double。可以随意将其更改回int,但平均小数将被截断(例如,4.6将为4)。