// Count loops: 2/9/2k DIS // Reprompt user for input public class reprompt2 { public static void main(String[] args) { //---------------------------------------------------------------------------- // Setup //---------------------------------------------------------------------------- TokenReader in = new TokenReader(System.in); int val = 0; int count = 0; boolean test; //---------------------------------------------------------------------------- // Prompt for initial input: reprompt if outside range //---------------------------------------------------------------------------- test = false; while(!test) { System.out.print("Enter an integer between -1 and 10, inclusive: "); val = in.readInt(); if (val > 10 || val < -1 ) { System.out.println("What? Pay attention fool!"); test = false; } else { System.out.println("Thanks for following instructions."); test = true; } } //---------------------------------------------------------------------------- // Count loops except when input is zero // Reprompt user for zero: we're going to skip adding it // Stop when user enters -1 //---------------------------------------------------------------------------- while(val != -1) { // Get next input // Reprompt for zero test = false; while(!test) { System.out.print("Enter the next integer, except 0: "); val = in.readInt(); if (val == 0 ) { System.out.println("Errr, try again!"); test = false; } else { ++count; test = true; } } } //---------------------------------------------------------------------------- // Report Results //---------------------------------------------------------------------------- if(count == 0) System.out.println("No values"); else System.out.println("Count: " + count); } }