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.*/

