Results 1 to 12 of 12
Thread: [Q] Generate Random Letter
- 01-30-2011, 07:10 PM #1
Member
- Join Date
- Jan 2011
- Location
- Ohio
- Posts
- 11
- Rep Power
- 0
[SOLVED] Generate Random Letter
Hi! I am very new to Java. This is my first time taking a class on it and for an assignment I have to make a game that generates a random number and has the player guess it within 4 tries. Unfortunately, I cannot figure out how to generate a random letter. I have been searching for around an hour and cannot figure it out! From what I have gathered, I have make a string containing my letters.
Then, I am supposed to generate a random number from 0-25.Java Code:String[] letters = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
Then, I am supposed to assign the randomly generated number to it's letter equivalent. And this is where I'm lost. I have no idea where to go from this point. I can't seem to figure out what code I can use to assign the number to it's corresponding letter. Any help is GREATLY appreciated! Thanks so much!Java Code:int num = (int)(Math.random() * 26);
PS: I'm sorry if this has been a recurring question. I have looked in so many forums and read so many different threads, that my brain feels all squishy. So please don't hate me too much for this.Last edited by iriscience; 01-31-2011 at 06:28 AM. Reason: solved
- 01-30-2011, 07:28 PM #2
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
letters[num] :confused:
- 01-30-2011, 07:30 PM #3
Member
- Join Date
- Jan 2011
- Location
- Gainesville, FL
- Posts
- 45
- Rep Power
- 0
The number you generated is between 0 and 25, which is perfect, because that corresponds to the elements in the array you created.
Say the number you generated is 4, and you want this to be converted to "E" (where 0 is "A").
will assign the letter at element [num] to the choice. Generate a 3, you have "D". Generate a 25, you have "Z".Java Code:String choice = letters[num];
- 01-30-2011, 07:31 PM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,560
- Rep Power
- 11
Java Code:String[] letters = ... int num = ... String randomLetter = letters[num];
The expression letters[num] means "the n-th element of the letters array". See Arrays in Oracle's Tutorial, or your textbook.
--------------------------
"Just the place for a Snark!" the Bellman cried,
As he landed his crew with care;
Supporting each man on the top of the tide
By a finger entwined in his hair.
"Just the place for a Snark! I have said it twice:
That alone should encourage the crew.
Just the place for a Snark! I have said it thrice:
What i tell you three times is true."
- 01-30-2011, 07:31 PM #5
Here is a pretty easy way to do it. Have the String[] with all the letters then get one of them using a randomly generated number from 0 to 25. Like this:
Java Code:String[] letters = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"}; public static String randomLetter() { int num = (int)(Math.random() * 26); return letters[num]; }
- 01-30-2011, 07:36 PM #6
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,560
- Rep Power
- 11
Two more and I get to quote A. A. Milne...
- 01-30-2011, 07:48 PM #7
Member
- Join Date
- Jan 2011
- Location
- Ohio
- Posts
- 11
- Rep Power
- 0
-
- 01-30-2011, 10:25 PM #9
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,560
- Rep Power
- 11
You're welcome.Thank you so much!
-
OK, it's killing me, and sorry to be off topic, but what A. A. Milne quote were you talking about??
- 01-30-2011, 11:14 PM #11
This can be achieved without using an array for the letters. A hint:
Now all you need is to generate a random number in the correct range.Java Code:System.out.println((char)65);
- 01-31-2011, 12:10 AM #12
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,560
- Rep Power
- 11
but what A. A. Milne quote were you talking about?
Well I was the third person to suggest accessing the array at the index that had been calculated. Hence the Snark quote.
Then Zman3359 suggested the same thing (4).
And I thought "Two more and ... Now We Are Six"
------------------
When I was One,
I had just begun.
When I was Two,
I was nearly new.
When I was Three
I was hardly me.
When I was Four,
I was not much more.
When I was Five,
I was just alive.
But now I am Six,
I'm as clever as clever,
So I think I'll be six now for ever and ever.
Similar Threads
-
Random integer generate
By trbLeeciN in forum New To JavaReplies: 6Last Post: 06-22-2010, 01:19 AM -
How do I generate random numbers in a certain range using the random class?
By frasifrasi in forum New To JavaReplies: 8Last Post: 04-19-2009, 05:50 PM -
Trying to Generate Random number
By PeterFeng in forum New To JavaReplies: 10Last Post: 01-14-2009, 08:37 AM -
Generate a random number
By romina in forum New To JavaReplies: 1Last Post: 08-07-2007, 05:23 AM -
How to generate random number in java
By fernando in forum New To JavaReplies: 1Last Post: 08-01-2007, 07:32 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks