/*********************** * Loopy - practice with loops * * @author Shawn Brenneman (modified by ibarland to use methods and less I/O) * @date 2019-Sep 25 ***********************/ import java.util.*; public class LoopyBefore extends Object120 { /** Return the number of rolls of 2d6 until we rolled (1,1). */ static int rollsTilSnakeEyes() { int die1; int die2; // two loop vars: //boolean __________ = ______; // a loop-control variable //int __________ = ______; // accumulator var (the pertinent info accumulated from all previous iterations) //while (_________) { die1 = randomInt(6) + 1; die2 = randomInt(6) + 1; //System.out.println("Debug: rolled " + die1 + "," + die2); //____________________________ // update accumulator var if (die1 == 1 && die2 == 1) { // update loop-control var // _______________________ // } } return -999; } /** Have a human play the hi-lo guessing game. * @return the number of guesses the human took. */ static int hiloHumanIO() { int target = randomInt(100); int guess; // We'll read input from the keyboard ("System.in"). //int _________________ // accumulator //System.out.println( "debug: target is " + target ); System.out.print( "Guess a number in [0,100): " ); guess = nextInt(); // the function's full name: Object120.nextInt while (guess != target) { // (no explicit loop-control var) // inform the user: guess is too high, or too low. guess = nextInt(); // update the loop-condition var //________________________ // update the accumulator } System.out.println( "You got it after some guesses." ); return -99; } /* Not actually unit-tests -- we can't use `assertEquals`, because the expected-outcome isn't known. */ public static void testAll() { System.out.println( "==== testing all" ); System.out.println( "#1: It took " + toString(rollsTilSnakeEyes()) + " rolls to get snakeeyes." ); System.out.println( "#2: It took " + toString(rollsTilSnakeEyes()) + " rolls to get snakeeyes." ); System.out.println( "#3: It took " + toString(rollsTilSnakeEyes()) + " rolls to get snakeeyes." ); System.out.println( "#4: It took " + toString(rollsTilSnakeEyes()) + " rolls to get snakeeyes." ); hiloHumanIO(); return; } public static void main( String[] args ) { testAll(); } }