I'm stuck with while loop on my random number guessing game

Viewed 628

Want it to loop through everything until the user puts in the right number. When the user puts in wrong number it should say "type in a different number". When the user puts in the right number it should say "congrats you won". But until then it will loop and say "type in a different number" and after 5 tries I want it to say "you failed this mission! do you want to try again?"

If they guess it on 1 try they will be giving 500 dollar and second try 400 dollar and so on until 5 tries.

import javax.swing.*;
import java.util.Random;

public class Projekt_1 {

    public static void main(String[] args) {
        JOptionPane.showMessageDialog(null, "WELCOME TO GUESS GAME!" + "\nYou gonna guess a number between 1 and 20 " + "\nYou have  5 tries to guess the number! " + "\nYou gonna get more money on less tries, highest win are 500 dollar on one try! " + "\nHOPE YOU LIKE IT :)");

        Random talet = new Random();
        int secretnumber = talet.nextInt(20) + 1;
        int tries = 0;
        int money = 600;
        String number;

        int guess;
        boolean win = false;

        while (win == false) {
            number = JOptionPane.showInputDialog("Guess a number between 1 and 20");
            guess = Integer.parseInt(number);
            tries++;

            if (guess == secretnumber) {
                win = true;
            } else if (guess > secretnumber) {
                JOptionPane.showInputDialog("Your number is to low :(" + "\nType in a higher number!");
                guess = Integer.parseInt(number);
            } else if (guess < secretnumber) {
                JOptionPane.showInputDialog("Your number is to high :(" + "\nType ina a lower number!");
                guess= Integer.parseInt(number);
            }
        }

        JOptionPane.showMessageDialog(null, "Congrats you won!" + "\nYour number was " + secretnumber + "\nit took you " + tries + "tries");
    }
}   
3 Answers
Related