While Loop Assistance

I have a question about While Loops... can you have a complex boolean expression in a while loop?

I wrote a program a while back that added up golf scores and did random functions to the scores you put in. When I wrote it, it allows the user to enter in an indefinite number of scores, but allows the user to enter "-1" to terminate the while loop. I'd like the user to still have that option, but I'd like to limit the amount of entires that they can put in to only 10 entires... I tried using this expression to replace the while loop below:while ((golfScore!= -1) || (rounds == 10))

but that doesn't seem to work.

Any help here is appreciated. Here is my complete code. I want to have the code doe everything it does below, but instead of an indefinite amount of entries, I want to limit it to 10. Thank you in advance.

import java.util.*;

import java.text.DecimalFormat;

/*** Import Decimal Formating Class to format scores average. ***/

publicclass GolfXXX

{

publicstaticvoid golfScores()/*Method for data*/

{

/*Variables*/

int rounds = 0;

int golfScoreAverage = 0;

int golfScoreTotal = 0;

int golfScore;

Scanner scannerObject =new Scanner(System.in);

/*Description of program for user.*/

System.out.println("This program will calculate your average");

System.out.println("score for an indefinite number of");

System.out.println("rounds of golf.\n");

System.out.println("Enter your scores when prompted.");

System.out.println("Enter -1 when you are done entering scores.");

System.out.println("\n\n");

System.out.println("Enter your score for each round.");

System.out.println("Press enter after each score.\n");

golfScore = scannerObject.nextInt();

/** While statement to create loop for user. **/

while (golfScore!= -1)/*While loop to enter. Enter -1 to exit */

{

golfScoreTotal += golfScore;/*Sums all the scores entered.*/

rounds ++;/* Counts the number of golf scores entered */

System.out.println("Enter another score or -1 to end.");

golfScore = scannerObject.nextInt();

}/*End of while loop.*/

System.out.println("\n");

golfScoreAverage = golfScoreTotal / rounds;/*Calculates average score.*/

DecimalFormat twoDigits =new DecimalFormat("0.00");

/** Create new DecimalFormat object named twoDigits **/

/*Displays number ofrounds, total score and average score.*/

System.out.println("You entered " + rounds +" rounds of golf.");

System.out.println("The sum of your scores is " + golfScoreTotal);

System.out.println("Your average score is " + twoDigits.format(golfScoreAverage));

System.out.println("\n\n");

/*If statements used to give advise concerning your golf ability.*/

if (golfScoreAverage <= 70)

{

System.out.println("Give up Java programing and join the PGA Tour.");

System.out.println("\n");

}

/*&& allows you to set a range of for variables being compared.*/

if ((golfScoreAverage > 70) && (golfScoreAverage <= 85))

{

System.out.println("You proabably spend too much time golfing.");

System.out.println("\n");

}

if ((golfScoreAverage > 85) && (golfScoreAverage <= 95))

{

System.out.println("You are better than 90% of the people who golf.");

System.out.println("\n");

}

if ((golfScoreAverage >95) && (golfScoreAverage <= 115))

{

System.out.println("You probably need some golf lessons.");

System.out.println("\n");

}

if (golfScoreAverage > 115)

{

System.out.println("You should consider replacing golf with tennis.");

System.out.println("\n");

}

}/*End of method for data.*/

publicstaticvoid main(String[] args)

{/*Main method.*/

GolfXXX.golfScores();

}/*End of main method.*/

}/*End of class GolfXXX.*/

[6565 byte] By [TTierno2a] at [2007-11-26 19:56:52]
# 1
while ((golfScore!= -1) || (rounds == 10))//Should Bewhile((golfScore != -1) && (rounds < 10))~Tim
SomeoneElsea at 2007-7-9 22:51:16 > top of Java-index,Java Essentials,New To Java...
# 2
Perhaps your test should read:while ((golfScore!= -1) && (rounds < 10))
ProggerFromKupfera at 2007-7-9 22:51:16 > top of Java-index,Java Essentials,New To Java...
# 3
Sorry, SomeoneElse, I'm always a little bit slow...
ProggerFromKupfera at 2007-7-9 22:51:16 > top of Java-index,Java Essentials,New To Java...
# 4

