Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: error with a java game..(beginner)

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default error with a java game..(beginner)

    On my geussing game I do have 2 class (main and guessing class)
    in the first class the user chose the range of the game
    and it will have the question if he wanna play again or no...

    in the secondon class i generate the random number and the loop...I got many error...can someone help me.


    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';

    /* 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

    do {
    RunAgain = JOptionPane.showInputDialog("Would you like to play again Yes (Y) or No (N) ?");
    }
    while (PlayAgain == 'y' || PlayAgain == 'Y');

    if (RunAgain == "y")
    {
    game.playGuessingGame(RangeGame);
    }
    else
    {
    System.out.println("Thank you for choosing x's Game! I hope to see you again");
    System.exit(0);
    }
    }

    }



    import java.util.*;
    import javax.swing.*;
    public 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)
    {
    JOptionPane.showMessageDialog(null, "You are going to guess " +
    " number between 1 and what number " + RangeGame, Guessing, RangeGame);
    Random randomNumber = new Random();
    random = (int) randomNumber.nextInt(1000);
    GuessedNumber = (random % RangeGame) + 1;


    if (GuessedNumber < random)
    {
    JOptionPane.showMessageDialog(null, "Too low. Try again"); // hint for the Gamer
    }
    if (GuessedNumber > random)
    {
    JOptionPane.showMessageDialog(null, "Too high. Try again");
    }
    if (GuessedNumber == random)
    ct ++; // Keep track of the tries.
    JOptionPane.showMessageDialog(null, "You won the Game!" + "You tried only " + ct + " times"); // Final result
    }
    }


  2. #2
    Member
    Join Date
    Oct 2010
    Location
    Denver, CO
    Posts
    55
    Thanks
    1
    Thanked 30 Times in 29 Posts

    Default Re: error with a java game..(beginner)

    What error are you getting? It compiled for me, but showMessageDialog gives an error as you are passing the range instead of "MESSAGE_TYPE" as the 4th parameter, also, the do/while loop looks infinite, as I don't see where playagain is set as runagain

  3. #3
    Junior Member
    Join Date
    Nov 2010
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: error with a java game..(beginner)

    it doesn't work...you should input the range and after play the game...anyway with eclipse doesn't work

  4. #4
    Member
    Join Date
    Oct 2010
    Location
    Denver, CO
    Posts
    55
    Thanks
    1
    Thanked 30 Times in 29 Posts

    Default Re: error with a java game..(beginner)

    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);
    		}
    	}
    }
    Last edited by Zula; November 1st, 2010 at 02:12 PM.

Similar Threads

  1. Java Beginner Here!
    By j3nn42o in forum The Cafe
    Replies: 10
    Last Post: January 10th, 2011, 04:57 AM
  2. A java beginner needs a lot of help and ideas
    By vesa in forum Java Theory & Questions
    Replies: 1
    Last Post: May 24th, 2010, 09:46 PM
  3. Error in Program for Game of Craps
    By TheAsianMenace in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 23rd, 2010, 04:31 AM
  4. Beginner needs help with simple java assignment.
    By joachim89 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 6th, 2010, 07:53 PM
  5. Java Beginner
    By rannoune in forum Java Theory & Questions
    Replies: 3
    Last Post: December 25th, 2009, 03:30 AM