Refactor
:my
=>
'code'
Codes
Refactorings
Popular
Best
Submit
Spam
Account
Logout
Login
JavaScript doesn't seem to be activated, expect things to be ugly and sloppy!
Learn How to Create Your Own Programming Language
createyourproglang.com
Recent
Simple Particle Engine for a shooter game
Snake / Nibbles clone in C and Ncurses
Please improve
Parsing of XML data has high CPU usage
Convert simple Javascript to jQuery plugin
Active Record getting unique records
List the files in a directory without the directory name or the extension
clean the code
ohs system, recruitment software, hr software, oh&s software, human resources software, ohs software
Array parsing in a block
Popular
Parsing of XML data has high CPU usage
Please improve
Snake / Nibbles clone in C and Ncurses
List the files in a directory without the directory name or the extension
Convert simple Javascript to jQuery plugin
Simple Particle Engine for a shooter game
Active Record getting unique records
Breadth first cartesian product iterator
php refactoring
first BST
Pastable version of
number guessing game 1000 to 9999
<pre class='prettyprint' language='actionscript'>import java.util.ArrayList; import java.util.Random; import javax.swing.JOptionPane; public class Assignment2 { ArrayList<Integer> num; public Assignment2 ( ){ // fill in code here // initialization num= new ArrayList<Integer>(); } public int myGuessIs() { // fill in code here } public int totalNumGuesses() { // fill in code here // this should return the total number of guesses taken } public void updateMyGuess(int nmatches) { // fill in code here // update the guess based on the number of matches claimed by the user } // fill in code here (optional) // feel free to add more methods as needed // you shouldn't need to change the main function public static void main(String[] args) { Assignment2 gamer = new Assignment2( ); JOptionPane.showMessageDialog(null, "Think of a number between 1000 and 9999.\n Click OK when you are ready...", "Let's play a game", JOptionPane.INFORMATION_MESSAGE); int numMatches = 0; int myguess = 0; do { myguess = gamer.myGuessIs(); if (myguess == -1) { JOptionPane.showMessageDialog(null, "I don't think your number exists.\n I could be wrong though...", "Mistake", JOptionPane.INFORMATION_MESSAGE); System.exit(0); } String userInput = JOptionPane.showInputDialog("I guess your number is " + myguess + ". How many digits did I guess correctly?"); // quit if the user input nothing (such as pressed ESC) if (userInput == null) System.exit(0); // parse user input, pop up a warning message if the input is invalid try { numMatches = Integer.parseInt(userInput.trim()); } catch(Exception exception) { JOptionPane.showMessageDialog(null, "Your input is invalid. Please enter a number between 0 and 4", "Warning", JOptionPane.WARNING_MESSAGE); numMatches = 0; } // the number of matches must be between 0 and 4 if (numMatches < 0 || numMatches > 4) { JOptionPane.showMessageDialog(null, "Your input is invalid. Please enter a number between 0 and 4", "Warning", JOptionPane.WARNING_MESSAGE); numMatches = 0; } if (numMatches == 4) break; // update based on user input gamer.updateMyGuess(numMatches); } while (true); // the game ends when the user says all 4 digits are correct System.out.println("Aha, I got it, your number is " + myguess + "."); System.out.println("I did it in " + gamer.totalNumGuesses() + " turns."); } }</pre> <a href="http://www.refactormycode.com/codes/1172-number-guessing-game-1000-to-9999" style="color:#fff" title="As seen on RefactorMyCode.com"><img alt="Small_logo" src="http://www.refactormycode.com/images/small_logo.gif" style="border:0" /></a>