Results 1 to 11 of 11
Like Tree1Likes
  • 1 Post By Junky

Thread: For Loop

  1. #1
    ttime1117 is offline Member
    Join Date
    Oct 2011
    Posts
    5
    Rep Power
    0

    Default For Loop

    Hello. I'm somewhat new to Java, and I need help writing a program. I would really appreciate it if someone could help me.

    Use a for loop to find the two highest test scores on a test and to identify the students who made those scores.
    Prompt the user for the number of test grades that will be entered. Then use the for loop to read the students' names and scores.
    (Prompt for each name and score.)

    Display the names and scores of the two students with the highest grades in a user-friendly manner.

    I'm pretty lost on what to do, so any help would be great! Thanks!

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,526
    Blog Entries
    7
    Rep Power
    20

    Default Re: For Loop

    Forget about that silly computer for a moment; think about students and their score passing by, slowly and one by one; you have a pencil and paper and you have to write down the two students and their score which are the highest score in the list passing by. You can't call them back after they've passed by. How would you do it? You're allowed to write down whatever you want and how much you want to write down. After the last student has gone you should be able to cough up the two students with the highest scores immediately.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    ttime1117 is offline Member
    Join Date
    Oct 2011
    Posts
    5
    Rep Power
    0

    Default Re: For Loop

    I'm not really following that too well...I kinda know the order in which to do it. I'm just lost on the coding side of things.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: For Loop

    Quote Originally Posted by ttime1117 View Post
    I'm not really following that too well...I kinda know the order in which to do it. I'm just lost on the coding side of things.
    Then write down the logical steps on how you'd solve this and post these steps here.

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: For Loop

    Quote Originally Posted by ttime1117 View Post
    I kinda know the order in which to do it.
    This is known as the algorithm. Developing the algorithm would be the hardest task. Once you have the algorithm clear in your head then converting it to code is a simple task. You should know all the basic building blocks: class, method, variable, if statement, loop etc. All you have to do when writing code is to pick the correct building blocks and put them in the correct order so that the algorithm is satisfied. If you still maintain that you do not know how to write the code then either programming is not for you and you should drop the course or you should go back and review all the material you have covered so far.
    Fubarable likes this.

  6. #6
    ttime1117 is offline Member
    Join Date
    Oct 2011
    Posts
    5
    Rep Power
    0

    Default Re: For Loop

    1st: Ask the user for the number of grades that are going to be entered

    2nd: Start the for loop, and in the for loop, ask the user for each student's name and score.

    3rd: Somehow save the 2 highest scores that were entered as they are being entered.

    4th: Display the 2 students' names and scores.

    Like I know what I'm trying to do, just not how to code it correctly. I'm quite new to this, so I apologize if I'm not following you guys too well.

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: For Loop

    Quote Originally Posted by ttime1117 View Post
    1st: Ask the user for the number of grades that are going to be entered
    OK, so how do you plan on doing that? How can you ask the user anything? What happens when the user enters a response?

    This is the first step you need to take. Write a program that only does this: prompts user to enter something, read input, store input. Once you have done that compile your code and test it thoroughly. Not once! Not twice! Keep repeating with different inputs until you are 100% satisified that it works. Then get someone else to test it because 9 times out of 10 they will think of someway to crash your program.

    Once you have completed this you can move onto the next step. etc.

  8. #8
    ttime1117 is offline Member
    Join Date
    Oct 2011
    Posts
    5
    Rep Power
    0

    Default Re: For Loop

    I've done that. I don't know how to put that into the for loop though. Like when I start the loop.

  9. #9
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: For Loop

    Seriously dude make an attempt. Nothing will happen if you write code that is wrong. Once again write some code (just a loop), compile and test it. We are not here to hold your hand.

  10. #10
    ttime1117 is offline Member
    Join Date
    Oct 2011
    Posts
    5
    Rep Power
    0

    Default Re: For Loop

    I've been attempting it over and over again. It's just that I'm having trouble starting the loop. I don't properly understand what to put for the 3 condition inside the opening of the loop. That's mainly where I'm lost.

  11. #11
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: For Loop

    How about you post your code so we can see what you so far instead of making us guess.

Similar Threads

  1. Replies: 1
    Last Post: 10-01-2011, 02:12 AM
  2. JTextField loop 2x for-loop WEIRD!
    By Streetproject in forum AWT / Swing
    Replies: 2
    Last Post: 02-16-2011, 05:46 PM
  3. Replies: 5
    Last Post: 04-30-2010, 05:05 PM
  4. A loop that doesn't loop
    By MichYer in forum New To Java
    Replies: 2
    Last Post: 07-30-2007, 08:44 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •