/** * CS7 * This program plays a simple game with the user. It asks * them for pairs of numbers until they are able to guess * the operation correctly. The main steps are: * * while (guess is wrong) { * get input number * calculate real answer * get answer guess * if (incorrect) * print bad job message * } * print good job message */ import tio.*; // use the package tio class GuessOp { public static void main (String[] args) { double start=0,guess=0,answer=1; // guess != answer while (guess != answer) { // this is false when they win System.out.print("Enter an integer: "); start = Console.in.readInt(); answer = start * 2 - 6; // secret formula, user is trying // to infer this based on input/output System.out.print("Enter your answer: "); guess = Console.in.readInt(); // tell them they are wrong if they are if (guess != answer) System.out.println("Sorry Charlie. Try it again."); } // the loop is over, so they found the right number System.out.println("Nicely done. " + "The formula was f(x) = 2x - 6"); } } /******** SAMPLE OUTPUT: (9) unixs2 $ java GuessOp Enter an integer: 3 Enter your answer: 8 Sorry Charlie. Try it again. Enter an integer: 5 Enter your answer: 4 Nicely done. The formula was f(x) = 2x - 6 (10) unixs2 $ *********/