Alright, yes there is a logic issue with the game flow, Its should be another get input once a random number is generated, instead of showMessage, then compare the input number to the random number, GuessedNumber should be the inputted number
I guess I would do it like this:
import javax.swing.*;
import java.util.Random;
public class Main
{
public static void main(String[] args)
{
/* Declare variables */
String MaxNumberRange;
String RunAgain;
int RangeGame;
char PlayAgain = 'y';
do {
/* First question for the user */
MaxNumberRange = JOptionPane.showInputDialog("You are going to guess a number between 1 and what number ?");
RangeGame = Integer.parseInt(MaxNumberRange); // It transforms user input to a integer
/* Creation a GuessingGame Object */
GuessingGame game;
game = new GuessingGame();
game.playGuessingGame(RangeGame); // Initialization of the Game
RunAgain = JOptionPane.showInputDialog("Would you like to play again Yes (Y) or No (N) ?");
PlayAgain = RunAgain.charAt(0);
}
while (PlayAgain == 'y' || PlayAgain == 'Y');
System.out.println("Thank you for choosing x's Game! I hope to see you again");
System.exit(0);
}
private static class GuessingGame
{
/*Declare variable*/
private int GuessedNumber;
private String Guessing;
private int ct = 1 ; //Set up counter from 1
private int random;
public void playGuessingGame(int RangeGame) {
Random randomNumber = new Random();
random = (int) randomNumber.nextInt(1000);
int TargetNumber = (random % RangeGame) + 1;
do {
String inputString = JOptionPane.showInputDialog("Please enter a number between 1 and "+RangeGame);
int GuessedNumber = (int)(Integer.parseInt(inputString));
if (GuessedNumber < TargetNumber) {
JOptionPane.showMessageDialog(null, "Too low. Try again"); // hint for the Gamer
}
if (GuessedNumber > TargetNumber) {
JOptionPane.showMessageDialog(null, "Too high. Try again");
}
if (GuessedNumber == TargetNumber) {
JOptionPane.showMessageDialog(null, "You won the Game!" + "You tried only " + ct + " times"); // Final result
break;
}
ct ++; // Keep track of the tries.
}while(true);
}
}
}