Results 1 to 9 of 9
  1. #1
    Join Date
    Apr 2016
    Posts
    7
    Rep Power
    0

    Default New to Java, In need of some help with a basic program.

    Hello, I am very new to java, and am taking a class on it. My project right now is to make a "guessing game" with the numbers 1 to 6. However it has to be in a gui. I have the basic program somewhat setup. But i wanted to post here to make sure im going in the right direction. I also am stuck with having to use methods that return values.
    Any helps or tips are greatly appreciated. Here is my code so far.
    Java Code:
    import java.lang.*;  // loaded by default
    import javax.swing.JOptionPane;
    import javax.swing.UIManager;
    import java.awt.Font;  // need for Font and Point Size Changes
    import java.awt.Color;  // need to change Color
    import java.text.*;
                             
    
    class Guessinggame   // class Name will begin with a Capital Letter.
    {
    
        final int Wins = 1 ;
        final int Losses = 0 ;
    
        public static void main (String[] args)
        {
              runit();  // call method to start work
    			 
    			 System.exit (0);  // close program
    
        } // end of main
    	 
    	 public static void runit ()
    	 {
          setUI();
          openingmsg();
          game ();     
    	 
    	 }//method to tell order
        
        
        public static void openingmsg ()
        {
    	         int n = JOptionPane.showConfirmDialog(
                null,
                "Welcome, Would you like to play the guessing game?",
                "Adams number guessing game",
                JOptionPane.YES_NO_OPTION);
                
                if (n == JOptionPane.YES_OPTION)
                {
                   
                }
                
                else
                {
                   JOptionPane.showMessageDialog(null, "GOODBYE");
                   System.exit (0);
                }
        }//opening message, and continue option
       
         
           public static void setUI ()
        {
           UIManager um = new UIManager();
           um.put("OptionPane.messageForeground", Color.yellow);
           um.put("Panel.background", Color.black);
    
    		 
    		 Font font1 = new Font("serif",Font.BOLD,24);
           Font font2 = new Font("sansserif",Font.BOLD,18);
    		 
           um.put ("Label.font", font1);
           um.put ("Button.font", font2);
           um.put ("TitledBorder.font",font2);
           um.put ("TextField.font",font1);
        
        }//UI manager.
        
        public static void game ()
        {
           int comnum ;
           comnum = (int) (Math.random() * 6 + 1);
           int usernum ;
           System.out.println("The comnum is" + comnum);
           
           Object[] possibilities = {"1", "2", "3","4","5","6"};
           String s = (String)JOptionPane.showInputDialog(
                        null,
                        "Pick a number!\n"
                        + "\"One through 6\"",
                        "Guessing Game",
                        JOptionPane.PLAIN_MESSAGE,
                        null,
                        possibilities,
                        "6");
                       
           
           
           System.exit(0);
    	 }//method that runs the game

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: New to Java, In need of some help with a basic program.

    I presume you want to check the generated number to what the player chose. So the return value from the
    InputDialog (String s) contains the chosen number. Now you have to compare that to the generated number.
    You may then want to provide a win/loss message.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Join Date
    Apr 2016
    Posts
    7
    Rep Power
    0

    Default Re: New to Java, In need of some help with a basic program.

    Yes you are correct. So string s is essentially the users choice? So how would i make each case make usernum equal to the button clicked? Maybe integer parse? I also do want to add a win/loss box, however i cannot figure out how to do this in a gui window instead of System.out.prinln statements.
    Thanks ahead of time. This has been baffling me.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: New to Java, In need of some help with a basic program.

    Since you know the string must be an integer (only choices by list selection) you can use the Integer parse. Or you can convert the integer to a string by appending a ""
    to it (not necessarily good practice but it will let you get this working). Then simply compare the two strings (using equals and not == since Strings are objects). Then based on the comparison, simply print out a dialog of win or loss (like you did with the goodbye dialog).

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    Join Date
    Apr 2016
    Posts
    7
    Rep Power
    0

    Default Re: New to Java, In need of some help with a basic program.

    So i am trying to use parse int to get an integer value for string s. That way i can compare this to the generated number. But i keep getting error codes. Maybe i shouldnt have declared the usernum variable as early as i did.
    Java Code:
    public static void game ()
        {
           int comnum ;
           comnum = (int) (Math.random() * 6 + 1);
           
           System.out.println("The comnum is" + comnum);
           int usernum;
           Object[] possibilities = {"1", "2", "3","4","5","6"};
           String s = (String)JOptionPane.showInputDialog(
                        null,
                        "Pick a number!\n"
                        + "\"One through 6\"",
                        "Guessing Game",
                        JOptionPane.PLAIN_MESSAGE,
                        null,
                        possibilities,
                        "6");
             static int parseInt (String s);         
             
              
                      
           JOptionPane.showMessageDialog(null, "You have won" + Wins + "times"
                                             + "You have lost" + Losses + "times");

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: New to Java, In need of some help with a basic program.

    Your syntax makes no sense. What are you trying to do? To convert a String to an int use Integer.valueOf().

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: New to Java, In need of some help with a basic program.

    BTW, you can just declare an array of Integers instead of Objects. Then cast the return value from the OptionPane to Integer. Then
    no conversion needs to be done. You can simply compare usernum to comnum.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  8. #8
    Join Date
    Apr 2016
    Posts
    7
    Rep Power
    0

    Default Re: New to Java, In need of some help with a basic program.

    I edited some of my code to be a little more smooth. I have everything in there almost. But i need to keep the user in the program for them to play again. And my counter doesnt keep acurate count. Im getting close i can feel it! Here is my revised code. These are using some templates that i was given .

    Java Code:
    import java.util.Random; 
    import java.lang.*;  // loaded by default
    import javax.swing.JOptionPane;
    import javax.swing.UIManager;
    import java.awt.Font;  // need for Font and Point Size Changes
    import java.awt.Color;  // need to change Color
    import java.text.*;
                             
    
    class Guessinggame   // class Name will begin with a Capital Letter.
    {
    
        final static int Wins = 1 ;
        final static int Losses = 0 ;
        final static int Games = 0 ;
        final static int EXIT = 7;     
        final static int X = 0;
        
        public static void main (String[] args)
        {
              runit();  // call method to start work
    			 
    			 System.exit (0);  // close program
    
        } // end of main
    	 
    	 public static void runit ()
    	 {
          setUI();
          openingmsg();
           
    	 
    	 }//method to tell order
        
        
        public static void openingmsg ()
        {
    	         int n = JOptionPane.showConfirmDialog(
                null,
                "Welcome, Would you like to play the guessing game?",
                "Adams number guessing game",
                JOptionPane.YES_NO_OPTION);
                
                if (n == JOptionPane.YES_OPTION)
                {
                  getUser ();
                  getCom (); 
                  total ();
                
                }
                
                else
                {
                   JOptionPane.showMessageDialog(null, "GOODBYE");
                   System.exit (0);
                }
        }//opening message, and continue option
       
         
           public static void setUI ()
        {
           UIManager um = new UIManager();
           um.put("OptionPane.messageForeground", Color.yellow);
           um.put("Panel.background", Color.black);
    
    		 
    		 Font font1 = new Font("serif",Font.BOLD,24);
           Font font2 = new Font("sansserif",Font.BOLD,18);
    		 
           um.put ("Label.font", font1);
           um.put ("Button.font", font2);
           um.put ("TitledBorder.font",font2);
           um.put ("TextField.font",font1);
        
        }//UI manager.
        
       
          public static int getUser() 
          {  
          int games = 0;
          int mc = JOptionPane.QUESTION_MESSAGE;  
          String[] opts = { "1", "2", "3","4","5","6","Exit" };          
          int ch = JOptionPane.showOptionDialog (null, "Choose a Number   from 1 - 6",
           "Number Choices", 0, mc, null, opts, opts[2]);  
           ch++;  
           games++;
           return ch; 
            
          }// getUserNum 
          
          public static int getCom()  // returns an int value via the return statement
            {
            	Random generator = new Random();  // declare a new Instance of type Random
    
               int  rNum  = generator.nextInt (6) + 1; //  generator.nextInt(6) creates numbers from 0 to 5;  thus the reason to add 1
                JOptionPane.showMessageDialog(null, "Computer chose" + rNum);
               return rNum;  // return value
              
    
           }
           
          public static boolean compareNum (int ch, int rNum)
    	   {
    	   boolean won = false;
    		
    		  if (ch == rNum)
    		  {
    		     won = true;
    		  }// if
    		return won;
          
    	   }// compareTwo
     
          public static void total ()
          {
             JOptionPane.showMessageDialog(null, "You have won" + Wins + "Times"
                                                +"You have lost" + Losses + "Times"
                                                +"You have played" + Games + "Times");
          } 
     }

  9. #9
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,807
    Rep Power
    13

    Default Re: New to Java, In need of some help with a basic program.

    If you want the user to keep playing then you will need a loop somewhere.

Similar Threads

  1. Output of a basic Java program
    By aussieguy0101 in forum New To Java
    Replies: 5
    Last Post: 02-17-2013, 08:52 AM
  2. Replies: 2
    Last Post: 07-29-2012, 11:10 PM
  3. Simple First Java program: Basic Contact register
    By ZinoXxX in forum New To Java
    Replies: 8
    Last Post: 03-17-2012, 04:13 PM
  4. need help to figure out basic java program
    By shane123 in forum New To Java
    Replies: 21
    Last Post: 12-02-2011, 05:12 AM
  5. Basic Program Please Help!!
    By VinceGuad in forum New To Java
    Replies: 3
    Last Post: 02-01-2008, 04:35 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •