My problem is in the guessing method, I am not sure if the way I set up playing the game again works, (don't know if entering yes will work) and I'm not sure on how to call the main method to play again, and also I'm suppose to give the user double their bet if they guess the number in half the tries (so if they ask for 4 guesses and guess the number in 2 tries their bet is doubled and is added to their amount), so I multiply bet by 2, but for some reason it doesn't get multiplied by 2, it just prints out the same amount. Extra stuff: user gets minus 10 for each guess,typing 0 ends the game, and I know I print out the number they have to guess in the beginning, just their to make sure guessing high or low works. Any help is appreciated, and sorry if my coding is bad, or it's not set up well enough, only a month into java so don't be to hard on me :)

/**
 * 
 */
package CH5;

/**
 * @author bielinskin13
 *
 */
 import java.util.Scanner;
public class test {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		int RandomNumber;
		int number = 0;
		int useramount = 500;
		int bet = 0;
		int guessamount = 0;
		int Guessing;
		int guess = 0;
		// brings the random number into RandomNumber
		RandomNumber = (int)randomnumber();
		System.out.println(RandomNumber);
		// betting
		System.out.println("You have $" + useramount + ". How much do you want to bet? ");
		Scanner Input = new Scanner (System.in);
		// bet now holds the bet amount in an integer
		bet = Input.nextInt();
		// if bet is more than useramount max bet is assigned
		if (bet > useramount){
			System.out.println("You cant bet more than you have, using max bet");
			bet = useramount;
		}
		// guessing
		System.out.println("How many tries to guess the number? No more than 8 tries: ");
		Scanner Input2 = new Scanner (System.in);
		// the guess amount is now within guessamount integer form
		guessamount = Input2.nextInt();
		// if guessamount is more than 8 then guess is assigned 8
		if (guessamount > 8){
			guessamount = 8;
			System.out.println("You cant have more than 8 guesses, using max guesses");
		}
		Guessing = guessing(RandomNumber,guess,guessamount,useramount,bet);
	}
	public static double randomnumber(){
		double number=0;
		number = Math.random() * 200 + 1;
		// converts the double number to an integer, taking off the decimal
		return((int)number);
	}
	public static int guessing(int RandomNumber, int guessamount, int guess,int useramount,int bet){
		int counter = 1;
		
		do{
		// shows user their guess number
		System.out.println("Guess #" + counter);
		Scanner Input = new Scanner(System.in);
		// guess is the number guessed
		guess = Input.nextInt();
		if ((guess > RandomNumber)&&(guess > 0))
			System.out.println("That is to high!");
		if ((guess < RandomNumber)&&(guess > 0))
			System.out.println("That is to low!");
		if (guess == RandomNumber){
			System.out.println("");
			System.out.println("YOU GOT IT RIGHT!!");
			
			// gets half the guess amount so if counter equals half the guess amount their bet is doubled
			if (counter == (guessamount / 2))
				bet = bet * 2;
			    System.out.println("You got it in half the tries!");
				useramount = useramount + bet;
				System.out.println("Your new total is " + useramount);
				System.out.println("Do you want to play again? (Yes/No)");
				Scanner Input2 = new Scanner(System.in);
				String playagain = Input2.next();
				if(playagain == "Yes")
					// need to call the main to repeat the program
				if (playagain == "No")
					System.out.println("Thank you for playing, you ended up with," + useramount);
				
					
			break;
		}
		if (guess == 0){
			System.out.println("uMAD?");
			break;
		}
		
		// counter goes up each time through the loop so guess number goes up 1
		counter = counter + 1;
		// subtracts 10 off for each guess
		useramount = useramount - 10;
		}while(guess != RandomNumber);
		return(guess);
	}
}

Recommended Answers

All 2 Replies

What you have to do is put the content in the main method in a do while loop. Define an other variable to hold the users decision. You also have to move the user input code lines in to the do while loop.

public static void main(String[] args) 
{
	int RandomNumber;
	int number = 0;
	int useramount = 500;
	int bet = 0;
	int guessamount = 0;
	int Guessing;
	int guess = 0;
	
	// New Variable
	boolean playAgain = false;
	
	do
	{
		// Rest of the code in the main method
		
		// Asking the user's choice on playing again
		System.out.println("Do you want to play again? (Yes/No)");
		String playagain = Input2.next();
		if(playagain == "Yes")
		{
			playAgain = true;
		}
		if (playagain == "No")
		{
			playAgain = false;
			System.out.println("Thank you for playing, you ended up with," + useramount);
		}
	}
	while(playAgain);
}

Thank you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.