0

I´m really new to java and GUI´s but I´m trying to implement the "guess the number game" to an app. I want to change the max and min variables here n = Integer.parseInt(JOptionPane.showInputDialog ("Choose a number between [%d,%d]:"), min, max); to make the game easier. Obviously this doesn´t work, but it´s the end goal. Any advice? Thanks

import javax.swing.JOptionPane;
import javax.swing.JTextField;

public class guessTheNumber {
    public static void main(String[] args) {
        int nAttempts= 0, min = 0, max = 100;
        int n = 0, nRandom = (int)( Math.random() * (( max - min ) + 1 )) + min;
        do
        {
            nAttempts++;
            n = Integer.parseInt(JOptionPane.showInputDialog ("Choose a number between [%d,%d]:"), min, max);
            
            if ( n != nRandom)
            {
                if( n > nRandom )
                {
                    JTextField.
                    JOptionPane.showMessageDialog(null, "Too High","Wrong Number", JOptionPane.INFORMATION_MESSAGE);
                }
    
                if( n < nRandom )
                {
                    JOptionPane.showMessageDialog(null, "Too Low","Wrong Number", JOptionPane.INFORMATION_MESSAGE);
                }
            }
        } while ( n != nRandom);
        JOptionPane.showMessageDialog(null, "Congrats!! Attempts = "+ nAttempts,"Level finished", JOptionPane.WARNING_MESSAGE);
    }
}
LASO
  • 1
  • 4

1 Answers1

0

As per my comment, you might want to take a look at String.format

Disclaimer: I haven't tested the code, but it should work OK.

import javax.swing.JOptionPane;
import javax.swing.JTextField;

public class guessTheNumber {
    public static void main(String[] args) {
        int nAttempts= 0, min = 0, max = 100;
        int n = 0, nRandom = (int)( Math.random() * (( max - min ) + 1 )) + min;
        do
        {
            nAttempts++;

//change is on the following line

            n = Integer.parseInt(JOptionPane.showInputDialog (String.format("Choose a number between [%d,%d]:", min, max)));
            
            if ( n != nRandom)
            {
                if( n > nRandom )
                {
                    JTextField.
                    JOptionPane.showMessageDialog(null, "Too High","Wrong Number", JOptionPane.INFORMATION_MESSAGE);
                }
    
                if( n < nRandom )
                {
                    JOptionPane.showMessageDialog(null, "Too Low","Wrong Number", JOptionPane.INFORMATION_MESSAGE);
                }
            }
        } while ( n != nRandom);
        JOptionPane.showMessageDialog(null, "Congrats!! Attempts = "+ nAttempts,"Level finished", JOptionPane.WARNING_MESSAGE);
    }
}
ShadowFlame
  • 2,996
  • 5
  • 26
  • 40