The Artima Developer Community
Sponsored Link

Java Answers Forum
HELP?

0 replies on 1 page.

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 0 replies on 1 page
Ernie Douglas

Posts: 18
Nickname: y2j
Registered: Nov, 2002

HELP? Posted: Nov 26, 2002 9:41 AM
Reply to this message Reply
Advertisement
could someone help me with this coding please


import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

public class guessnumber extends Applet implements Runnable, ActionListener
{
String msg;
Thread t = null;
int state;
boolean stopflag;
private Label l1, l2;
private TextField t1, t2;
private Button b1;
private int num, mynum, diff, red, blue, green, chance = 0;
private Color c;

public void init()
{
c = new Color( 0, 80, 0 );
setForeground( c );
l1 = new Label("I have kept a number between 1 and 1000.");
l2 = new Label("Can you guess this number?");
t1 = new TextField(10);
t2 = new TextField(10);
b1 = new Button("Play Again");
t2.setEditable(false);
add(l1);
add(l2);
add(t1);
add(t2);
add(b1);

t 1.addActionListener(this);
b1.addActionListener(this);
num = 1 + (int) (Math.random()*1000);
}

public void start()
{
msg = getParameter( "message" );
if(msg == null)
{
msg = "Message not found";
}
msg = " " + msg;
t = new Thread( this );
stopflag = false;
t.start();
}

public void run()
{
char ch;
for( ; ; )
{
try
{
repaint();
Thread.sleep(200);
ch = msg.charAt(0);
msg = msg.substring( 1, msg.length() );
msg += ch;
if(stopflag)
{
break;
}
}
catch( InterruptedException e)
{
System.out.println( "Exception" + e );
}
}
}

public void stop()
{
stopflag = true;
t = null;
}

public void paint(Graphics g)
{
g.drawString( msg, 70, 130 );
}

public void actionPerformed( ActionEvent ae )
{

if( ae.getSource() == b1 )
{
red = 255;
blue = 255;
green = 255;
c = new Color(red, green, blue);
setBackground(c);
num = 0;
num = 1 + (int) (Math.random()*1000);
chance = 0;
showStatus( "" );
t1.setEditable(true);
t2.setEditable(false);
t1.setText("");
t2.setText("");
}
else
{
mynum = Integer.parseInt(t1.getText());
chance++;
if( chance == 1 )
{
showStatus( chance + " chance over" );
}
else
{
showStatus( chance + " chances over" );
}
if( chance >= 9 )
{
t1.setText( String.valueOf(num) );
showStatus( "Your 9 chances are up" );
t1.setEditable( false );
}
diff = num - mynum;
if(diff == 0)
{
setBackground(Color.pink);
t1.setEditable(false);
t2.setText("You've done it");
t2.setEditable(false);
}
else if(Math.abs(diff)>200)
{
if(diff>0)
{
t2.setText("Too Low");
}
else
{
t2.setText("Too High");
}
}
else if(Math.abs(diff)<200)
{
if(diff>0)
{
red = 255-diff;
green = 0;
blue = 0;
c = new Color(red, green, blue);
t2.setText("");
setBackground(c);
}
else if(diff<0)
{
red = 0;
green = 0;
blue = 255 + diff;
c = new Color(red, green, blue);
t2.setText("");
setBackground(c);
}
}
}
}
}

i need to know how to When the player enters the correct answer, display one of the following messages, depending on how many attempts the player took to get the answer right: If the number of guesses was fewer than 10, print "Congratulations. You guessed the number in n guesses - either you know the secret or got lucky!" where n is the actual number of attempts taken. If the number of guesses was exactly 10, print "Congratulations. You guessed the number in 10 guesses". If the player takes more than 10 guesses, print "You took n guesses. You should be able to do better!" again, where n is the actual number of attempts taken.

once the application is working, i want to modify it to allow the player to enter their first name and the number range to use (e.g., 1 - 100, 20 - 2000, etc...) Change the output to display the player's name as well as the appropriate output message. i aslo need to Calculate the number of guesses that a player should take to find the random number given the inputted range, and use this value, rather than 10, when deciding which of the three final output messages to display when the player successfully guesses the random number.
if someone could show me would be very grateful

Topic: Servlet Communication Previous Topic   Next Topic Topic: New to Java

Sponsored Links



Google
  Web Artima.com   

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