The Artima Developer Community
Sponsored Link

Java Answers Forum
Order for school: write little program for guessing a number

3 replies on 1 page. Most recent reply: Feb 22, 2003 6:31 PM by Charles Bell

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
Agnes

Posts: 1
Nickname: hopeless01
Registered: Feb, 2003

Order for school: write little program for guessing a number Posted: Feb 18, 2003 3:04 AM
Reply to this message Reply
Advertisement
I do'nt know many of Java and i would be very kind if anyone could help me. I heve to create a java program for school. The meaning is: Write a java program for playing against the computer. You have to guess a number of 4 nubbers (0000 tot 9999). It has to be a random number (Math.random()). The user of the program can try to guess the number 10 times. After every try the program says if the guessed number is too high, too low or guessed right and if guessed, in how many times.
At the start of the programm, a short explanation must appear.
I really do not understand how i can do this and i hope somebody can help me. At forehand thanks for this. My email is koekiemonster1002@hotmail.com.


Chris Staplehurst

Posts: 2
Nickname: allymceff
Registered: Feb, 2003

Re: Order for school: write little program for guessing a number Posted: Feb 18, 2003 4:01 PM
Reply to this message Reply
u on msn messengar?

deliquentMind

Posts: 3
Nickname: deliquent
Registered: Feb, 2003

Re: Order for school: write little program for guessing a number Posted: Feb 21, 2003 8:31 AM
Reply to this message Reply
code this: if you have problem, go back here

set mystery equal to call random(0,9999)
set right equal to true
set counter equal to 1
for (counter equal to 1;
counter less than or equal 10;
increment counter by 1)
begin
print "Enter guess : "
read guess

if (guess equal to msystery)
set right equal to true
else if (guess is less than mystery)
print "too low."
else
print "too high."

if (right equal to true)
set counter equal to 10
increment tries by 1
end
if (right equal to true)
print"you guess it, it takes " + tries + " try."
else
print"sorry, mystery is " + mystery

Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: Order for school: write little program for guessing a number Posted: Feb 22, 2003 6:31 PM
Reply to this message Reply

import java.io.*;

public class NumberGuess{

public static void main (String[] args){
boolean guessedNumber = false;
int tries = 0;
int random = (int) (Math.random() * 1000);
int maximumAllowedAttempts = 10;
String message = "Enter a four digit number between 0 and 9999";
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
while ((tries < maximumAllowedAttempts) && !guessedNumber){
try{
System.out.println(message);
int guess = Integer.parseInt(br.readLine());
tries++;
int triesLeft = maximumAllowedAttempts - tries;
if ((guess < 0) || (guess > 9999)) {
System.out.println("Bad guess. " + message);
}else if(guess < random){
System.out.println("Your number was too low.");
if (triesLeft > 1) {
System.out.println("You have " + String.valueOf(triesLeft) + " tries left.");
}else if (triesLeft == 1){
System.out.println("You have one more try.");
}else{
System.out.println("Sorry! You are out of tries.");
}

}else if(guess > random){
System.out.println("Your number was too high.");
if (triesLeft > 1) {
System.out.println("You have " + String.valueOf(triesLeft) + " tries left.");
}else if (triesLeft == 1){
System.out.println("You have one more try.");
}else{
System.out.println("Sorry! You are out of tries.");
}
}else if(guess == random){
System.out.println("Your number was correct.");
guessedNumber = true;
}
}catch (NumberFormatException nfe){
System.err.println("Bad guess. " + message);
}catch (IOException ioe){
System.err.println("IOException: " + ioe.getMessage());
}
}
}
}

Flat View: This topic has 3 replies on 1 page
Topic: ActionListener in JTabbedPane Previous Topic   Next Topic Topic: help with small program - pretty basic

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use