* * Author: Masha Koscheev * Date: 12/10/24 * Course: AP CSA * Period: 3rd * * Summary of file: * Create a guessing game */ import java.util.Scanner; import static java.lang.System.*;
public class GuessingGame
{ private int upperBound;
public GuessingGame(int stop)
{ upperBound = stop; }
public void playGame()
{ Scanner keyboard = new Scanner(System.in); int ans = 0; int count = 0; int correct = (int)(Math.random()*(upperBound)+1 );
while (ans !=correct){
out.print("Enter a number between 1 and "+upperBound+" ");
ans = keyboard.nextInt(); if (ans<1 || ans>upperBound) { out.println("Number out of range!"); } else count++; } out.println("It took "+count+" guesses to guess "+correct+"."); int perc =(int)( ((count-1)/(double)upperBound)*100 ); out.println("You guessed wrong "+perc+" percent of the time.\n"); }