0% found this document useful (0 votes)
7 views

annotated-GuessingGame.java

Uploaded by

mgkoscheev
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

annotated-GuessingGame.java

Uploaded by

mgkoscheev
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

/**

* public class GuessingGame


*
* 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");
}

public String toString()


{
String output="Upper limit : "+upperBound;
return output;
}
}

You might also like