显示超过特定百分比Java的值的数量

时间:2017-02-01 20:31:34

标签: java arrays

我需要使用二维数组编写一个Java程序,该数组存储由8名受试者中的40名学生完成的标记,找到平均值并查找获得区别的事件(标记超过70%)。 除最后一项要求外,该程序运行正常。现在它计算所有标记如果一个或多个超过70%(因此结果总是8)。 我想我对如何计算超过70%的商标感到困惑。示例代码很好,但也请尝试解释我做错了什么... 谢谢! :)

import java.util.Scanner;
public class db {
    public static void main(String[] args) {
        //Variables
        double mark = 0, average = 0, sum = 0, counter = 0, achievement = 0, percentage = 0, counterpercentage = 0;

        double[][] marksTable = new double[40][8];
        //New Scanner object
        Scanner fromKeyboard = new Scanner(System.in);

        for (int studentNo = 0; studentNo < 40; studentNo++) {
            System.out.println("Enter marks for student no" + studentNo);
            sum = 0;
            counter = 0;

            for (int moduleNo = 0; moduleNo < 8; moduleNo++) {
                System.out.println("Mark for student " + studentNo + " for module no " + moduleNo + ":");
                //Read value into variable mark
                mark = fromKeyboard.nextDouble();
                // Write mark into array
                marksTable[studentNo][moduleNo] = mark;
                //Calculations
                sum = sum + mark;
                counter = counter + 1;
            }
            percentage = mark;
            average = sum / counter;
            counterpercentage = counter;
            //Display array
            for (int moduleNo = 0; moduleNo < 8; moduleNo++) {
                System.out.println("Average for student " + studentNo + " for module no " + moduleNo + " is: " + average);
                break;
            }
            if (percentage >= 70) {
                System.out.println(" The number of high marks achieved for this student are: " + counterpercentage);
            }
            if (percentage < 70) {
                System.out.println("No high marks obtained");
            }
        }
    }
}

2 个答案:

答案 0 :(得分:0)

试试这个:

import java.util.Scanner;
public class db
{
    public static void main (String []args)
    {
    //Variables
    double mark=0, average=0, sum=0, counter=0, achievement=0, percentage=0, counterpercentage=0;

    double[][] marksTable = new double[40][8];
    //New Scanner object
    Scanner fromKeyboard=new Scanner (System.in);

for (int studentNo = 0; studentNo < 40; studentNo++)
{
     System.out.println("Enter marks for student no" +studentNo);
     sum =0 ;
     counter = 0;

for (int moduleNo = 0; moduleNo < 8; moduleNo++)
{
     System.out.println("Mark for student "+studentNo+" for module no      "+moduleNo+":");
     //Read value into variable mark
     mark = fromKeyboard.nextDouble();
     // Write mark into array
     marksTable[studentNo][moduleNo] = mark;
     //Calculations
     sum=sum+mark;
     counter=counter+1;
     if(mark >= 70) {  //***Changed***//
       counterpercentage++;
     }
}
average=sum/counter;
//Display array
for (int moduleNo = 0; moduleNo < 8; moduleNo++)
{
      System.out.println("Average for student "+studentNo+" for module no "+moduleNo+" is: "+average);
     break;
}
    if (counterpercentage >=0)  //***Changed***//
    {
        System.out.println("The number of high marks achieved for this student are: "+ counterpercentage);
    }
    else
    {
        System.out.println("No high marks obtained");
    }
}
}
}

答案 1 :(得分:0)

这是因为这句话:

percentage = mark;

它将mark的值分配给百分比,我们需要做的是计算百分比,即总分数除以最大分数,例如

percentage = (sum/(100.0 * marksTable[studentNo].length))*100; //(Assuming 100 as max marks for eah module)