Difference between revisions of "Telgar/Problem311"
From Exalted - Unofficial Wiki
m (link fix) |
m |
||
(One intermediate revision by one other user not shown) | |||
Line 1: | Line 1: | ||
− | public class | + | import java.util.*; |
+ | |||
+ | public class Problem311 | ||
{ | { | ||
− | public | + | int guess; |
+ | int enigma; | ||
+ | int counter; | ||
+ | char response; | ||
+ | |||
+ | public Problem311() | ||
{ | { | ||
− | |||
} | } | ||
− | + | public void Random() | |
+ | {} | ||
+ | |||
+ | public void genNum() | ||
+ | { | ||
+ | Random gen = new Random(); | ||
+ | enigma = gen.nextInt(100) +1; | ||
+ | } | ||
+ | |||
+ | public void enterNum() | ||
{ | { | ||
− | + | System.out.print ("The game has started! Please enter your guess: "); | |
− | + | guess = Keyboard.readInt(); | |
− | + | counter = 0; | |
− | |||
− | |||
− | |||
− | System.out. | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
} | } | ||
+ | |||
+ | public void guessAgain() | ||
+ | { | ||
+ | System.out.print ("Enter your new guess:"); | ||
+ | guess = Keyboard.readInt(); | ||
+ | counter ++; | ||
+ | guessNum(); | ||
+ | } | ||
+ | |||
+ | public void checkPlaying() | ||
+ | { | ||
+ | System.out.println ("Do you want to play again? Y/N?"); | ||
+ | response = Keyboard.readChar(); | ||
+ | if (response == 'Y') | ||
+ | { | ||
+ | guessAgain(); | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | System.out.println ("Thanks for playing!"); | ||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | public void guessNum() | ||
+ | { | ||
+ | if (guess > enigma) | ||
+ | { | ||
+ | System.out.println ("You have guessed high."); | ||
+ | checkPlaying(); | ||
+ | } | ||
+ | else if (guess < enigma) | ||
+ | { | ||
+ | System.out.println ("You have guessed low."); | ||
+ | checkPlaying(); | ||
+ | } | ||
+ | else if (guess == enigma) | ||
+ | { | ||
+ | System.out.println ("You guessed right! You took "+ counter +" guesses. Play again? Y/N"); | ||
+ | response = Keyboard.readChar(); | ||
+ | if (response == 'Y') | ||
+ | { | ||
+ | genNum(); | ||
+ | enterNum(); | ||
+ | guessNum(); | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | System.out.print ("Thanks for playing!"); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | public void main() | ||
+ | { | ||
+ | Problem311 game = new Problem311(); | ||
+ | |||
+ | game.genNum(); | ||
+ | game.enterNum(); | ||
+ | game.guessNum(); | ||
+ | } | ||
} | } |
Revision as of 17:20, 17 November 2004
import java.util.*;
public class Problem311 {
int guess; int enigma; int counter; char response;
public Problem311() { } public void Random() {} public void genNum() { Random gen = new Random(); enigma = gen.nextInt(100) +1; } public void enterNum() { System.out.print ("The game has started! Please enter your guess: "); guess = Keyboard.readInt(); counter = 0; } public void guessAgain() { System.out.print ("Enter your new guess:"); guess = Keyboard.readInt(); counter ++; guessNum(); } public void checkPlaying() { System.out.println ("Do you want to play again? Y/N?"); response = Keyboard.readChar(); if (response == 'Y') { guessAgain(); } else { System.out.println ("Thanks for playing!"); } } public void guessNum() { if (guess > enigma) { System.out.println ("You have guessed high."); checkPlaying(); } else if (guess < enigma) { System.out.println ("You have guessed low."); checkPlaying(); } else if (guess == enigma) { System.out.println ("You guessed right! You took "+ counter +" guesses. Play again? Y/N"); response = Keyboard.readChar(); if (response == 'Y') { genNum(); enterNum(); guessNum(); } else { System.out.print ("Thanks for playing!"); } } } public void main() { Problem311 game = new Problem311(); game.genNum(); game.enterNum(); game.guessNum(); }
}