//******************************************************************** // WinPercentage.java Author: Lewis/Loftus // // Demonstrates the use of a while loop for input validation. //******************************************************************** import java.text.NumberFormat; import cs1.Keyboard; public class WinPercentage { //----------------------------------------------------------------- // Computes the percentage of games won by a team. //----------------------------------------------------------------- public static void main (String[] args) { final int NUM_GAMES = 12; int won; double ratio; System.out.print ("Enter the number of games won (0 to " + NUM_GAMES + "): "); won = Keyboard.readInt(); while (won < 0 || won > NUM_GAMES) { System.out.print ("Invalid input. Please reenter: "); won = Keyboard.readInt(); } ratio = (double)won / NUM_GAMES; NumberFormat fmt = NumberFormat.getPercentInstance(); System.out.println (); System.out.println ("Winning percentage: " + fmt.format(ratio)); } }