In an exam mark in the range between 1 and 100

1.4k Views Asked by At
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));
    }
}

In the exam marks for 5 students and determine the highest mark achieved along with the average of the marks to the nearest whole number. These marks should all be mathematical integers (whole numbers) within the range 0 to 100.

That paragraph above is the object on this code . I'm wondering that this code is weird logically.

if(mark>highestmark) highestmark = mark; when I saw this code, I thought all Numbers can be the mark like 1,2,3 to 100 but highestmark was initialized to 0 at first. how it is logical?

and last statement is (totalmarks/5f) why should I add 'f' at the end of 5? if I do not add f,an error outputs Please answer and explain those questions for me and thank you for your help

2

There are 2 best solutions below

0
hwhite4 On

So highest mark is initialized to 0 because any mark will be higher than this. This means that the first time through the loop highestmark will be set to the first mark. If it was initialized to some number between 1 and 100 this could cause an incorrect output if all student marks were below the initial value for highestmark. It could even be set to -1 since 0 is a valid mark.

As for the last statement it uses 5f beacuse this casts the value to a float which is required for the division. If both values were integers java would perform an integer division which is not what you would want in this case.

2
geco17 On

I made some modifications to your code for readability and added some comments as to what it's doing. I changed the variable names and declaration as well. See the comments directly in the code regarding the logic. As for the f at the end of the number in your division for calculating the average, if this is missing you get a java.util.IllegalFormatConversionException exception because the data type of the 5 is int (and not float). In the code below, the literal (5) has been replaced with a constant, TOTAL_STUDENTS, which is cast to a float (see (float)) to perform the division.

// 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));
}