Results 1 to 5 of 5
Thread: problems with if statement
- 11-24-2010, 03:27 AM #1
Member
- Join Date
- Sep 2010
- Location
- Southwest Missouri
- Posts
- 97
- Rep Power
- 0
problems with if statement
I am attempting to write a program that prompts the user to enter the number of students and each student's name and score, and finally displays the name of the student with the highest score. However, instead of displaying the student with the highest score, the program displays the name and score of the last student input. I'd appreciate it if someone could take a look at my code and tell me where I am going wrong. Thank you.
Java Code:/* HighestScore.java * jim01 * 11/23/2010 * This program sorts inputted students names and scores and displays the * student with the highest score. */ import java.util.Scanner; public class HighestScore { public static void main(String [] args) { //create variables double highestScore = 0; double studentScore = 0; String studentName = ""; double count = 0; double totalStudents = 0; //input and processing //create new scanner object Scanner input = new Scanner(System.in); //prompt user to enter the total number of students System.out.print("Enter the total number of students: "); totalStudents = input.nextDouble(); /*Prompt user to enter the student names and scores and keep track of the number of students*/ while ( count < totalStudents ) { //Prompt user to enter student name System.out.print("Enter Student Name: "); studentName = input.next(); //Prompt user to enter student score System.out.print("Enter Student Score: "); studentScore = input.nextDouble(); count++; } if(studentScore > highestScore) { highestScore = studentScore; count++; } //output System.out.println(studentName + " has the highest score of " + highestScore); } }
- 11-24-2010, 03:33 AM #2
Well, if you hadn't already figured out, these four lines are the issue:
Java Code:if(studentScore > highestScore) { highestScore = studentScore; count++; }
Instead of checking who has the highest score, you've simply checked if the last one entered has the highest score.
What you'll want to do is, instead, put your if statement in the while loop to check each name as it's entered. If it's higher, record the name and score in your highestScore (and another variable, a String called highestStudentName) then output THOSE in your println instead.
- 11-24-2010, 04:03 AM #3
Member
- Join Date
- Sep 2010
- Location
- Southwest Missouri
- Posts
- 97
- Rep Power
- 0
OK, I see where I made the mistake of not incorporating the if statement with the while statement. However, I am confused on how to match the name of the person with the highest score with the highestStudentName variable. I tried this:
Java Code:while ( count < totalStudents ) { //Prompt user to enter student name System.out.print("Enter Student Name: "); studentName = input.next(); //Prompt user to enter student score System.out.print("Enter Student Score: "); studentScore = input.nextDouble(); count++; if(studentScore > highestScore) { highestScore = studentScore; studentName = highestStudentName; count++; } } //output System.out.println(highestStudentName + " has the highest score of " + highestScore); } }
I'm having a real hard time with if statements. I don't know what I'm missing but I can't seen to figure them out.
- 11-24-2010, 04:16 AM #4
You're actually completely correct except for two things:
1) The count++; inside the if statement shouldn't be there. You only want count to increase once, and it already does that.
2) studentName = highestStudentName; -- remember how variables work! This should be the other way around, as you want the highestStudentName to BECOME the studentName. ;)
Give that a try and see how it works for ya.
- 11-24-2010, 04:28 AM #5
Member
- Join Date
- Sep 2010
- Location
- Southwest Missouri
- Posts
- 97
- Rep Power
- 0
I completely forgot about the count. That explains why I was having problems when I combined the if statement with the while statement.
It works now. I don't know why I couldn't see that I had it backwards, especially since highestScore = studentScore was directly above it. LOL! Duh.
I hope I get the hang of these darned if statements sooner or later. They are hampering my progress and my final exam is less than a month away.
Thank you for your help.
Similar Threads
-
While statement problems
By Danieldcc in forum New To JavaReplies: 5Last Post: 10-18-2010, 04:37 PM -
problems with print statement
By soccer_kid_6 in forum New To JavaReplies: 8Last Post: 03-22-2010, 03:24 PM -
add an If Else statement and......uh????
By sonny in forum New To JavaReplies: 6Last Post: 03-04-2010, 07:57 PM -
for statement help
By helpisontheway in forum New To JavaReplies: 5Last Post: 11-14-2009, 05:14 PM -
Statement or Prepared Statement ?
By paty in forum JDBCReplies: 3Last Post: 08-01-2007, 05:45 PM
Bookmarks