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: Little help with this project please

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Little help with this project please

    Hi everyone and thank you in advance for helping me with this issue. What im trying to do is, in my ending message, im trying to make it say thanks for playing... "you have won + wins" but i cant figure out how to add the +wins in there (ending message is just a copy of opening message that im changing). im rather new to java. also any suggestions on changing the code a bit to make it better is greatly appreciated.

    import javax.swing.*;
    import java.awt.Font;  // need for Font and Point Size Changes
    import java.awt.Color;  // need to change Color
     
    public class Lab2
    {
     
        static int wins = 0;
        static int losses = 0;
        static int totalPlays = 0;
     
     
    public static void main(String[] args)
    	{
    		runThis();
    		System.exit(0);
     
    	}//main
    public static void runThis()
    {
    	int userNum;
    	int compNum;
    	int value;
     
    	openingMessage();
    	getInt();
     
     
    	do
    	{
    		userNum = getInt();
     
    		compNum = rollTheDice();
     
    		compareNum (userNum, compNum);
     
    		value = JOptionPane.showConfirmDialog(null, "Continue with the game?");
     
          }
          	while (value == JOptionPane.YES_OPTION);  // do while
     
         endingMessage();
    }
     
    public static void openingMessage()
    	{
    		    String output = "";
     
    	        Font big = new Font ( "Serif", Font.BOLD, 22 );  // set up how the letters will look
     
    	        JTextArea outputArea = new JTextArea ();  // create a new JTextArea named outputArea
     
    	        outputArea.setFont(big);    // use the font stuff from above
     
    	        outputArea.setBackground ( new Color ( 0x00, 0x00, 0x80 ) );  // Navy 000080
     
    	        outputArea.setForeground ( new Color ( 0xFF, 0xF8, 0xDC ) );  //  Corn Silk  FFF8DC
     
    	        output = "Welcome to the Guess the Numbers from 1 to 6 Program \n\n"
    	        + "The computer will generate a random number from 1 to 6. \n"
    	        + "        Your task is to see if you can guess the number. \n\n"
    	        + "The game will keep track of how many games you played \n"
    	        + "and it will tell you how many wins you had \n \n"
    	        + "\t     Click OK to Begin.\n\n\t           Good Luck \n";
     
    	        outputArea.setText(output);
     
                JOptionPane.showMessageDialog(null, outputArea,
                    "The Guess the Numbers Game", JOptionPane.INFORMATION_MESSAGE);
     
    	       return;
     
          }// opening message
     
     
    public static void compareNum (int user, int comp)
    	{
     
    		if(user == comp)
    		{
    			JOptionPane.showMessageDialog (null, "You Win");
    			wins++;
    			totalPlays++;
    			System.out.println ("Wins:" + wins);
     
    		}
    		else
    	     {
    	     	JOptionPane.showMessageDialog (null, "You Lost");
    			losses++;
    			totalPlays++;
    			System.out.println ("Losses:" + losses);
     
    		}
    	}
     
    public static int getInt()
    {
          int val;
     
          while (true)  // a seemingly endless loop - uses break to get out
          { // loop until we get a valid int
     
            String s;
     
            s = JOptionPane.showInputDialog ( "Enter a number from 1 - 6:" );
     
            if (s == null)
            {
          JOptionPane.showMessageDialog(null, "Leaving program");  // exit on cancel
                  System.exit (0);
        } // cancel if
        else
              if (s.equals(""))
          {
        JOptionPane.showMessageDialog
         (null, "You must make an entry in the InputBox");
     
          } // entered nothing
     
            try
            {
                val = Integer.parseInt(s);
     
                if (val >= 1 && val <= 6)
     
                break;  // exit loop with valid int
                // could have had a variable change here to get out of loop
     
            }// try
            catch (Exception e)
            {
                JOptionPane.showMessageDialog(null, "Enter a number from 1 - 6");
     
            }// catch
          }//while
     
             return val;  //return the number to the calling method
     
      }  //end getInt
     
    public static void endingMessage()
    	{
    		    String output = "";
     
    	        Font big = new Font ( "Serif", Font.BOLD, 22 );  // set up how the letters will look
     
    	        JTextArea outputArea = new JTextArea ();  // create a new JTextArea named outputArea
     
    	        outputArea.setFont(big);    // use the font stuff from above
     
    	        outputArea.setBackground ( new Color ( 0x00, 0x00, 0x80 ) );  // Navy 000080
     
    	        outputArea.setForeground ( new Color ( 0xFF, 0xF8, 0xDC ) );  //  Corn Silk  FFF8DC
     
    	        output = "Thanks for playing the Guess the Numbers from 1 to 6 Program \n\n"
    	        + "You have won. \n"
    	        + "        Your task is to see if you can guess the number. \n\n"
    	        + "The game will keep track of how many games you played \n"
    	        + "and it will tell you how many wins you had \n \n"
    	        + "\t     Click OK to Begin.\n\n\t           Good Luck \n";
     
    	        outputArea.setText(output);
     
                JOptionPane.showMessageDialog(null, outputArea,
                    "The Guess the Numbers Game", JOptionPane.INFORMATION_MESSAGE);
     
    	       return;
     
          }// opening message
     
    public static void dice()
    		{
    		int Total;
    		String output = "";
     
    		//collect from user guess of
    		//die total with method guesTheDice(make if statement for not to break if val<= 1 && <=6
    		Total = guessTheDice();
     
    		int rolledTotal;
     
    		//roll the dice in method
    		rolledTotal = rollTheDice();
     
    		if(Total == rolledTotal)
    			output = "You guessed right!" +
    			"  The dice total is " + Total;
    		else
    			output = "Sorry.  The dice total is: "
    			+rolledTotal;
    		JOptionPane.showMessageDialog(
    			null, output);
     
    		System.exit(0);
    	}//end main
     
    	public static int guessTheDice ()
    	{
    		int dice1; //first value of die1
     
    		int total;
     
    		String firstDice;
     
    		firstDice=JOptionPane.showInputDialog
    			("Guess die 1 (1-6): ");
     
    		dice1=Integer.parseInt(firstDice);
     
    		total=dice1;
     
    		return total;
    	}//end guessTheDice method
     
    	public static int rollTheDice()
    	{
    		int x;
     
    		//generating random numbers
    		//between 1-6 with
    		//Math.random
    		x=1 + (int) (Math.random( ) *6);
     
    		return x;
    	}//end rollTheDice method
    	}// try val = integer parseInt

    The program is a game that randomly generates a number from 1 - 6 and tracks your wins, losses, and times played. Another problem with it is that the firs time you guess a number, the program kinda disregards the first guess and starts tracking from the second guess onwards. Thanks again


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Little help with this project please

    Look at how you print out Total (which should be total according to standard naming conventions) and just do something similar with your wins.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Little help with this project please

    Sorry i dont understand.. im trying to get it to look like this..

    public static void endingMessage()
    	{
    		    String output = "";
     
    	        Font big = new Font ( "Serif", Font.BOLD, 22 );  // set up how the letters will look
     
    	        JTextArea outputArea = new JTextArea ();  // create a new JTextArea named outputArea
     
    	        outputArea.setFont(big);    // use the font stuff from above
     
    	        outputArea.setBackground ( new Color ( 0x00, 0x00, 0x80 ) );  // Navy 000080
     
    	        outputArea.setForeground ( new Color ( 0xFF, 0xF8, 0xDC ) );  //  Corn Silk  FFF8DC
     
    	        output = "Thanks for playing the Guess the Numbers from 1 to 6 Program \n\n"
    	        + "You have won +wins \n"
    	        + "You have lost +losses \n\n"
    	        + "You have played +totalPlays \n";
     
    	        outputArea.setText(output);
     
                JOptionPane.showMessageDialog(null, outputArea,
                    "Thanks for playing", JOptionPane.INFORMATION_MESSAGE);
     
    	       return;
     
          }// ending message
    But that doesn't work.. so it would say you have won 5 times etc. i just cant figure it out =(

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Little help with this project please

    That's not how you print the total, is it? Pay close attention to where the quotation marks are.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Help with Project
    By rawrxeroes in forum Object Oriented Programming
    Replies: 1
    Last Post: October 12th, 2011, 07:12 AM
  2. Project
    By Mac in forum What's Wrong With My Code?
    Replies: 15
    Last Post: June 4th, 2010, 04:39 AM
  3. help with project
    By pairenoid in forum Object Oriented Programming
    Replies: 1
    Last Post: May 7th, 2010, 08:55 AM
  4. Project - Please Help
    By toxikbuni in forum Java Theory & Questions
    Replies: 0
    Last Post: April 20th, 2010, 09:58 AM
  5. Can anyone help me on my project?
    By alesana514 in forum Paid Java Projects
    Replies: 1
    Last Post: December 16th, 2009, 09:24 AM