HANGMAN - PowerPoint PPT Presentation

About This Presentation
Title:

HANGMAN

Description:

We've played the game, now let's see what goes on behind the scenes.... O. d b ... Getting Input From the Console. class System ... Now let's look at the code! ... – PowerPoint PPT presentation

Number of Views:12562
Avg rating:3.0/5.0
Slides: 11
Provided by: tcnj
Learn more at: https://owd.tcnj.edu
Category:
Tags: hangman

less

Transcript and Presenter's Notes

Title: HANGMAN


1
HANGMAN
Project by Lori Kraus 12/3/03
2
  • Weve played the game, now lets 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 typelength
  • arrayName0 item1
  • arrayName1 item2
  • type arrayName item0, item1, , itemN
  • Heres what I did
  • private String wordBank "computer",
    "science", "project", "object", "binary",
    "application", "applet", "website", "method",
    "variable"

4
Generating a Random Number
  • class Math
  • Random numbers
  • Math.random( ) produces a double value between
  • 0 and 1
  • Max and min
  • Exponents
  • Square roots, etc.
  • Heres what I did
  • randomNum
  • (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
  • wordArray new charlength
  • 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

6
System.out.println
  • Suppose int answer 5
  • We know
  • System.out.println(answer answer)
  • 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)
  • will look like answer
  • 5

7
  • Heres what I did
  • System.out.println("\nYou win!\n")
  • It looks like
  • You win!
  • Also, in order to print a backslash ( \ ), you
    have to type
  • System.out.println(\\)

8
Getting Input From the Console
  • class System
  • 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
  • class BufferedReader
  • 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

9
Common Example
  • BufferedReader inputGuess new BufferedReader (
    new InputStreamReader (System.in))
  • System.out.print("\nGuess a letter")
  • String guess inputGuess.readLine( )
  • This takes whatever letter the user types in and
    stores it into the String guess

10
  • Now lets look at the code!
Write a Comment
User Comments (0)
About PowerShow.com