2017-10-13 8 views
0

このプログラムの目的は、ユーザーから5つの値(テストスコア)を摂取して平均スコアを出力することです。私は配列に精通していないので、私が間違っていることを少しでも手掛かりにしません。私が知っているのは、double 'sum'はint 'total'と等価に設定できないということです。ダムであるために申し訳ありませんが、あなたが行くここ:)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"); 
    } 
} 

基本的に、あなたが必要とするすべては、あなたがそれからavgを得ることができ、sum変数です!そのヌルように反転するのであなたは[I]をtestscoreするスコアを設定したくない
:行うべき

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

いくつかの変更:

0

最初私はこのようなあなたの変数を宣言します。倍精度を整数にキャストしたい場合は、Integer.valueOf()を使用します。また、forループの外に置き、図のように、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"); 
    

    は、私はあなたのコードに次のようなものを修正しました同じ機能を有する2つのアレイ(testScoreおよびsum)。両方とも、その中に成績を保存することを意図しています。私はそれらの1つを削除しました。

  • iが既にその機能を満たしているため、xを削除しました。
  • 変数avgのタイプをdoubleに変更しました。自由にintに変更してください。平均の小数は切り捨てられます(例:4.6は4になります)。
関連する問題