考试范围介于1到100之间

时间:2018-03-10 14:06:59

标签: java jcreator

import java.util.Scanner;

public class Exercise1{
public static void main(String args[]){
    int i=1,mark,totalmarks=0,highestmark=0;

    Scanner input = new Scanner(System.in);

    do{
        System.out.print("Please enter the mark of student " + i + ":");
        mark = input.nextInt();

        while(mark<0 || mark>100)
        {
            System.out.print("Invaild! Please enter the mark of student " + i + ":");
            mark = input.nextInt();
        }
        if(mark>highestmark)
            highestmark = mark;

        totalmarks += mark;

        System.out.println("\n");

        i++;

    }while(i<=5);

    System.out.println("\n\nHighest mark was: " + highestmark +
                       "\nAverage mark was: " + String.format("%.0f",totalmarks/5f));
    }
}

在5名学生的考试中,确定达到的最高分数以及最接近整数的分数平均值。这些标记都应该是0到100范围内的数学整数(整数)。

上面的段落是此代码的对象。 我想知道这段代码在逻辑上很奇怪。

如果(标记&GT; highestmark)   最高标记=标记; 当我看到这段代码时,我认为所有的数字都可以是1,2,3到100的标记 但最高标记最初初始化为0。 它是如何合乎逻辑的?

和最后一个陈述是(totalmarks / 5f) 我为什么要在5月底加'f'? 如果我不添加f,则输出错误 请回答并向我解释这些问题并感谢您的帮助

2 个答案:

答案 0 :(得分:0)

因此,最高标记初始化为0,因为任何标记都会高于此标记。这意味着第一次通过循环最高标记将被设置为第一个标记。如果它被初始化为1到100之间的某个数字,如果所有学生标记低于最高标记的初始值,则可能导致输出错误。它甚至可以设置为-1,因为0是有效标记。

至于最后一个语句,它使用5f beacuse,这会将值转换为除法所需的浮点值。如果两个值都是整数,则java将执行整数除法,这不是您在这种情况下所需的值。

答案 1 :(得分:0)

我对您的代码进行了一些修改以提高可读性,并添加了一些关于它正在做什么的评论。我也改变了变量名和声明。请参阅有关逻辑的代码中的注释。对于你的部门中用于计算平均值的数字末尾的f,如果缺少这个,则会得到java.util.IllegalFormatConversionException异常,因为5的数据类型是int(而不是float)。在下面的代码中,文字(5)已被替换为常量TOTAL_STUDENTS,它被强制转换为浮点数(请参阅(float))以执行除法。

// declare a constant for the number of students.
// this will make changes easier (avoid hard-coding constants in code).
private static final int TOTAL_STUDENTS = 5;

// these two constants are for the bounds on the mark (it
// must be between 0 and 100)
private static final int LOWEST_MARK_POSSIBLE = 0;

private static final int HIGHEST_MARK_POSSIBLE = 100;

public static void main(String args[]) {
    // you had the current student as 1 and an increment at the end of the loop
    // but if you increment at the beginning of your do loop it reads better
    int currentStudent = 0;

    // no marks have been given yet in input
    int totalMarks = 0;

    // initialize the highest mark at this point (beginning of the program) 
    // as 0 because no data has been collected yet and since all marks 
    // have to be greater than or equal to 0, this is the lowest starting 
    // mark possible
    int highestMark = LOWEST_MARK_POSSIBLE;

    Scanner input = new Scanner(System.in);

    do {
        currentStudent++;
        System.out.print("Please enter the mark of student " + currentStudent + ":");

        // get the next integer entered from the keyboard
        int currentStudentMark = input.nextInt();
        // validate it -- if it's not in the acceptable bounds, retry
        while (currentStudentMark < LOWEST_MARK_POSSIBLE || currentStudentMark > HIGHEST_MARK_POSSIBLE) {
            System.out.print("Invaild! Please enter the mark of student " + currentStudent + ":");
            currentStudentMark = input.nextInt();
        }

        // if the mark that was just entered is more than the 
        // highest mark stored, update the highest mark to 
        // be equal to the mark just entered
        if (currentStudentMark > highestMark) {
            highestMark = currentStudentMark;
        }

        // add the current student mark to the total marks counted
        totalMarks += currentStudentMark;

        System.out.println("\n");

    } while (currentStudent < TOTAL_STUDENTS); 
    // your while condition was <= 5 because you had the increment at the end of the 
    // cycle for the current student

    // this was missing in your original code; the InputStream should always be closed when you're done with it
    input.close();

    System.out.println("\n\nHighest mark was: " + highestMark + "\nAverage mark was: "
            + String.format("%.0f", totalMarks / (float) TOTAL_STUDENTS));
}