考试分数在1到100之间 import java.util.Scanner; 公开课练习1{ 公共静态void main(字符串参数[]){ int i=1,mark,TotalMark=0,highestmark=0; 扫描仪输入=新扫描仪(System.in); 做{ 系统输出打印(“请输入学生“+i+”:)的分数; mark=input.nextInt(); while(mark100) { 系统输出打印(“Invaild!请输入学生“+i+”:)的分数; mark=input.nextInt(); } 如果(标记>最高标记) 最高分数=分数; 总分+=总分; System.out.println(“\n”); i++; }当(i

考试分数在1到100之间 import java.util.Scanner; 公开课练习1{ 公共静态void main(字符串参数[]){ int i=1,mark,TotalMark=0,highestmark=0; 扫描仪输入=新扫描仪(System.in); 做{ 系统输出打印(“请输入学生“+i+”:)的分数; mark=input.nextInt(); while(mark100) { 系统输出打印(“Invaild!请输入学生“+i+”:)的分数; mark=input.nextInt(); } 如果(标记>最高标记) 最高分数=分数; 总分+=总分; System.out.println(“\n”); i++; }当(i,java,jcreator,Java,Jcreator,所以最高分数被初始化为0,因为任何分数都会高于此值。这意味着通过循环的第一次最高分数将被设置为第一个分数。如果它被初始化为1到100之间的某个数字,如果所有学生分数都低于最高分数的初始值,则可能会导致错误的输出。甚至可能是be设置为-1,因为0是有效标记 至于最后一条语句,它使用5f,因为这会将值强制转换为除法所需的浮点值。如果两个值都是整数,java将执行整数除法,而这不是您在本例中所希望的。为了可读性,我对代码做了一些修改,并添加了一些注释。我更改了变量名称和声明。请直接在代码中查看有关逻辑

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


至于最后一条语句,它使用5f,因为这会将值强制转换为除法所需的浮点值。如果两个值都是整数,java将执行整数除法,而这不是您在本例中所希望的。

为了可读性,我对代码做了一些修改,并添加了一些注释。我更改了变量名称和声明。请直接在代码中查看有关逻辑的注释。对于计算平均值的除法中数字末尾的
f
,如果缺少该值,则会出现
java.util.IllegalFormatConversionException
异常,因为5的数据类型为int(而不是float)。在下面的代码中,文字(5)已替换为常量,
TOTAL\u STUDENTS
,该常量被转换为浮点数(请参见
(float)
)以执行除法

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;
//这两个常量用于标记(it)的边界
//必须介于0和100之间)
私有静态最终整数最低标记可能=0;
私有静态最终整数最高分数可能=100;
公共静态void main(字符串参数[]){
//您将当前学生设置为1,并在循环结束时增加一个值
//但是,如果在do循环的开始处增加,它会读得更好
int currentStudent=0;
//输入中尚未给出任何分数
int totalMarks=0;
//在此点(程序开始)初始化最高标记
//由于尚未收集任何数据,且自所有标记
//必须大于或等于0,这是最低起点
//标记可能
int highestMark=可能的最低标记;
扫描仪输入=新扫描仪(System.in);
做{
当前学生++;
系统输出打印(“请输入学生“+currentStudent+”:)的分数;
//获取从键盘输入的下一个整数
int currentStudentMark=input.nextInt();
//验证它——如果它不在可接受的范围内,请重试
而(currentStudentMark<最低分数| | currentStudentMark>最高分数|可能){
系统输出打印(“INVIILD!请输入学生“+currentStudent+”:)的标记;
currentStudentMark=input.nextInt();
}
//如果刚刚输入的标记大于
//已存储最高标记,将最高标记更新为
//等于刚才输入的分数
如果(当前学生分数>最高分数){
最高分数=当前学生分数;
}
//将当前学生分数添加到计算的总分数中
总分数+=当前学生分数;
System.out.println(“\n”);
}而(当前学生<总学生);

//你的while条件是请在每篇文章中问一个问题,并花更多的时间解释你自己。你说如果没有
f
“错误输出”但是你没有说那是什么错误,你关于
highestmark的问题对我来说也很不清楚。似乎你从某个地方复制了这个程序,现在你不理解它。要学习编程,最好是你自己尝试解决练习。“highestmark”被初始化为尽可能低的值。任何高于<代码> HealthMark < /代码>,将由代码> >(标记>高级标记)HeavestMark=Mark;< /COD> >谢谢您的友好解释。如果您觉得它足够了,请考虑接受。如果它丢失了什么,请让我知道,我将扩展它。
// 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));
}