找到分数的平均值

时间:2017-04-21 11:24:32

标签: java average

我是java的新手,whixh解释了loq质量代码。我试图计算我在我的代码中输入的分数的平均值,这是在下面,但它只获得在执行其余代码后输入的数字的平均值。我如何通过这行代码找到我在代码中放入的分数的平均值:
 System.out.printf(“%。2f”,平均值); 为了清楚起见,我想要在我输入星号之后输入的数字的平均值。 谢谢

import java.util.Scanner;
public class week4
{
    public static void main(String[] args) {

int n =1;

{
    do{
    n++;
    String studentname;

    int mark;

    Scanner inText = new Scanner(System.in);

    System.out.print("Please enter the name of the student >> ");

    studentname = inText.nextLine();

    Scanner inNumber = new Scanner(System.in);

    *System.out.print("Please enter mark for student " + studentname + " out of 100 >> ");*


    mark = inText.nextInt();

    if(mark <50) System.out.println("The grade for " + studentname + " is F " );

    else if(mark <65) System.out.println("The grade for " + studentname + " is P " );

    else if(mark <75) System.out.println("The grade for " + studentname + " is C " );

    else if(mark <85) System.out.println("The grade for " + studentname + " is D " );

    else System.out.println("The grade for " + studentname + " is HD" );

    } while(n <=3);

    Scanner input = new Scanner(System.in);

                int sum = 0;
                int mark;
                int average;
                int counter = 1;

                while (counter < 2){
                    mark = input.nextInt();
                    sum = sum + mark;
                    counter++;
                    average = sum/3;
                    System.out.printf("The aerage score is " + average  );
                }


        }
    }
}

1 个答案:

答案 0 :(得分:0)

最后的代码块是不必要的:

                    while (counter < 2){
                    mark = input.nextInt();
                    sum = sum + mark;
                    counter++;
                    average = sum/3;
                    System.out.printf("The average score is " + average  );

您应该添加一个全局变量,例如total。当用户输入其标记时,将每个用户分数存储在总变量中。用此替换您的代码(用粗体突出显示更改):

public class week4 {

    public static void main(String[] args) {
        int total = 0;

        int n = 1;

        {
            do {
                n++;
                String studentname;

                int mark;

                Scanner inText = new Scanner(System.in);

                System.out.print("Please enter the name of the student >> ");

                studentname = inText.nextLine();

                Scanner inNumber = new Scanner(System.in);

                //*
                System.out.print("Please enter mark for student " + studentname + " out of 100 >> ");

                mark = inText.nextInt();
                **total += mark;**

                if (mark < 50) {
                    System.out.println("The grade for " + studentname + " is F ");
                } else if (mark < 65) {
                    System.out.println("The grade for " + studentname + " is P ");
                } else if (mark < 75) {
                    System.out.println("The grade for " + studentname + " is C ");
                } else if (mark < 85) {
                    System.out.println("The grade for " + studentname + " is D ");
                } else {
                    System.out.println("The grade for " + studentname + " is HD");
                }

            } while (n <= 3);

            Scanner input = new Scanner(System.in);

            int sum = 0;
            int mark;
            int average;
            int counter = 1;

            **average = total / 3;
            System.out.printf("The aerage score is " + average);**

        }
    }