| | |
Array dismay!
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Mar 2008
Posts: 52
Reputation:
Solved Threads: 0
I'm working on a simple matching game, whereby my array includes 3 numbers:{ 5,9,0 } and the user tries to guess them.
Instead, I want to use the Random class to generate 3 different numbers from 0-9, but I'm not sure how or where to start. I came across info on the Random class; however, there are no good examples using it with arrays. But, then I'm awful at arrays and need improvement in this area.
Can anyone help me out in this area? Thanks in advance.
Instead, I want to use the Random class to generate 3 different numbers from 0-9, but I'm not sure how or where to start. I came across info on the Random class; however, there are no good examples using it with arrays. But, then I'm awful at arrays and need improvement in this area.
Can anyone help me out in this area? Thanks in advance.
Java Syntax (Toggle Plain Text)
import java.io.*; import javax.swing.JOptionPane; public class MatchGame { public static int[] guess = {5,9,0}; public static void main(String[] args) throws IOException { MatchGame(); } public static void MatchGame() { int o = 3; boolean arraysMatch = true; String input; int i = 0; int[] x = new int[o]; for (i = 0; i < o; i++) { input = JOptionPane.showInputDialog("Input your 3 guesses: "+ (i + 1)); x[i] = Integer.parseInt(input); } i = 0; if (guess.length != x.length) arraysMatch = false; while (arraysMatch && i < guess.length) { if (guess[i] != x[i]) arraysMatch = false; i++; } if (arraysMatch) JOptionPane.showMessageDialog(null, "Hooray! You matched all 3 numbers."); else JOptionPane.showMessageDialog(null, "Sorry, you didn't match!"); results(); } public static void results() { JOptionPane.showMessageDialog(null, "The Correct Numbers: " + guess[0] + guess[1] + guess[2]); } }
•
•
Join Date: Dec 2008
Posts: 53
Reputation:
Solved Threads: 6
To use Random, you essentially:
(1) Construct a single Random object at the start.
(2) Each time you need a random number, call nextInt() on the Random object you created, specifying the upper bound on the random numbers you want. For example, to get a random number between 0 and 9 inclusive, call nextInt(10) (you add 1, because the upper bound you pass in to nextInt() is exclusive).
Always always pass in the upper bound to nextInt() -- sometimes you'll see people suggesting you call "nextInt() % 10", but this is INCORRECT.
I'm not sure what your mental block with arrays is, but in terms of using them with random numbers there's no other special thing to know. Put the first random integer in array position 0, the second in position 1 etc.
(1) Construct a single Random object at the start.
(2) Each time you need a random number, call nextInt() on the Random object you created, specifying the upper bound on the random numbers you want. For example, to get a random number between 0 and 9 inclusive, call nextInt(10) (you add 1, because the upper bound you pass in to nextInt() is exclusive).
Always always pass in the upper bound to nextInt() -- sometimes you'll see people suggesting you call "nextInt() % 10", but this is INCORRECT.
I'm not sure what your mental block with arrays is, but in terms of using them with random numbers there's no other special thing to know. Put the first random integer in array position 0, the second in position 1 etc.
•
•
Join Date: Mar 2008
Posts: 52
Reputation:
Solved Threads: 0
•
•
•
•
To use Random, you essentially:
(1) Construct a single Random object at the start.
(2) Each time you need a random number, call nextInt() on the Random object you created, specifying the upper bound on the random numbers you want. For example, to get a random number between 0 and 9 inclusive, call nextInt(10) (you add 1, because the upper bound you pass in to nextInt() is exclusive).
Always always pass in the upper bound to nextInt() -- sometimes you'll see people suggesting you call "nextInt() % 10", but this is INCORRECT.
I'm not sure what your mental block with arrays is, but in terms of using them with random numbers there's no other special thing to know. Put the first random integer in array position 0, the second in position 1 etc.
•
•
•
•
To construct a single random object at the start, etc., do you mean replace:
Java Syntax (Toggle Plain Text)
public static int[] guess = {5,9,0};
Java Syntax (Toggle Plain Text)
int[] guess = new int[10]; for (int i = 0; i < 10; i++) guess = i;
Last edited by clueless101; Dec 11th, 2008 at 8:08 pm.
•
•
Join Date: Jan 2008
Posts: 3,819
Reputation:
Solved Threads: 501
•
•
•
•
with:Java Syntax (Toggle Plain Text)
public static int[] guess = {5,9,0};
Java Syntax (Toggle Plain Text)
int[] guess = new int[10]; for (int i = 0; i < 10; i++) guess = i;
I interpreted the answers to questions 1 and 2 as 3 and 10, respectively, from post 1, so change the loop from:
Java Syntax (Toggle Plain Text)
int[] guess = new int[10]; for (int i = 0; i < 10; i++) guess = i;
to
int[] guess = new int[3]; for (int i = 0; i < 3; i++) { // code }
You definitely don't want the
guess = i; line, since that's not random.Create a Random object before the loop.
Java Syntax (Toggle Plain Text)
Random rand = new Random ();
Inside the loop, create the random numbers and fill the array.
Java Syntax (Toggle Plain Text)
Random rand = new Random (); int[] guess = new int[3]; for (int i = 0; i < 3; i++) { guess[i] = rand.nextInt (10); }
Depending on your answer to whether you allow repeats, the code could be a little more involved, but if you don't care, the above is the idea if I'm reading your problem correctly.
•
•
Join Date: Mar 2008
Posts: 52
Reputation:
Solved Threads: 0
I appreciate the help, but you lost me.
1st ? - Do I remove:
2nd ? - Where do I insert Random object and related loop?
I'm getting errors out the bazoo, no matter what or where I try.
Thanks for any suggestions.
1st ? - Do I remove:
Java Syntax (Toggle Plain Text)
public static int[] guess = {5,9,0};
2nd ? - Where do I insert Random object and related loop?
I'm getting errors out the bazoo, no matter what or where I try.
Thanks for any suggestions.
•
•
Join Date: Jan 2008
Posts: 3,819
Reputation:
Solved Threads: 501
•
•
•
•
I appreciate the help, but you lost me.
1st ? - Do I remove:
Java Syntax (Toggle Plain Text)
public static int[] guess = {5,9,0};
Java Syntax (Toggle Plain Text)
int[] guess = new int[3];
so you can't declare it twice.
Probably make both of them non-static class variables and instantiate them in your constructor:
Java Syntax (Toggle Plain Text)
import java.util.Random; public class MatchGame { Random rand; int[] guess; public MatchGame() { rand = new Random (); guess = new int[3]; for (int i = 0; i < 3; i++) { guess[i] = rand.nextInt (10); } // more code } // more MatchGame class functions here public static void main (String args[]) { new MatchGame (); } }
Last edited by VernonDozier; Dec 11th, 2008 at 10:11 pm.
•
•
Join Date: Mar 2008
Posts: 52
Reputation:
Solved Threads: 0
•
•
•
•
I end up with 11 errors after I revise my code. Did I miss something? Thanks in advance.
Java Syntax (Toggle Plain Text)
import java.util.Random; public class MatchGame { Random rand; int[] guess; public MatchGame() { rand = new Random(); guess = new int[3]; for (int i = 0; i < 3; i++) { guess[i] = rand.nextInt (10); } { MatchGame(); } public static void MatchGame() { int o = 3; boolean arraysMatch = true; String input; int i = 0; int[] x = new int[o]; for (i = 0; i < o; i++) { input = JOptionPane.showInputDialog("Input your 3 guesses: "+ (i + 1)); x[i] = Integer.parseInt(input); } i = 0; if (guess.length != x.length) arraysMatch = false; while (arraysMatch && i < guess.length) { if (guess[i] != x[i]) arraysMatch = false; i++; } if (arraysMatch) JOptionPane.showMessageDialog(null, "Hooray! You matched all 3 numbers."); else JOptionPane.showMessageDialog(null, "Sorry, you didn't match!"); results(); } public static void results() { JOptionPane.showMessageDialog(null, "The Correct Numbers: " + guess[0] + guess[1] + guess[2]); } public static void main (String[]args) { new MatchGame (); } } }
ÏÏÏ ----jGRASP exec: javac -g C:\Program Files\MatchGame.java
ÏÏÏ
ÏÏMatchGame.java:34: illegal start of expression
ÏÏÏ public MatchGame()
ÏÏÏ ^
ÏÏMatchGame.java:34: ';' expected
ÏÏÏ public MatchGame()
ÏÏÏ ^
ÏÏMatchGame.java:63: illegal start of expression
ÏÏÏ public static void results()
ÏÏÏ ^
ÏÏMatchGame.java:63: illegal start of expression
ÏÏÏ public static void results()
ÏÏÏ ^
ÏÏMatchGame.java:63: ';' expected
Ï Ï public static void results()
ÏÏÏ ^
ÏÏMatchGame.java:63: ';' expected
ÏÏÏ public static void results()
ÏÏÏ ^
ÏÏMatchGame.java:69: illegal start of expression
ÏÏÏ public static void main (String[]args)
ÏÏÏ ^
ÏÏMatchGame.java:69: illegal start of expression
ÏÏÏ public static void main (String[]args)
ÏÏÏ ^
Ï MatchGame.java:69: ';' expected
ÏÏÏ public static void main (String[]args)
ÏÏÏ ^
ÏÏMatchGame.java:69: '.class' expected
ÏÏÏ public static void main (String[]args)
ÏÏÏ ^
ÏÏMatchGame.java:69: ';' expected
ÏÏÏ public static void main (String[]args)
ÏÏÏ ^
ÏÏÏ11 errors
•
•
Join Date: Jan 2008
Posts: 3,819
Reputation:
Solved Threads: 501
Start with my skeleton, compile it, run it without errors, then put in your code a little bit at a time. Look at your brackets, look at your main function, look at your indentation. Remove any brackets that don't serve a purpose, indent consistently so you know what code is part of which section of brackets. Look at anything in your code that says "static" and if you don't know why you are making it static, don't make it static. main has to be static, but for everything else, if you don't need to make it static, don't. If you have two functions with the same name, rename one. If a function is not called, comment it out.
You cannot debug code until you have consistent indentation. It will really help you see what's wrong.
You cannot debug code until you have consistent indentation. It will really help you see what's wrong.
![]() |
Similar Threads
Other Threads in the Java Forum
- Previous Thread: how to add smack packge to API
- Next Thread: Getting integer values from a JTable
| Thread Tools | Search this Thread |
android api applet application apps array arrays automation awt bidirectional binary birt bluetooth businessintelligence busy_handler(null) card chat class classes client code columns component constructor database designadrawingapplicationusingjavajslider draw eclipse editor error errors eventlistener exception expand fractal game givemetehcodez graphics gui guidancer html ide image inetaddress input integer intellij j2me java javafx javamicroeditionuseofmotionsensor javaprojects jme jni jpanel jtree julia linux list loop map method methods mobile mobiledevelopmentcreatejar myaggfun netbeans newbie oracle parsing plazmic print problem program programming project recursion scanner screen server set sharepoint size smart sms smsspam sort sortedmaps sql string subclass support swing textfield threads tree unlimited utility webservices windows






