Jrod Posted March 11, 2007 Report Share Posted March 11, 2007 I am taking Java programming for the first time right now in college and was assigned a program as a major part of my grade. The program is to make a game that creates a random number between 1 - 100 and then asks the user to guess that number in 10 or less attempts. It'll then tell the user if their guess was too high or too low. So far, I've been able to do that with a while loop. BUT, I also need to congradulate the user for guessing right and then ask if they want to play again. The problem is, is that if/else statement to tell if the guess is too high/low/correct is nested in the while loop. Using the JOptionPane, I need to ask if they want to play again and if they do, I need the program to loop back to beginning where it generates a new random number. Also, if the user runs out of guesses, I need to tell the user they used all their guesses, and then too, ask if they want to play again and find a way to start the game over. I've tried using the .showConfirmDialog to give the option of 'yes' or 'no' but I don't know what to do from there. If anyone can give any suggestions I'd appreciate it. If you'd need to see the written program I'd be able to do that. Link to comment Share on other sites More sharing options...
Jamie_B Posted March 11, 2007 Report Share Posted March 11, 2007 Hint Look into Static Class variables. Link to comment Share on other sites More sharing options...
Jamie_B Posted March 11, 2007 Report Share Posted March 11, 2007 Further documentation. [url="http://java.sun.com/docs/books/tutorial/java/javaOO/classvars.html"]http://java.sun.com/docs/books/tutorial/ja.../classvars.html[/url] Link to comment Share on other sites More sharing options...
Jrod Posted March 11, 2007 Author Report Share Posted March 11, 2007 [quote name='Jamie_B' post='455288' date='Mar 11 2007, 05:58 PM']Further documentation. [url="http://java.sun.com/docs/books/tutorial/java/javaOO/classvars.html"]http://java.sun.com/docs/books/tutorial/ja.../classvars.html[/url][/quote] That's what I was looking at when I searched up static variables, but I'm afraid I have no idea how to incorporate that into what I need to do. I do know that the .showConfirmDialog of the JOptionPane returns an integer depending on what the user chooses, but I don't know how to use that to start back to the beginning of the program. I don't want to get into anything I haven't learned in lectures and class yet, you know? Link to comment Share on other sites More sharing options...
Jamie_B Posted March 11, 2007 Report Share Posted March 11, 2007 [quote name='Jrod' post='455290' date='Mar 11 2007, 06:05 PM']That's what I was looking at when I searched up static variables, but I'm afraid I have no idea how to incorporate that into what I need to do. I do know that the .showConfirmDialog of the JOptionPane returns an integer depending on what the user chooses, but I don't know how to use that to start back to the beginning of the program. I don't want to get into anything I haven't learned in lectures and class yet, you know?[/quote] have you leart about creating more than one class or does everything need to be within the same class? Link to comment Share on other sites More sharing options...
Jamie_B Posted March 11, 2007 Report Share Posted March 11, 2007 Can you post the code? Link to comment Share on other sites More sharing options...
Jamie_B Posted March 11, 2007 Report Share Posted March 11, 2007 Also when is this due? I ask because Ive got a bunch of stuff I need to get done around the house, and my own school stuff to look at. But this should be easy enought that I can help depending on the due date. Link to comment Share on other sites More sharing options...
Jrod Posted March 11, 2007 Author Report Share Posted March 11, 2007 [quote name='Jamie_B' post='455295' date='Mar 11 2007, 06:10 PM']Also when is this due? I ask because Ive got a bunch of stuff I need to get done around the house, and my own school stuff to look at. But this should be easy enought that I can help depending on the due date.[/quote] So far, we've only used one class. This project is due by 6 pm tomorrow... yeah yeah.. last minute. I've had no internet access from home for months, making it difficult for me to get anything done consistently. There's nothing high speed where I live and my phone lines were fucked by the DirectTV guy. I'm using a friend's computer now so I'll try to post the code shortly. Link to comment Share on other sites More sharing options...
Jamie_B Posted March 11, 2007 Report Share Posted March 11, 2007 [quote name='Jrod' post='455299' date='Mar 11 2007, 06:19 PM']So far, we've only used one class. This project is due by 6 pm tomorrow... yeah yeah.. last minute. I've had no internet access from home for months, making it difficult for me to get anything done consistently. There's nothing high speed where I live and my phone lines were fucked by the DirectTV guy. I'm using a friend's computer now so I'll try to post the code shortly.[/quote] Ill take a look around 8ish then I really got alot to get done. Link to comment Share on other sites More sharing options...
Jrod Posted March 11, 2007 Author Report Share Posted March 11, 2007 [quote name='Jamie_B' post='455300' date='Mar 11 2007, 06:21 PM']Ill take a look around 8ish then I really got alot to get done.[/quote] Here's the code: (I tried to send an attachment for easier reading but got an error and I have to leave shortly) /* Jared Collum * NumberGame * Created February 28, 2007 */ package collum; import javax.swing.JOptionPane; import java.util.Random; public class NumberGame { public static void main(String[] args) { // defining variables and creating a Random number object Random genNum = new Random(); byte randomNum = 0; byte userGuess = 0; // Convert Generated Random number from 1 - 100 to use in calculation randomNum = (byte)((genNum.nextFloat() * 100) + 1); // Displays the Number generated from 1 - 100 System.out.println(randomNum); // Welcomes the user JOptionPane.showMessageDialog ( null, "Welcome to the Random Number Game", "Welcome", -1 ); // A while loop that allows 10 guesses and tells if the guess is too large or small while (userGuess <= 10) { String userString = JOptionPane.showInputDialog ( null, "Guess a number between 1 and 100", "Guess a Number", -1 ); // Converts the String value to an integer int guess = Integer.parseInt(userString); // When the user's guess is low if(guess < randomNum) { JOptionPane.showMessageDialog ( null, "Your guess was too low", "Incorrect", 1 ); userGuess ++; } // When the user's guess is high else if(guess > randomNum) { JOptionPane.showMessageDialog ( null, "Your guess was too high", "Incorrect", 1 ); userGuess ++; } // When the user's guess is correct else { JOptionPane.showMessageDialog ( null, "Correct!\nIt took you " + userGuess + " guesses", "You Guessed Correct!", -1 ); // Will ask the user to play again and choose 'yes' or 'no' // Unable to figure out how to loop back to beginning of game JOptionPane.showConfirmDialog ( null, "Would you like to play again?", "Play Again?", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE ); } } // Tells the user they used all their guesses JOptionPane.showMessageDialog ( null, "You ran out of guesses", "No more guesses", 2 ); // Asks the user to play again after using all their guesses JOptionPane.showConfirmDialog ( null, "Would you like to play again?", "Play Again?", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE ); // Will thank the user for playing when they select 'no' // Displays even if 'yes' is selected JOptionPane.showMessageDialog ( null, "Thank You for Playing", "Thank You", -1 ); } } If you have any suggestions post them, I'll try to check in when I get on campus tomorrow morning. Nevertheless, I appreciate your effort to help. Link to comment Share on other sites More sharing options...
Jamie_B Posted March 12, 2007 Report Share Posted March 12, 2007 Forget what I said about the static stuff, its been so long since I did this stuff that I forget things sometimes. Hell I cant even remember how to get the javac compiler to work on my pc. I remember vaguely it has to do with classpath, but not what I need to do to get it to work. So because of that I can offer suggestions but cant test them.... For the new random number. Put your random number stuff in its own method and call it when you need it before the while loop and then again when you need to generaete a new random numer. Do this as well for your while loop so that you can call the random number method and then within it call the while method to start over. For the yes/no thing. You need to set your showconfirmdialogue to a variable and use that variable in a if statement to either quit or return you to the random number method. Here is a good doc on it. [url="http://java.sun.com/docs/books/tutorial/uiswing/components/dialog.html"]http://java.sun.com/docs/books/tutorial/ui...nts/dialog.html[/url] Scroll down to where it says "Customizing Button Text" and you can see what they did where they have int n = JOptionPane.showConfirmDialog( frame, "Would you like green eggs and ham?", "An Inane Question", JOptionPane.YES_NO_OPTION); I believe its either 0 or 1 MAYBEE a -1, but like I said I cant remember how to correctly get the javac compliler to work to do some testing. Good luck. Link to comment Share on other sites More sharing options...
|Elflocko| Posted March 12, 2007 Report Share Posted March 12, 2007 Sounds like you might need a subroutine: [url="http://www.faqs.org/docs/javap/c4/s2.html"]http://www.faqs.org/docs/javap/c4/s2.html[/url] I took programming because I had to, so I defer to Jamie's knowledge as I'm creative as a brick when it comes to figuring out the logic for shit like this. Now when you get to SQL, that's another story.... Link to comment Share on other sites More sharing options...
Jamie_B Posted March 12, 2007 Report Share Posted March 12, 2007 [quote name='Elflocko' post='455383' date='Mar 11 2007, 10:19 PM']Sounds like you might need a subroutine: [url="http://www.faqs.org/docs/javap/c4/s2.html"]http://www.faqs.org/docs/javap/c4/s2.html[/url] I took programming because I had to, so I defer to Jamie's knowledge as I'm creative as a brick when it comes to figuring out the logic for shit like this. Now when you get to SQL, that's another story.... [/quote] Yes a subroutine, I dont know why I kept calling them methods. Good site. SQL. MS or Oracle? Link to comment Share on other sites More sharing options...
-GoBengals- Posted March 12, 2007 Report Share Posted March 12, 2007 *wipes tear* thats all exactly what this forum was designed for.... Link to comment Share on other sites More sharing options...
Jamie_B Posted March 12, 2007 Report Share Posted March 12, 2007 [quote name='GoBengals' post='455410' date='Mar 11 2007, 11:25 PM']*wipes tear* thats all exactly what this forum was designed for.... [/quote] Link to comment Share on other sites More sharing options...
Guest CincyInDC Posted March 12, 2007 Report Share Posted March 12, 2007 [quote name='Jamie_B' post='455397' date='Mar 11 2007, 10:49 PM']Yes a subroutine, I dont know why I kept calling them methods. Good site. SQL. MS or Oracle?[/quote] (please say PL/SQL, please say PL/SQL, please say PL/SQL, please say PL/SQL, please say PL/SQL, please say PL/SQL) Link to comment Share on other sites More sharing options...
|Elflocko| Posted March 12, 2007 Report Share Posted March 12, 2007 [quote name='Jamie_B' post='455397' date='Mar 11 2007, 09:49 PM']Yes a subroutine, I dont know why I kept calling them methods. Good site. SQL. MS or Oracle?[/quote] MS, MySQL, Sybase, [b]and[/b] we just got a brand spanking new LTS DB running......... Oracle 10g, so I'm learning PL/SQL starting this week and starting on my certification track. I don't think there's that much difference between the varieties, but I'll find out soon enough. Well, Sybase is really fucked up, but that would explain their 1% DB market share... Link to comment Share on other sites More sharing options...
Jamie_B Posted March 12, 2007 Report Share Posted March 12, 2007 [quote name='Elflocko' post='455475' date='Mar 12 2007, 08:52 AM']MS, MySQL, Sybase, [b]and[/b] we just got a brand spanking new LTS DB running......... Oracle 10g, so I'm learning PL/SQL starting this week and starting on my certification track. I don't think there's that much difference between the varieties, but I'll find out soon enough. Well, Sybase is really fucked up, but that would explain their 1% DB market share...[/quote] Maybee its me and Im more the programmer than the SQL programmer, but T-SQL and PL-SQL looks vastly different. I need to learn both T-SQL for my certs (since visual interdev doesnt exist anymore when I last took my cert I did that isntead of SQL Server) and PL SQL for work/school. Though neither have to be learnt right away. So I may hit you up from time to time when I do need to learn them and have questions then. Link to comment Share on other sites More sharing options...
|Elflocko| Posted March 12, 2007 Report Share Posted March 12, 2007 [quote name='Jamie_B' post='455477' date='Mar 12 2007, 08:04 AM']Maybee its me and Im more the programmer than the SQL programmer, but T-SQL and PL-SQL looks vastly different. I need to learn both T-SQL for my certs (since visual interdev doesnt exist anymore when I last took my cert I did that isntead of SQL Server) and PL SQL for work/school. Though neither have to be learnt right away. So I may hit you up from time to time when I do need to learn them and have questions then.[/quote] Once you learn one really well, the differences don't seem as large, as it's mainly a difference in syntax. That, and certain languages do or don't support certain commands. For example, DB2 and MySQL support Natural Joins, while PL/SQL doesn't. It's just a matter of remembering which one supports what, and for that I use O'Reilly's SQL Pocket Guide... Link to comment Share on other sites More sharing options...
Jamie_B Posted March 12, 2007 Report Share Posted March 12, 2007 [quote name='Elflocko' post='455525' date='Mar 12 2007, 12:08 PM']Once you learn one really well, the differences don't seem as large, as it's mainly a difference in syntax. That, and certain languages do or don't support certain commands. For example, DB2 and MySQL support Natural Joins, while PL/SQL doesn't. It's just a matter of remembering which one supports what, and for that I use O'Reilly's SQL Pocket Guide...[/quote] O'Reilly makes the best books! Link to comment Share on other sites More sharing options...
Guest bengalrick Posted March 13, 2007 Report Share Posted March 13, 2007 guys, i am also in need of some help... I am trying to write a program that generates 10 random characters but only prints distinct numbers, so if 1, 2, 3, 1, 2, 4 were all generated, only 1, 2, 3, and 4 would actually be printed and not the doubles... we are using arrays and methods, so i have both utilized in this program, but i cannot get this code to work... if you look in method randomNumbers, that seems to be my problem... if i added a counts[i] = 0, would that solve my problem? i just thought about that as i was writing this thread, and don't have Jbuilder at work to check to see if that solves my problem... any help would be very grateful... thanks... [code]public class assignment4_1 { public static void main (String [] args) { int[] randomNumbers = createArray(); // Creates the array method // Display the array System.out.println ("The 10 random numbers before distinct numbers are: "); displayArray(randomNumbers); // Displays the 100 random Numbers method int[] counts = countNumbers(randomNumbers); // Count the occurances for each number int[] occurs = checkOccurs(counts); // Checks the occurances to weed out repeats displayDoubleCounts (occurs); // Display doubled countscounts displayOtherCounts (occurs); } public static int[] createArray() { int[] randomNumbers = new int[10]; // Create 10 random numbers for the array for (int i = 0; i < randomNumbers.length; i++) { randomNumbers[i] = (int)(Math.random() * 10); } // return the array return randomNumbers; } public static void displayArray (int[] randomNumbers) { for (int i = 0; i < randomNumbers.length; i++) { if ((i + 1) % 10 == 0) System.out.println (randomNumbers[i] + " "); else System.out.print(randomNumbers[i] + " "); } } public static int[] countNumbers(int[] randomNumbers) { // Declare and create an array of 10 numbers. int[] counts = new int[10]; // For each number from 0 - 9, count it for (int i = 0; i < randomNumbers.length; i++) { counts[randomNumbers[i]]++; } // return the array return counts; } public static int[] checkOccurs(int[] counts) { for (int i = 0; i < counts.length; i++) { if (counts[i] < 1) counts[i] = 1; } return counts; } public static void displayDoubleCounts(int[] occurs) { for (int i = 0; i < occurs.length; i++) { if (occurs[i] > 1) { System.out.println ("The distinct numbers are: " + i); } } } public static void displayOtherCounts(int[] occurs) { for (int i = 0; i < occurs.length; i++) { if (occurs[i] == 1) { System.out.println ("The distinct numbers are: " + i); } } } }[/code] Link to comment Share on other sites More sharing options...
Jamie_B Posted March 13, 2007 Report Share Posted March 13, 2007 I know you said you dont have jbuilder at work, but do you remember the exact error you get? Link to comment Share on other sites More sharing options...
Guest bengalrick Posted March 13, 2007 Report Share Posted March 13, 2007 [quote name='Jamie_B' post='455872' date='Mar 13 2007, 10:52 AM']I know you said you dont have jbuilder at work, but do you remember the exact error you get?[/quote] actually, i wasn't getting an error... what would display was: "The ten distict numbers are:" <and then 10 random numbers between 0-9 would be here> then it would print any duplicates that were in there.... for instance if it gave me (7,7,1,1,3,3,9,4,0), it would print the 7, 1, and 3 because there were all more than 2 instances (and this code takes care of this problem [code]public static int[] checkOccurs(int[] counts) { for (int i = 0; i < counts.length; i++) { if (counts[i] < 1) counts[i] = 1; } return counts; }[/code] the problem is that when i do the displayOtherCounts method, that displays all numbers... my code might be hard to understand, but i pretty much do a count for every number between 0-9 and then if the count if higher than 1, i change it to 1 (to knock out distinct numbers) but when i try to figure out if counts = 1, everything else prints up instead of only the random numbers that were generated... so w/ my example above, i want it to print (7,1,3,9,4,0), but instead it prints (7,1,3,0,2,4,5,6,8,9) for some reason... i think my problem is that i didn't sent count[i] = 0, and b/c of that, all count[i] values are automatically set to 1 and my code messes up... hope i explained myself well... there was probably an easier way of figuring this problem out, but this is what i came up with... thanks jamie... Link to comment Share on other sites More sharing options...
Jamie_B Posted March 13, 2007 Report Share Posted March 13, 2007 Here is where Im getting tripped up. [code]public static int[] countNumbers(int[] randomNumbers) { // Declare and create an array of 10 numbers. int[] counts = new int[10]; // For each number from 0 - 9, count it for (int i = 0; i < randomNumbers.length; i++) { counts[randomNumbers[i]]++; } // return the array return counts; }[/code] By the time you hit this method, you have created a 10 number array (randomNumbers) that may have duplicates. Yes you need a counts = 0; outside of your for loop, but I think if Im understanding your logic you also need some checking within the for loop, as you want to check to see if the randomNumbers has any duplicates. And you createing another arrray (counts) where you want to keep track of the number of dupes you have? But thats nots what goes on in this method. [code]public static int[] countNumbers(int[] randomNumbers) { // Declare and create an array of 10 numbers. int[] counts = new int[10]; [color="#FF0000"]** here you have your randomNumbers array and now a new counts array, with your randomNumbers array having the 10 numbers in it with possible dupes and the counts array being empty**[/color] [color="#FF0000"]**yes you need a count = 0; here, but lets follow the logic as to what happens even with it**[/color] // For each number from 0 - 9, count it for (int i = 0; i < randomNumbers.length; i++) { [color="#FF0000"]**you loop for loop 10 times as your randomNumbers array has a length of the 10 numbers it stored.**[/color] counts[randomNumbers[i]]++; [color="#FF0000"]** this is where im getting tripped up. Your going through each number in your randomNumbers array, getting its value (which is that a whole number or a decimal?, if its a decimal you might want to trim it) taking that random number which is one through 10 adding 1 to it and using it as your position in the counts array[/color] } // return the array return counts; }[/code] can you better explain that counts[randomNumbers[i]]++; line? Link to comment Share on other sites More sharing options...
Guest bengalrick Posted March 13, 2007 Report Share Posted March 13, 2007 [quote name='Jamie_B' post='455923' date='Mar 13 2007, 01:00 PM']Here is where Im getting tripped up. [code]public static int[] countNumbers(int[] randomNumbers) { // Declare and create an array of 10 numbers. int[] counts = new int[10]; // For each number from 0 - 9, count it for (int i = 0; i < randomNumbers.length; i++) { counts[randomNumbers[i]]++; } // return the array return counts; }[/code] By the time you hit this method, you have created a 10 number array (randomNumbers) that may have duplicates. Yes you need a counts = 0; outside of your for loop, but I think if Im understanding your logic you also need some checking within the for loop, as you want to check to see if the randomNumbers has any duplicates. And you createing another arrray (counts) where you want to keep track of the number of dupes you have? But thats nots what goes on in this method. [code]public static int[] countNumbers(int[] randomNumbers) { // Declare and create an array of 10 numbers. int[] counts = new int[10]; [color="#FF0000"]** here you have your randomNumbers array and now a new counts array, with your randomNumbers array having the 10 numbers in it with possible dupes and the counts array being empty**[/color] [color="#FF0000"]**yes you need a count = 0; here, but lets follow the logic as to what happens even with it**[/color] // For each number from 0 - 9, count it for (int i = 0; i < randomNumbers.length; i++) { [color="#FF0000"]**you loop for loop 10 times as your randomNumbers array has a length of the 10 numbers it stored.**[/color] counts[randomNumbers[i]]++; [color="#FF0000"]** this is where im getting tripped up. Your going through each number in your randomNumbers array, getting its value (which is that a whole number or a decimal?, if its a decimal you might want to trim it) taking that random number which is one through 10 adding 1 to it and using it as your position in the counts array[/color] } // return the array return counts; }[/code] can you better explain that counts[randomNumbers[i]]++; line?[/quote] the counts[randomNumbers[i]]++ line will build a count for counts[i] whatever i is... therefore, whatever randomNumbers equals, it will add another count to counts[i]... what might be confussing you about my program is that i am not actually dismissing any duplicates, but am filtering through them with method checkOccurs and then only printing them with the last two methods... i am seeing somewhat of what your saying, but i got real busy at work all the sudden, so i'll have to figure this out better later on tonight.. thanks again for your help... Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.