|
Newbie
Join Date: Apr 2008
Location: Ft Meade MD
Posts: 6
Rep Power: 0 
|
Re: Random "Letter" Generator
First post on this forum but I have been lurking for a while. It has been a few years since I have used Java so thought I'd give this a go for some practice. Here is what I came up with based largely on your code. References are at the bottom.
import java.util.*; import javax.swing.JOptionPane; public class guessing2 { static Scanner console = new Scanner(System.in); private static boolean isNumeric(String checkStr) // determine if string is numeric { try{ Integer.parseInt(checkStr); return true; //Did not throw, must be a number }catch(NumberFormatException err){ return false; //Threw, So is not a number } } public static void main (String[] args) { int numGuesses = 0, uiInt = 0; boolean done = false; int rndNum = (int)(10.0 * Math.random()); String uiStr = "", rndGuess = ""; char firstChar; String[] Letters = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J"}; rndGuess = Letters[rndNum]; while ((numGuesses < 3) && !done) { uiStr = JOptionPane.showInputDialog("Please enter a letter beween A-J or number between 1-10"); uiStr = uiStr.toUpperCase(); if(isNumeric(uiStr)) // check if user input was numeric // parse string input to integer and subtract // one in order to compare with rndNum // retrieve letter based on numeric input to // compare against rndGuess { uiInt = Integer.parseInt(uiStr) - 1; uiStr = Letters[uiInt]; } else { // if is isn't numeric pull first character // from user input into firstChar // then based on that store the numeric value // in uiInt for comparison with rndNum firstChar = uiStr.charAt(0); switch(firstChar) { case 'A': uiInt = 0; break; case 'B': uiInt = 1; break; case 'C': uiInt = 2; break; case 'D': uiInt = 3; break; case 'E': uiInt = 4; break; case 'F': uiInt = 5; break; case 'G': uiInt = 6; break; case 'H': uiInt = 7; break; case 'I': uiInt = 8; break; case 'J': uiInt = 9; break; default: JOptionPane.showMessageDialog(null, "Invalid Character Input", "Invalid Input", JOptionPane.INFORMATION_MESSAGE); done = true; break; } } if (uiInt < rndNum | uiStr.compareTo(rndGuess) < 0) JOptionPane.showMessageDialog(null, "You're too low!", "Failure", JOptionPane.INFORMATION_MESSAGE); else if (uiInt > rndNum | uiStr.compareTo(rndGuess) > 0) JOptionPane.showMessageDialog(null, "You're too high!", "Failure", JOptionPane.INFORMATION_MESSAGE); else JOptionPane.showMessageDialog(null, "Congratulations you guessed it", "Congratulations", JOptionPane.INFORMATION_MESSAGE); done = true; numGuesses++; } //end while if (numGuesses == 3) JOptionPane.showMessageDialog(null, "You lose, Too many Tries, Correct Letter = " + Letters[rndNum] + " and Correct Number = " + rndNum++, "Failure", JOptionPane.INFORMATION_MESSAGE); } }
The reason I choose the Switch statement was mainly because I don't think that if a user inputs a letter that ".parseDouble" converts it to a numeric value. So my reasoning was that if they only guess letters they'll never succeed with that system. Please correct me if I'm wrong in that logic.
-Manny
http://www.leepoint.net/notes-java/a...andom-api.html
http://bytes.com/forum/thread17584.html ---isNumeric() function
http://www.roseindia.net/java/master...twise-or.shtml
http://www.leepoint.net/notes-java/d...goverview.html
http://www.javabeginner.com/java-string-comparison.htm
http://forum.java.sun.com/thread.jsp...sageID=9698690 ---Converting letters to numbers
http://java.sun.com/docs/books/tutor...ipstrings.html
Last edited by manny85; Apr 12th, 2008 at 11:54 PM.
Reason: Adjusting Code Format
|