for this im supposed to create a program that randomly chooses a number between 1-100 for the user to guess and if the user is wrong then ask for another guess
then wen the user gets it right, to then state the amount of tries and to ask if the user wants to play again
heres the code that my friend and I have com up with...
import java.util.Scanner;
import java.util.Random;
import cs1.Keyboard;
public class Guessing
{
public static void main (String[] args)
{
final int max=10;
int answer=0;
int guess=0;
int reportCount=0;
int decsion;
Scanner scan = new Scanner (System.in);
Random generator = new Random();
answer = generator.nextInt(max) + 1;
System.out.println ("I'm thinking of a number between 1 and "+max+".");
System.out.print ("Guess what it is: ");
guess = scan.nextInt();
while(true){
System.out.print ("Sorry wrong answer please guess again: ");
guess = scan.nextInt();
if (guess==answer){
System.out.println("You got it! Good Guessing!");
break; }
else
System.out.print ("");}
for(guess=1;guess<=500;guess++)
reportCount=reportCount+guess;
System.out.println(reportCount);
{
String decision,input;
decision=new String("again");
System.out.println("Type the word:'again' if you want to play another round. ");
input = Keyboard.readString();
while(input.compareTo(decision)!=0)
{
System.out.println("I did not understand you please type again.");
input = Keyboard.readString();
}
}
}
wat i need help on is how to count the amount of tries of the user and how to make the code restart at the top for another game
why is it so complex? O_o
use math.Random()*100 and store it into an int. Then, compare it with the guess. If wrong, then countwrong++. If right, then done
Actually, you'll need 1 + 100 * Math.random() in order to go from 1 to 100.
As for the repetition, just include a question at the end, like "Do you want to play again?", and if the user types "Yes", repeat, or else close.
yup his right, same here you can use 1 + 100 * Math.random() for randomizing your guessing game program.
[URL=http://www.studyinaustralia.com]mba education in australia[/URL]
hey, there's a way to write this way more easily. You don't need the random or keyboard classes. Start a while loop while the guess doesn't equal the number. Number should equal (-1 + Math.random() * 101);
Then keep making if statements for if it is above the number or below so it will give the player hints. Lastly, report the number and how many guesses
Thanks a lot to introduce such a nice tricks. I like the program created by you.