Results 1 to 8 of 8
  1. #1
    drewtrcy is offline Member
    Join Date
    May 2011
    Posts
    27
    Rep Power
    0

    Default I could use some suggestions for creating a new method

    I put astrics above and below what I need as a new method and suggestions on how to get rid of the boolean error at the beginning of each main if statement there.


    Java Code:
    package Final;
    
    import javax.swing.*;
    
    public class P_Final_Exam {
        //declaring main variables
        static int currentRoll;
        static int lastRoll;
        static int score;
        static int guess;
    
        public static void main(String[] args){
    
        //ask player if they want to play
            int answer = JOptionPane.showConfirmDialog(null, "Do you want to play?",
                    "Dice Game",JOptionPane.YES_NO_CANCEL_OPTION,
        JOptionPane.QUESTION_MESSAGE);
    
            if (answer == JOptionPane.NO_OPTION){
                System.exit(1);
            } else if (answer == JOptionPane.CANCEL_OPTION){
                System.exit(1);
            }
            currentRoll = random();
    
            //JOptionPane.showMessageDialog(null, random());
            //System.out.println(System.getProperty("user.dir"));
    
        ImageIcon icon = new ImageIcon ("icons/" + currentRoll + ".png");
        Object[] options = {"Higher",
                        "Lower",
                        "Cancel"};
    
    guess = JOptionPane.showOptionDialog(null,
        "Will the next roll be higher?",
        "Dice Game",
        JOptionPane.YES_NO_CANCEL_OPTION,
        JOptionPane.QUESTION_MESSAGE,
        icon,
        options,
        options[0]);
    
    //********************************************************
    
      if (currentRoll == lastRoll){
          JOptionPane.showMessageDialog(null,
        "Your guess was neither correct nor incorrect.  Score will remain the same.",
        "Dice Game",
        JOptionPane.INFORMATION_MESSAGE,
        icon);      
      }else if (currentRoll > lastRoll){
          if (guess = JOptionPane.YES_OPTION){
              rightAnswer();
          }else {
              wrongAnswer();
          }
      }else (currentRoll < lastRoll){
          if (guess = JOptionPane.NO_OPTION){
              rightAnswer();
          }else {
              wrongAnswer();
          }
      }
    
    //********************************************************
    
      lastRoll = currentRoll;
    }
    public static void diceRoll(){
        //dice roll and guess
    
    
    
    }    
    public static int random(){
        //generate random number for die and icon
       int num;
       num = (int)(Math.random()*6) + 1;
    
       return num;
    }
    public static int rightAnswer(){
        //add to score
        score++;
        return score;
    }
    public static int wrongAnswer(){
        //subtract from score
        score--;
        return score;
    }
    public static int sameAnswer(){
        //do nothing to score
        return score;
    }
    public static int exit(){
        //display final score on exit
    
        return exit();
        }
    }

  2. #2
    drewtrcy is offline Member
    Join Date
    May 2011
    Posts
    27
    Rep Power
    0

    Default

    Please, any suggestions or examples somewhere online would be appreciated.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,422
    Rep Power
    20

    Default

    General rules for factoring out a method:
    1. Apart from state (instance fields), what does the method need to know? These pieces of information form the parameters passed to the method.

    2. What does the method need to do? That logic forms the body of the method.

    3. What does the method need to say? That's the return value, or void if it deosn't need to say anything.

    Here's a tutorial:
    Defining Methods (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

    db

  4. #4
    drewtrcy is offline Member
    Join Date
    May 2011
    Posts
    27
    Rep Power
    0

    Default

    believe it or not, that tutorial just has me even more confused. Are there any examples similar to what I am trying to do above?

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,422
    Rep Power
    20

    Default

    No. You need to understand the concepts and then implement them.

    db

  6. #6
    drewtrcy is offline Member
    Join Date
    May 2011
    Posts
    27
    Rep Power
    0

    Default

    I am going to go ahead and leave the code as is but I am a getting boolean error on line 53 and 69 and a "not a statement" error "expecting ';'" on line 68. Could someone explain to me why that is please?


    this is the code starting and ending with the errors, if you need the rest just look above. Thanks so much in advance.

    Java Code:
          if (guess = JOptionPane.YES_OPTION){
          JOptionPane.showMessageDialog(null,
          "Your guess was right.  Your score has increased by 1.",
          "Dice Game",
          JOptionPane.INFORMATION_MESSAGE,
          icon);
              rightAnswer();
          }else {
          JOptionPane.showMessageDialog(null,
          "Your guess was right.  Your score has increased by 1.",
          "Dice Game",
          JOptionPane.INFORMATION_MESSAGE,
          icon);
              wrongAnswer();
          }
      }else (currentRoll < lastRoll){
          if (guess = JOptionPane.NO_OPTION){

  7. #7
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    I can't see the if block for the else statement on the second last line.

    And "expecting ;" is more of kind of syntax error. You have messed it up somewhere. Have a closer look at your opening/closing brackets and make sure that everything is at its place.

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  8. #8
    drewtrcy is offline Member
    Join Date
    May 2011
    Posts
    27
    Rep Power
    0

    Default

    I figured out the problem with the boolean error, I didn't have enough = in the statements, now the only problem left is the expected ";" error

Similar Threads

  1. Creating a 'turn' method
    By StateofMind in forum New To Java
    Replies: 6
    Last Post: 11-27-2012, 10:36 AM
  2. Creating a second method in java
    By Teclis in forum New To Java
    Replies: 3
    Last Post: 04-09-2011, 05:49 PM
  3. Which method is best for creating Threads?
    By makpandian in forum Threads and Synchronization
    Replies: 11
    Last Post: 06-08-2009, 08:00 AM
  4. Creating a GUI - which method
    By matpj in forum New To Java
    Replies: 3
    Last Post: 01-15-2009, 04:40 PM
  5. Creating a new equals() method help
    By Dave0703 in forum New To Java
    Replies: 2
    Last Post: 09-21-2008, 05:32 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
  •