java applet小程序猜数字代码求改正
//猜数字小游戏importjava.applet.*;importjava.awt.*;importjava.awt.event.*;importjava.util.*...
//猜数字小游戏
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class JavaAppGraphicsInOut extends Applet implements ActionListener
{
Label prompt;
TextField input,output;
Button btn;
Image myImage;
public void init()//实现Applet的init方法,初始化界面
{
prompt=new Label("请输入您猜的数字");
input=new TextField(6);
output=new TextField(6);
btn=new Button("关闭");
myImage=getImage(getDocumentBase(),"pict.JPG");
setLayout(new FlowLayout());
add(prompt);
add(input);
add(output);
add(btn);
input.addActionListener(this);
btn.addActionListener(this);
setSize(600,450);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==input)
output.setText(input.getText());
else//未设置关闭按钮的功能
{
System.exit(0);
}
}
public void paint(Graphics g)
{
g.drawString("您要加油啊",20,60);
g.drawImage(myImage,30,80,this);
}
void GuessNumber()
{
System.out.println("请输入一个数字:");
int lintTargetNum=30;
Scanner scanner=new Scanner(System.in);//scanner少了个n
while(true)
{
int lintGuessNumber=scanner.nextInt();
if(lintGuessNumber>lintTargetNum)
{
System.out.println("太大了");
continue;
}
if(lintGuessNumber<lintTargetNum)
{
System.out.println("太小了");//少了个判断条件
continue;
}
System.out.println("祝贺你猜对了");
break;
}
}
} 展开
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class JavaAppGraphicsInOut extends Applet implements ActionListener
{
Label prompt;
TextField input,output;
Button btn;
Image myImage;
public void init()//实现Applet的init方法,初始化界面
{
prompt=new Label("请输入您猜的数字");
input=new TextField(6);
output=new TextField(6);
btn=new Button("关闭");
myImage=getImage(getDocumentBase(),"pict.JPG");
setLayout(new FlowLayout());
add(prompt);
add(input);
add(output);
add(btn);
input.addActionListener(this);
btn.addActionListener(this);
setSize(600,450);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==input)
output.setText(input.getText());
else//未设置关闭按钮的功能
{
System.exit(0);
}
}
public void paint(Graphics g)
{
g.drawString("您要加油啊",20,60);
g.drawImage(myImage,30,80,this);
}
void GuessNumber()
{
System.out.println("请输入一个数字:");
int lintTargetNum=30;
Scanner scanner=new Scanner(System.in);//scanner少了个n
while(true)
{
int lintGuessNumber=scanner.nextInt();
if(lintGuessNumber>lintTargetNum)
{
System.out.println("太大了");
continue;
}
if(lintGuessNumber<lintTargetNum)
{
System.out.println("太小了");//少了个判断条件
continue;
}
System.out.println("祝贺你猜对了");
break;
}
}
} 展开
2个回答
展开全部
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class JavaAppGraphicsInOut extends Applet implements ActionListener,TextListener
{
Label prompt,result;
TextField input,output;
Button btn,start;
Image myImage;
public void init()//实现Applet的init方法,初始化界面
{
prompt=new Label("请输入您猜的数字(1-10)");
result=new Label("尚未输入数字");
input=new TextField(6);
output=new TextField(6);
start=new Button("猜");
btn=new Button("关闭");
myImage=getImage(getCodeBase(),"pict.JPG"); //getDocumentBase()返回的是HTML文档的URL
//setLayout(new FlowLayout());
add(prompt);
add(input);
add(output);
add(start);
add(result);
add(btn);
input.addTextListener(this);
start.addActionListener(this);
btn.addActionListener(this);
setSize(600,450);
//setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource()==start) {
if (!input.getText().trim().equals("")) //判断输入框是否为空
GuessNumber();
}
}
public void paint(Graphics g)
{
g.drawString("您要加油啊",20,60);
g.drawImage(myImage,30,80,this);
}
public void textValueChanged(TextEvent e) {
output.setText(input.getText());
}
void GuessNumber()
{
int lintGuessNumber=Integer.parseInt(input.getText());
int lintTargetNum=(int)(Math.random()*10);
if(lintGuessNumber>lintTargetNum)
{
result.setText("太大了");
}
if(lintGuessNumber<lintTargetNum)
{
result.setText("太小了");
}
if (lintGuessNumber==lintTargetNum)
result.setText("祝贺你猜对了");
}
}
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class JavaAppGraphicsInOut extends Applet implements ActionListener,TextListener
{
Label prompt,result;
TextField input,output;
Button btn,start;
Image myImage;
public void init()//实现Applet的init方法,初始化界面
{
prompt=new Label("请输入您猜的数字(1-10)");
result=new Label("尚未输入数字");
input=new TextField(6);
output=new TextField(6);
start=new Button("猜");
btn=new Button("关闭");
myImage=getImage(getCodeBase(),"pict.JPG"); //getDocumentBase()返回的是HTML文档的URL
//setLayout(new FlowLayout());
add(prompt);
add(input);
add(output);
add(start);
add(result);
add(btn);
input.addTextListener(this);
start.addActionListener(this);
btn.addActionListener(this);
setSize(600,450);
//setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource()==start) {
if (!input.getText().trim().equals("")) //判断输入框是否为空
GuessNumber();
}
}
public void paint(Graphics g)
{
g.drawString("您要加油啊",20,60);
g.drawImage(myImage,30,80,this);
}
public void textValueChanged(TextEvent e) {
output.setText(input.getText());
}
void GuessNumber()
{
int lintGuessNumber=Integer.parseInt(input.getText());
int lintTargetNum=(int)(Math.random()*10);
if(lintGuessNumber>lintTargetNum)
{
result.setText("太大了");
}
if(lintGuessNumber<lintTargetNum)
{
result.setText("太小了");
}
if (lintGuessNumber==lintTargetNum)
result.setText("祝贺你猜对了");
}
}
2011-03-14
展开全部
是你吗?王骏
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询