2017-10-13 148 views
0

这个程序的目的是从用户中吸取5个值(测试分数),然后输出平均分数。我对阵列并不熟悉,所以我真的没有丝毫的线索知道我做错了什么。我所知道的是双倍'总和'不能等同于'总数'。对不起,我是愚蠢的,但我想在这里:)如何将int转换为double?

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

    } 
} 
+0

另外,我知道我不能使用Main作为公共类。 –

+0

'sum'是一个数组,'total'是一个'double',将一个指派给另一个是没有意义的。也许你应该阅读一些关于数组的知识,它应该会帮助你很多... – acdcjunior

回答

1
double sum = 0; 
for (double score: testScore) { 
    sum += score; 
} 
double avg = sum/testScore.length; 
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

0

我首先声明变量是这样的:

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

要做出一些改动:
你不想设置比分testscore [我],因为它的空,因此该翻转。如果你想把双精度转换为整数,使用Integer.valueOf()。你也应该将它们放在外面的for循环,并在计算总和环,如图所示:

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

我没有测试代码,但我希望它能帮助。

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)。