Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: loop

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default loop

    can somebody help me to solve this issue below ?

    • Using a loop, write a program that takes 10 values representing exam grades (between 0 and 100) from the keyboard and output the minimum value, maximum values, and average value of all the values entered. Your program should not accept values less than 0 or greater than 100.

    Additional notes:
    • Your program should neatly display the results. You are free to decide how you want the display to look. One possible output is shown below



    Enter ten scores between 0 and 100.
    Score 1: 45
    Score 2: 94
    Score 3: -3
    Invalid score. Please try again.
    Score 3: 85

    Score 10: 88
    Minimum score: 45
    Maximum score: 94
    Average score: 78.0


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: loop

    Please post what you've tried, and it helps to get you help if you ask a specific question about your attempt(s)

  3. #3
    Member
    Join Date
    Mar 2012
    Location
    United States
    Posts
    118
    My Mood
    Inspired
    Thanks
    1
    Thanked 33 Times in 31 Posts

    Default Re: loop

    ^---copeg got it before me
    redundant so I removed it.

  4. #4
    Junior Member
    Join Date
    Mar 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: loop

    This is it, it is incomplete because I am stuck, about the condition

    import java.util.Scanner;


    /**
    *
    * Program to demonstrate looping.
    *
    */
    public class LoopProject6 {
    public static void main(String[] args) {
    // In this section of the program we will use
    // a while loop that takes values
    // representing exam grades
    // (between 0 and 100) inclusive from the keyboard

    // for loop used since whe know how many times(10 times) we need to loop
    // We need a sum to accumulate values into.
    // And it needs to be initialized.

    int sum = 0;

    float average;
    double Max = 0;

    double Min = 101;

    Scanner console = new Scanner(System.in);
    System.out.println("Enter ten scores between 0 and 100");


    for (int score =1; score<= 10; score ++)
    {
    System.out.print("score" + score+ ":");
    score= console.nextInt();
    }

    {
    System.out.println("Invalid score.Please try again");
    }
    if (score > Minimum)
    score= Minimum;
    {
    System.out.println("Minimum score: " ) ;
    }
    if (score < Maximum)
    score= Maximum;
    {
    System.out.println("Maximum score: " );
    }
    average = sum / 10;
    {
    System.out.println("Average score: " );
    }
    }
    }
    Last edited by tamaljordan; March 9th, 2014 at 01:01 AM. Reason: Help

  5. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: loop

    Welcome to the forum! Please read this topic to learn how to post code correctly and other useful info for new members.

  6. #6
    Junior Member
    Join Date
    Mar 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: loop

     
    import java.util.Scanner;
     
     
    /**
     * 
     * Program to demonstrate looping.
     * 
     */
    public class LoopProject6 {
    public static void main(String[] args) {
       // In this section of the program we will use
             // a while loop that takes  values 
           // representing exam grades 
         // (between 0 and 100) inclusive from the keyboard
     
            // for loop used since whe know how many times(10 times) we need to loop 
            // We need a sum to accumulate values into.
           // And it needs to be initialized.
     
    int sum = 0;
     
    float average;
    double Max = 0;
     
    double Min = 101; 
     
    Scanner console = new Scanner(System.in);
    System.out.println("Enter ten scores between 0 and 100");
     
     
    for (int score =1; score<= 10; score ++)
       {
      System.out.print("score" + score+ ":");  
       score= console.nextInt();
       }
     
        {
         System.out.println("Invalid score.Please  try again");
        }
       if (score > Minimum)
           score= Minimum;
        {
          System.out.println("Minimum score: " )  ;
        }
      if (score < Maximum)
           score= Maximum;
        {
          System.out.println("Maximum score: " );
        }
        average = sum / 10;
        {
          System.out.println("Average score: "  );
        }
    }
    }


    --- Update ---

    Thanks, I posted it as you asked

  7. #7
    Member
    Join Date
    Mar 2012
    Location
    United States
    Posts
    118
    My Mood
    Inspired
    Thanks
    1
    Thanked 33 Times in 31 Posts

    Default Re: loop

    So there are a few issues I can see:
    1. The counter variable for your for loop (score) is the same variable you use to get input, rendering the counter useless.
    2. The variable score has a scope restricted to the for loop and is therefore not accessible outside of it.
    3. minimum and maximum are never declared as variables anywhere.

    I suggest getting a single portion of this assignment working first such as getting input for the 10 scores (making sure they are between 0-100). After that move onto either min score, max score, or average. If the for loop ends up giving you some troubles there are always other types of loops so keep that in mind.

    Perhaps go over this: The for Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)
    and this (just in case): The while and do-while Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)

    Forgot to mention you may want to look over java syntax rules as you have a lot of unnecessary curly braces.
    Last edited by KucerakJM; March 9th, 2014 at 09:23 AM.

Similar Threads

  1. loop once or loop multiple times
    By stanlj in forum Loops & Control Statements
    Replies: 3
    Last Post: November 7th, 2013, 02:14 PM
  2. help with when the for loop is met and i want to run the while loop again
    By m49er704 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: March 22nd, 2013, 09:03 AM
  3. For loop, the first command in the loop does not get executed the 2nd time..
    By lina_inverse in forum Loops & Control Statements
    Replies: 1
    Last Post: October 16th, 2012, 09:00 PM
  4. [SOLVED] Please help with my while loop that turned into infinite loop!
    By Hazmat210 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: March 10th, 2012, 11:22 PM
  5. Converting a while loop to a for loop and a for loop to a while loop.
    By awesom in forum Loops & Control Statements
    Replies: 3
    Last Post: February 26th, 2012, 08:57 PM