#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    7
    Rep Power
    0

    Result is not correct*answered*


    I am working on this for homework and what I need the program to do is display the score and name of the person that has the highest score, the problem is that no matter what the last score is (highest, lowest or in between) that is what gets shown. I think I might be missing something, but I'm not sure what that could be. Also, we have not covered lists or arrays in this class yet, we are supposed to do this without a list or array. Any help with this would be greatly appreciated.

    Code:
     public static void main(String[] args) 
        {
            // variables
            Scanner input = new Scanner(System.in);
            int count = 0;
            int numStudents;
            double grade = 0, highestGrade = 0;
            String name = "", highName = "", output="";
            
            String numGrades =
                    JOptionPane.showInputDialog
                    ("How many student grades are you entering: ");
            numStudents = Integer.parseInt(numGrades);
            
            //for(int count = 0; count < numStudents; count++)
            while(count < numStudents)
            {
            // prompt for the user to enter grades
            String inputName =
                    JOptionPane.showInputDialog("Enter a student name: ");
            name = inputName;
            //name = input.next(inputName);
            
            String inputGrade = 
                    JOptionPane.showInputDialog("What is that students grade: ");
            grade = Double.parseDouble(inputGrade);
            //grade = input.nextDouble();
            
            count++; 
            if(grade > highestGrade)
            {
                highName = name;
                highestGrade = grade;
            }
            else 
            {
                continue;
            }        
            }
            JOptionPane.showMessageDialog
                    (null, "The student with the highest score is " + highName + 
                    " with a grade of " + highestGrade);           
        }
    Last edited by crazyjdog; January 29th, 2013 at 03:07 PM. Reason: Found the answer for myself.
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    Where does the code print out the value of the highest grade?

    Where does the code save the value of the highest grade?

    What are the variables: grade and highestGrade used for?
    What data is saved in each of them?
    Last edited by NormR; January 29th, 2013 at 03:03 PM.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    7
    Rep Power
    0
    I actually got it fixed, but as for the variables; grade is the input from the user and highestGrade is the value that is assigned to the highest grade. Also, there is no need to store anything as the if statement checks if the new input is higher and if not throws it away. Now if I were making this in a real world setting I would probably use a list or array to store the information for later use, but since this is a homework assignment and the instructor for the class has not "taught" us about lists or arrays we were not to use them.
    Last edited by crazyjdog; January 29th, 2013 at 03:19 PM. Reason: left out information

IMN logo majestic logo threadwatch logo seochat tools logo