Download presentation
Presentation is loading. Please wait.
1
HANGMAN Project by Lori Kraus 12/3/03
2
WWWWe’ve played the game, now let’s see what goes on behind the scenes…. O \ | / | d b …but first, a few new things
3
Creating an Array 2 ways: type[ ] arrayName = new type[length]; type[ ] arrayName = new type[length]; arrayName[0] = item1; arrayName[0] = item1; arrayName[1] = item2; arrayName[1] = item2; type[ ] arrayName = {item0, item1, …, itemN}; type[ ] arrayName = {item0, item1, …, itemN}; Here’s what I did: private String[ ] wordBank = {"computer", "science", "project", "object", "binary", "application", "applet", "website", "method", "variable"}; private String[ ] wordBank = {"computer", "science", "project", "object", "binary", "application", "applet", "website", "method", "variable"};
4
Generating a Random Number class Math Random numbers Random numbers Math.random( ) produces a double value betweenMath.random( ) produces a double value between 0 and 1 0 and 1 Max and min Max and min Exponents Exponents Square roots, etc. Square roots, etc. Here’s what I did: randomNum = randomNum = (int)((Math.random( ) * 1000) % 10); (int)((Math.random( ) * 1000) % 10);
5
Other Useful Methods int length; String word; char[ ] wordArray; length = word.length( ); assigns the value of the length of the word to int length assigns the value of the length of the word to int length wordArray = new char[length]; creates character array wordArray same length as word creates character array wordArray same length as word word.getChars(0, length, wordArray, 0); puts characters from 0 to length of String word into wordArray, starting at index 0 puts characters from 0 to length of String word into wordArray, starting at index 0
6
System.out.println Suppose int answer = 5 We know System.out.println(“answer = ” + answer); System.out.println(“answer = ” + answer); will look like: answer = 5 will look like: answer = 5 There are also ways to print things on new lines (/n), at the tabs (/t), etc. For example: System.out.println(“answer = /n” + answer); System.out.println(“answer = /n” + answer); will look like: answer = will look like: answer = 5
7
Here’s what I did: System.out.println("\n***You win!***\n"); System.out.println("\n***You win!***\n"); It looks like: It looks like: ***You win!*** ***You win!*** Also, in order to print a backslash ( \ ), you have to type: System.out.println(“\\”); System.out.println(“\\”);
8
Getting Input From the Console class System System.in is the stream corresponding to the keyboard input System.in is the stream corresponding to the keyboard input class InputStreamReader Reads bytes and translates them into characters according to a specified character encoding Reads bytes and translates them into characters according to a specified character encoding class BufferedReader Reads text from a character-input stream, buffering characters so as to provide for more efficient reading Reads text from a character-input stream, buffering characters so as to provide for more efficient reading Method readLine( ) returns a String containing the contents of the particular line Method readLine( ) returns a String containing the contents of the particular line
9
Common Example: BufferedReader inputGuess = new BufferedReader ( new InputStreamReader (System.in)); System.out.print("\nGuess a letter:"); System.out.print("\nGuess a letter:"); String guess = inputGuess.readLine( ); String guess = inputGuess.readLine( ); This takes whatever letter the user types in and stores it into the String guess
10
Now let’s look at the code!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.