编写一个程序,反复提示用户提供分数(满分100分)java

时间:2018-04-19 07:44:02

标签: java

编写一个程序,反复提示用户在5个学生的测试中为5个学生提供分数(满分100分)一旦该程序具有真正的所有分数,它应该生成一个具有以下标题的表格(并自动填写其余部分)表)例如

                   Student No #              Score(out of 100)
                      1                             55

                      2                             66

程序应该计算总分

这是我的代码

import java.io.*;
import java.util.*;

public class test33 {

    public static void main(String[]args){

        Scanner kbReader = new Scanner (System.in);

        int scores[] = new int [100];
        int counter = 0;
        int sum = 0;    
        int input = 0;

        do {
            System.out.println("Enter score out of 100 or negative to break.");
            input = kbReader.nextInt();

            if (input < 0) {
                break;
            } else if (input > 100) {
                System.out.println("Score must be out of 100");
            } else {
                scores[input]++;
                counter++;
                sum += input;
            }
        } while (input > 0);

        System.out.println("Score\t# of occur...");
        for (int i = 0; i < 100; i++) {
            System.out.println(i + "\t" + scores[i]);
        }
        System.out.println("The mean score is " +(sum/counter));
    }
}                   

1 个答案:

答案 0 :(得分:-1)

scores[input]++;如果您想为数组scores[]分配值,则必须像我在下面所做的那样进行更正。

public static void main(String[]args) {
        Scanner kbReader= new Scanner (System.in);
        int scores[] = new int [100];
        int counter = 0;
        int sum = 0;
        int input = 0;
        do {
            System.out.println("Enter score out of 100 or negative to quit.");
            input=kbReader.nextInt();
             if(input<0) {
                 break;
             }
             else if (input>100) {
                System.out.println("Score must be out of 100");
            } else {
                scores[counter]=input;
                counter++;
                sum+=input;
            }
        } while (input>0);
        if(counter != 0){
            System.out.println("Score\t# of occur...");
            for(int i =0; i<100; i++) {
                System.out.println(i + "\t" + scores[i]);
            };
            System.out.println("The mean score is " +(sum/counter));
        }
    }