【问题标题】:In an exam mark in the range between 1 and 100考试分数在 1 到 100 之间
【发布时间】:2018-08-18 23:34:25
【问题描述】:
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 范围内的数学整数(整数)。

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

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

最后一个语句是 (totalmarks/5f) 为什么我要在 5 的末尾添加“f”? 如果我不添加 f,则会输出错误 请为我回答并解释这些问题,感谢您的帮助

【问题讨论】:

  • 请在每个帖子中提出 一个 问题 - 并花更多时间来解释自己。你说没有f“错误输出”但你没有说那个错误是什么,你关于highestmark的问题对我来说也很不清楚。
  • 您似乎从某个地方复制了这个程序,现在您不理解它。要学习编程,最好自己解决练习题。
  • 'highestmark` 被初始化为可能的最低值。任何高于highestmark 的标记都将上升if(mark&gt;highestmark) highestmark = mark;

标签: java jcreator


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    为了便于阅读,我对您的代码进行了一些修改,并添加了一些关于它在做什么的 cmets。我也更改了变量名和声明。有关逻辑,请直接在代码中查看 cmets。至于用于计算平均值的除法中数字末尾的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));
    }
    

    【讨论】:

    • 感谢您的精彩解释
    • 不客气。如果你觉得够了,可以考虑接受。如果它缺少某些东西,请告诉我,我会继续扩展。
    猜你喜欢
    • 2016-02-27
    • 2023-03-19
    • 2011-01-23
    • 1970-01-01
    • 2016-09-25
    • 2017-05-17
    相关资源
    最近更新 更多