vendredi 29 janvier 2016

Java finding the minimum value from Scanner input always resulting in zero

This is my first Java program. It is supposed to allow a user to enter int grades between 0 and 100. If the user enters a negative value, data entry ceases and statistics are displayed. I cannot incorporate static methods, math, or arrays.

I am having an issue with finding the minimum grade, minGrade. Regardless of what values are entered when the program is running, minGrade always results in zero. I have been tinkering with this for a while now to no avail.

The other issue I am having is that when I run the program, and I enter a bunch of int, but then enter some alphabet letters to test the error-checking, the program parses the user twice, instead of once.

"Please enter a numeric grade between 0 and 100, inclusive, and press Enter:"

The respective code is:

import java.util.Scanner;


public class Grades {

    public static void main(String[] args) 
    {
        Scanner keyboard = new Scanner(System.in);

        System.out.println("Please enter the Course Code and and press Enter:");
        String courseCode = keyboard.nextLine();
        System.out.println("You entered: " + courseCode + "\n");

        int grade = 0;

        int numberOfGrades = 0;
        int maxGrade = 0;
        int minGrade =0;
        double avgGrade = 0;
        int sumGrades = 0;

        int sentinel = 0;



        do

        {


                **System.out.println("Please enter a numeric grade between 0 and 100, inclusive, and press Enter:");**
                while(!keyboard.hasNextInt())
                {

                    System.out.println("Please enter a numeric grade between 0 and 100, inclusive, and press Enter:");
                    keyboard.nextLine();

                }

                grade = keyboard.nextInt();
                if((grade <=100) && (grade >= 0))
                {
                    numberOfGrades++;
                    sumGrades += grade;
                    sentinel = 0;

                    if(maxGrade < grade)
                        maxGrade = grade;

                    **if(minGrade > grade)
                        minGrade= grade;**
                }
                else if(grade > 100)
                {

                    System.out.println("The entered number was greater than 100. Please enter a number between 0 and 100 inclusive, "
                            + "or input a negative number to exit Grade entry");
                    sentinel = 0;
                }
                else if(grade <0)
                {
                    grade = 0;//maybe?
                    sentinel = -1;

                }

        }
        while((grade >100) || (sentinel == 0));


        avgGrade = (sumGrades/numberOfGrades);



        System.out.println("You entered: " + "\ngrade: " + grade + "\n" + "sentinel: "+ sentinel +
                "\nSum of Grades: " + sumGrades +"\nNumber of Grades: "+ numberOfGrades +"\nAverage Grades: " + avgGrade
                + "\nMaxium Grade: "+ maxGrade+"\nMinimum Grade: "+minGrade);
    }

}

Any input on this, the form of my code for my first java program, or anything else would be greatly appreciated.

The last issue I am having is that the average grade always has a tenth place of zero. How can I get the tenth place to not be zero and the actual average amount?

Aucun commentaire:

Enregistrer un commentaire