Welcome to Dream.In.Code
Become a Java Expert!

Join 150,057 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,738 people online right now. Registration is fast and FREE... Join Now!




For Loop Help!

 
Reply to this topicStart new topic

For Loop Help!

freshoreo
12 Jun, 2008 - 03:14 PM
Post #1

New D.I.C Head
*

Joined: 12 Jun, 2008
Posts: 11

I am trying to code a Test Score Application and I want the user to define how many test scores they are going to enter before they start. I can get the For Loop to go into effect, but I can't break out of the loop and print the final results. There is more I want to do after that but I will try to figure that part out on my own.

CODE

import java.util.*;
import java.text.NumberFormat;

public class EnhancedTestScoreApp
{
    public static void main(String[] args)
    {
        System.out.println("Please enter test scores that range from 0 to 100.");
        System.out.println("To end the program enter 999.");
        System.out.println();

        int scoreTotal = 0;
        int scoreCount = 0;
        int testScore = 0;
          int maxScore = 0;
          int minScore = 100;
          Scanner sc = new Scanner(System.in);

          do
        {    
                  System.out.print("Enter number of test scores to be entered: ");
                  int testNumber = sc.nextInt();
                    
                    for (int i = 1; i <= testNumber; i++)
                    {
                         System.out.print("Enter score " + i + ": ");
                         testScore = sc.nextInt();             
                   }
                                    
                if (testScore <= 100)
                {
                    scoreCount +=  1;
                   scoreTotal +=  testScore;
                if (testScore > maxScore)
                {
                    maxScore = testScore;
                }
                if (testScore < minScore)
                {
                    minScore = testScore;
                }
            }
                // error message if entry above 100 or below 999    
                else if (testScore <=998)
                {
                    System.out.println ("Invalid entry, not counted");
                }            
            }
            while (testScore <= 998);
            
        
        double averageScore = (double) scoreTotal / (double) scoreCount;
        NumberFormat number = NumberFormat.getNumberInstance();
        number.setMaximumFractionDigits(1);
        String message = "\n" +
                         "Score count:   " + scoreCount + "\n"
                       + "Score total:   " + scoreTotal + "\n"
                       + "Average score: " + number.format(averageScore) + "\n"
                            + "Highest test score: " + maxScore + "\n"
                            + "Lowest test score: " + minScore + "\n";
        System.out.println(message);
    }
}


Thank you!!
User is offlineProfile CardPM
+Quote Post

pbl
RE: For Loop Help!
12 Jun, 2008 - 03:53 PM
Post #2

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
CODE

for (int i = 1; i <= testNumber; i++)
{
       System.out.print("Enter score " + i + ": ");
       testScore = sc.nextInt();              
}     // <-----------------


You are reading testNumber times the score and put their values into testScore
You then exit the loop and continue your testing with only the last value read.

You have to remove the closing bracket of your for loop and bring it down just befor the while statement



User is online!Profile CardPM
+Quote Post

freshoreo
RE: For Loop Help!
12 Jun, 2008 - 03:59 PM
Post #3

New D.I.C Head
*

Joined: 12 Jun, 2008
Posts: 11

QUOTE(pbl @ 12 Jun, 2008 - 04:53 PM) *

CODE

for (int i = 1; i <= testNumber; i++)
{
       System.out.print("Enter score " + i + ": ");
       testScore = sc.nextInt();              
}     // <-----------------


You are reading testNumber times the score and put their values into testScore
You then exit the loop and continue your testing with only the last value read.

You have to remove the closing bracket of your for loop and bring it down just befor the while statement



If I put it after the while statement it won't compile. I'm guessing because it is a do-while statement and the while should be last. And if I put it right before the while statement the same thing happens. Like this:
CODE

Enter number of test scores to be entered: 3
Enter score 1: 5
Enter score 2: 5
Enter score 3: 5
Enter number of test scores to be entered:


It just keeps looping back.
User is offlineProfile CardPM
+Quote Post

pbl
RE: For Loop Help!
12 Jun, 2008 - 04:10 PM
Post #4

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
I said "before"

See what is your problem you will exit the do while loop only when the user enter 999

But you prompt for the number of tests to enter and read only that number of tests
So when the user is supposed to enter the 999 ?

So use one of the two methods but not both

Your do while is kind of useless if you prompt for the number of test to enter

That one will work

java

import java.util.*;
import java.text.NumberFormat;

public class EnhancedTestScoreApp
{
public static void main(String[] args)
{
int scoreTotal = 0;
int scoreCount = 0;
int testScore = 0;
int maxScore = 0;
int minScore = 100;
Scanner sc = new Scanner(System.in);

System.out.print("Enter number of test scores to be entered: ");
int testNumber = sc.nextInt();

for (int i = 1; i <= testNumber; i++)
{
System.out.print("Enter score " + i + ": ");
testScore = sc.nextInt();


if (testScore <= 100)
{
scoreCount += 1;
scoreTotal += testScore;
if (testScore > maxScore)
{
maxScore = testScore;
}
if (testScore < minScore)
{
minScore = testScore;
}
}
// error message if entry above 100 or below 999
else if (testScore <=998)
{
System.out.println ("Invalid entry, not counted");
}
}


double averageScore = (double) scoreTotal / (double) scoreCount;
NumberFormat number = NumberFormat.getNumberInstance();
number.setMaximumFractionDigits(1);
String message = "\n" +
"Score count: " + scoreCount + "\n"
+ "Score total: " + scoreTotal + "\n"
+ "Average score: " + number.format(averageScore) + "\n"
+ "Highest test score: " + maxScore + "\n"
+ "Lowest test score: " + minScore + "\n";
System.out.println(message);
}
}

User is online!Profile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 10:30PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month