Well that definitely works, but one more question regarding the use of the suggested code. Although the calculations work correctly when adding up the scores and the amount of rounds, using the aforementioned code with the suggested modification, the code still prompts me for an 11th score, even though it doesn't actually count it...

Is there a way to get rid of this?

TTierno2a at 2007-7-9 22:51:16 > top of Java-index,Java Essentials,New To Java...
# 5
You have a loop that looks like this:while (condition) { Use data Prompt for more data}So do you see why you get a prompt asking for more data that is never used? Does that suggest a change you could make?
DrClapa at 2007-7-9 22:51:16 > top of Java-index,Java Essentials,New To Java...
# 6

I have tried just about everything I learned in the short 7 weeks in class.

First I assumed from your advice that I needed to prompt for more data before I use the data, but this doesn't make sense because I'd erase the value in golfScore before I added it to golfScoreTotal.

Then I thought that the first golfScore = scannerObject.nextInt();

didn't need to be there, but then I run into problems with the round++;

as well as the golfScoreTotal.

I thought maybe using a do-while loop, I could change the order of the code, but essentially I got the same data back.

I just don't know....

TTierno2a at 2007-7-9 22:51:16 > top of Java-index,Java Essentials,New To Java...
# 7

There is one trick: You can use the Scanner in the while condition. So instead of:

golfScore = scannerObject.nextInt();

while(( golfScore!= -1) && ( rounds < 10))

{ ...

golfScore = scannerObject.nextInt();

}

you can simplify it:

while((( golfScore = scannerObject.nextInt())!= -1) && ( rounds < 10))

{ ...

}

bytetrona at 2007-7-9 22:51:16 > top of Java-index,Java Essentials,New To Java...
# 8

Hi,

Since you are already reading one value before getting into the loop, you should execute the loop at most 9 times. So you have to initialize "rounds" variable to 1:

int rounds = 1;

You therefore wouldn't have more than 10 prompts.

Regards,

Kumar.

kumar_iyera at 2007-7-9 22:51:17 > top of Java-index,Java Essentials,New To Java...
# 9

Neither of the previous suggestions work properly. They produce the same output, but I ended up fixing it myself based on DrClap's advice... Here's how I did it:

OLD CODE:

golfScore = scannerObject.nextInt();

/** While statement to create loop for user. **/

while (golfScore!= -1) /*While loop to enter. Enter -1 to exit */

{

golfScoreTotal += golfScore; /*Sums all the scores entered.*/

rounds ++; /* Counts the number of golf scores entered */

System.out.println("Enter another score or -1 to end.");

golfScore = scannerObject.nextInt();

}/*End of while loop.*/

I ended up removing the first scanner statement completely and just put it into the loop FIRST. That way, the first thing it does is get the data and THEN check it.

NEW CODE:

/** While statement to create loop for user. **/

while ((golfScore!= -1) && (rounds < 10)) /*While loop to enter. Enter -1 to exit */

{

golfScore = scannerObject.nextInt();

golfScoreTotal += golfScore; /*Sums all the scores entered.*/

rounds ++; /* Counts the number of golf scores entered */

System.out.println("Enter another score or -1 to end.");

}/*End of while loop.*/

Although the "Dr" may not ever return to this thread, I appreciate the guidance...

TTierno2a at 2007-7-9 22:51:17 > top of Java-index,Java Essentials,New To Java...
# 10
Oh, and of course I had to initialize the golfScore variable to zero in the variable "area", since the previous initialization statement (1st Scanner) was removed...
TTierno2a at 2007-7-9 22:51:17 > top of Java-index,Java Essentials,New To Java...