![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#11 |
|
Newbie
Join Date: Apr 2008
Location: Ft Meade MD
Posts: 6
Rep Power: 0
![]() |
Re: Random "Letter" Generator
Ok that is what I thought. It's how I implemented my isNumeric() method above. I just wanted to make sure I was reading kewlgeyes's code correctly in that when he runs it and enters a letter his
java Syntax (Toggle Plain Text)
will throw an exception if a letter is stored in "yguess". Thanks again for the clarification titaniumdecoy. |
|
|
|
|
|
#12 | |
|
Programmer
Join Date: Jan 2008
Posts: 54
Rep Power: 1
![]() |
Quote:
I am trying to pull up a window that allows a user to enter a number or a letter between 1-10 or A-J and depending on what they choose, the program will generate a random number or letter depending on the users first input. The user types A a new window pops up and asks you to guess a letter between A-J and you have 3 trys, if you enter z or a lowercase then you receive an error window. If the user enters 2 in the first window then you are taken to a new window that asks you to guess a number between 1-10 and you have 3 trys, likewise, if you enter 20 you will receive an invalid number error in a window, otherwise the program terminates with a window that says "Sorry you had three tries, the number or letter was "?" <------- whatever the number or letter was, but it should not display both random number or letter, just the one that you were guessing on. Thank you all for your help and recommendations on this. Manny, I tried your program and it works great, it should just show the letter or number though, and the letters are ok, but the numbers are 1 behind, meaning that if the actual number to be guessed is 4 I should enter a 5 in order to get it correct. But way to go, the program itself works. Nice job! ![]() |
|
|
|
|
|
|
#13 | |
|
Programmer
Join Date: Jan 2008
Posts: 54
Rep Power: 1
![]() |
Re: Random "Letter" Generator
Quote:
I was wondering this would make sense, but since I am knew to Java, I hope this won't sound silly. But what if you wrapped if statements around this whole code, and created like I am trying to do but haven't yet, a window that first checks to see if the input is a number or a letter and then goes to the appropriate code which would make the "guess = Double.parseDouble(yguess);" ok my poor psuedocode java Syntax (Toggle Plain Text)
|
|
|
|
|
|
|
#14 |
|
Newbie
Join Date: Apr 2008
Location: Ft Meade MD
Posts: 6
Rep Power: 0
![]() |
Re: Random "Letter" Generator
I believe this pseudo code should work for you. You are on the right track with the input validation (making sure users enter uppercase letters and numbers). I've also added that into the pseudo code so you can have some idea where it could go. I tried to bracket some of the code off to prevent confusion but let me know if you're having issues. If anyone else sees any holes or improvements in the pseudo code feel free to comment.
import java.lang*
import java.util.*;
import javax.swing.JOptionPane;
Declare your variables
Tell user to enter A for random letter or 1 for for random number.
if input = A then
{
while (attempts < 3 && !done)
{
rndGuess = Letter(rndNum (0-9))
ask user to pick a letter between A-J
if user input.isUpperCase() then
if rndGuess.CompareTo(userInputStr) < 0 then
guessed to low
else
{
if rndGuess.CompareTo(userInputStr) > 0 then
guessed too high
Else
guessed too correctly
done = true
}
attempts = attempts + 1
else
tell user to input only uppercase letters
if attempts > 3 then
tell user the correct number
} // end while
}
else
{
if input.isNumeric() then
userInputInt = Integer.parseInt(input)
if userInputInt == 1 then
while (attempts < 3 && !done)
{
generate rndNum(1-10)
ask user to pick a number between 1-10
if user input.isNumeric()
userInputInt = Integer.parseInt(userInputInt)
if userInputInt < rndGuess then
guessed too low.
else
{
if userInputInt > rndGuess then
guessed to high
else
guessed correctly
done = true
}
attempts = attempts + 1
Else
tell user to enter only numbers.
if attempts > 3 then
tell user the correct letter.
} // end while
else
tell user to enter to either A (letters) or 1 (numbers) only
}http://java.sun.com/j2se/1.3/docs/ap...pperCase(char)
__________________
98% of the teenage population will try, does or has tried smoking pot. If you're one of the 2% who hasn't, copy & paste this into your signature |
|
|
|
|
|
#15 | |
|
Newbie
Join Date: Apr 2008
Location: Ft Meade MD
Posts: 6
Rep Power: 0
![]() |
Re: Random "Letter" Generator
Quote:
__________________
98% of the teenage population will try, does or has tried smoking pot. If you're one of the 2% who hasn't, copy & paste this into your signature |
|
|
|
|
|
|
#16 |
|
Expert Programmer
|
Re: Random "Letter" Generator
java.lang.* is imported automatically in any Java program.
|
|
|
|
|
|
#17 |
|
Newbie
Join Date: Apr 2008
Location: Ft Meade MD
Posts: 6
Rep Power: 0
![]() |
Re: Random "Letter" Generator
Well I learn something new everyday
Looks like java.lang.* is the only package that is imported automatically according to http://java.sun.com/docs/books/jls/s...doc.html#26741. Thanks again titaniumdecoy, and I hope the links we've provided help you dig into Java's documentation a little easier kewlgeye.
__________________
98% of the teenage population will try, does or has tried smoking pot. If you're one of the 2% who hasn't, copy & paste this into your signature |
|
|
|
|
|
#18 |
|
Newbie
Join Date: May 2008
Posts: 11
Rep Power: 0
![]() |
Re: Random "Letter" Generator
Instead of using Double.parseDouble, could you turn guess into an int and Integer.parseInt? That would return a whole number, which could then be found with Letters[guess]. I may be wrong, because I'm still in college learning Java, and I'm sure most of these people are far more experienced than I am, but that's just my two cents.
|
|
|
|
|
|
#19 |
|
Professional Programmer
Join Date: Oct 2006
Posts: 257
Rep Power: 2
![]() |
Re: Random "Letter" Generator
Maybe I'm going crazy, but wouldn't a much easier way of doing this program be to generate a value that is between whatever the ASCII values for your letters are, then converting it to a char, and printing out the char?
|
|
|
|
|
|
#20 |
|
Professional Programmer
Join Date: Oct 2006
Posts: 257
Rep Power: 2
![]() |
Re: Random "Letter" Generator
double number = Math.round((Math.random() * 100));
char a = (char)(number); System.out.println(a); Yes... if you force it within the correct range, you can generate a letter between A-J only using Math.random() and converting it to char. The code I provided above isn't in the correct range, but it demonstrates what I mean. I hope I'm not overlooking something else in your code that requires the array. But just a thought. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Templated Random Number Generator | Ben.Dougall | C++ | 5 | Jul 6th, 2007 7:42 PM |
| random Numbers in openGL | csrocker101 | C++ | 5 | Apr 24th, 2007 8:02 PM |
| [vb6] need advice concerning a largescale non-recurring random number generator | chepfaust | Visual Basic | 8 | Jun 8th, 2006 5:04 AM |
| [Python] Password Generator | bulio | Show Off Your Open Source Projects | 2 | Feb 28th, 2006 3:01 AM |
| PHP random quote generator script | grimpirate | PHP | 10 | Sep 24th, 2005 9:22 AM |