0

I'm just a beginner in Java. Can someone please tell me how to arrange this program. In this program I want to store 10 marks in an array, and then output the ones that are equal to or greater than the average. Help would be appreciated.

public class ClassAverage{
public static void main (String Args []){

    int sum = 0;
    int mark[] = new int [10];

    for(int c = 0; c < 10; c++){

        System.out.print("Enter a mark: ");
        mark[c] = Keyboard.readInt();

        }
    sum = sum + mark[c];
    int average = sum / 10;

    if(mark[c]>=average){
    System.out.print(mark[c]);

    }

}

}

4

5 回答 5

3

您需要循环数组两次。第一次迭代对所有元素求和并计算平均值。另一个迭代输出满足您的约束的元素。

代码如下:

public class ClassAverage{
    public static void main (String Args []){

        int sum = 0;
        int mark[] = new int [10];
        // The first iteration sums all elements
        for(int c = 0; c < 10; c++){
            System.out.print("Enter a mark: ");
            mark[c] = Keyboard.readInt();

            sum = sum + mark[c];
        }
        int average = sum / 10;
        // This iteration outputs elements that meet your requirements.
        for(int c = 0; c < 10; c++){
            if(mark[c]>=average){
                System.out.print(mark[c]);
            }
        }
    }
}
于 2013-06-01T07:18:19.860 回答
1
public class aver{
  public static void main (String Args []){

    int sum = 0;
    int mark[] = new int [10];
    int average;
    for(int c = 0; c < 10; c++){
        System.out.print("Enter a mark: ");
        mark[c] = Keyboard.readInt();
    }
    for(int c = 0; c < 10; c++){
        sum = sum + mark[c];
    }
    average = sum / 10;
    for(int c = 0; c < 10; c++){
        if(mark[c]>=average){
           System.out.println(mark[c]);
        }
    }

  }
}
于 2013-06-01T07:20:25.453 回答
1
sum = sum + mark[c]; 

语句必须在您的循环内。

public class ClassAverage{
public static void main (String Args []){

    int sum = 0;
    int mark[] = new int [10];

    for(int c = 0; c < 10; c++)
    {
        System.out.print("Enter a mark: ");
        mark[c] = Keyboard.readInt();
         sum = sum + mark[c];
     }
    int average = sum / 10;
    for(int c = 0; c < 10; c++)
    {
           if(mark[c]>=average)
           {
             System.out.print(mark[c]);

           }
     }
}
于 2013-06-01T07:26:48.633 回答
1

for在计算平均值后,您需要添加另一个循环来迭代您的数组。这是带有所需for循环的更新代码:

public static void main (String Args []){
    int sum = 0;
    int mark[] = new int [10];

    for(int c = 0; c < 10; c++) {    
        System.out.print("Enter a mark: ");
        mark[c] = Keyboard.readInt();
        sum = sum + mark[c];
    }

    int average = sum / 10;
    for(int c = 0; c < 10; c++) {
        if(mark[c]>=average){
            System.out.print(mark[c]);
        }
    }
}
于 2013-06-01T07:16:58.987 回答
1

只是对您的代码进行了一些修改。

public class ClassAverage{
public static void main (String Args []){

    int sum = 0;
    int mark[] = new int [10];

    for(int c = 0; c < 10; c++){

        System.out.print("Enter a mark: ");
        mark[c] = Keyboard.readInt();

        sum = sum + mark[c];
    }
    int average = sum / 10;

    for(int c = 0; c < 10; c++){

        if(mark[c]>=average)
            System.out.print(mark[c]);

    }    
}

解释

  1. 您需要将每个元素相加才能得到总数。因此,在输入后在 for 循环中添加每个元素
  2. 您还需要检查每个元素以与平均值进行比较。所以你需要再次迭代数组来比较每个元素。
于 2013-06-01T07:15:07.923 回答