Results 1 to 8 of 8
- 05-05-2011, 01:25 AM #1
Member
- Join Date
- May 2011
- Posts
- 27
- Rep Power
- 0
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(); } }
- 05-05-2011, 04:25 AM #2
Member
- Join Date
- May 2011
- Posts
- 27
- Rep Power
- 0
Please, any suggestions or examples somewhere online would be appreciated.
- 05-05-2011, 04:31 AM #3
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
- 05-05-2011, 04:42 AM #4
Member
- Join Date
- May 2011
- Posts
- 27
- Rep Power
- 0
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?
- 05-05-2011, 05:14 AM #5
No. You need to understand the concepts and then implement them.
db
- 05-05-2011, 05:35 AM #6
Member
- Join Date
- May 2011
- Posts
- 27
- Rep Power
- 0
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){
- 05-05-2011, 06:44 AM #7
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.
GoldestJava Is A Funny Language... Really!
Click on * and add to member reputation, if you find their advices/solutions effective.
- 05-05-2011, 06:53 AM #8
Member
- Join Date
- May 2011
- Posts
- 27
- Rep Power
- 0
Similar Threads
-
Creating a 'turn' method
By StateofMind in forum New To JavaReplies: 6Last Post: 11-27-2012, 11:36 AM -
Creating a second method in java
By Teclis in forum New To JavaReplies: 3Last Post: 04-09-2011, 06:49 PM -
Which method is best for creating Threads?
By makpandian in forum Threads and SynchronizationReplies: 11Last Post: 06-08-2009, 09:00 AM -
Creating a GUI - which method
By matpj in forum New To JavaReplies: 3Last Post: 01-15-2009, 05:40 PM -
Creating a new equals() method help
By Dave0703 in forum New To JavaReplies: 2Last Post: 09-21-2008, 06:32 PM
Bookmarks