match.java

来自「java 编程的一些简单例子」· Java 代码 · 共 79 行

JAVA
79
字号
import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class match extends Applet implements ActionListener
{
	Label prompt,result,bonus;
	TextField input1;
	Button btn1,btn2,btn3;
	int y=0,r=0,j=0,randomNum,n=0;
	public void init()
	{
		prompt=new Label("请输入(1-3)火柴数:");
		bonus=new Label("           Come on! ");
		input1=new TextField(5);
		btn1=new Button("You First!");
		btn2=new Button("Jsj First!");
		btn3=new Button("Go!");
		result=new Label("目前火柴数:");
		add(prompt);
		add(input1);
		add(result);
		add(btn1);
		add(btn2);
		add(btn3);
		add(bonus);
		input1.addActionListener(this);
		btn1.addActionListener(this);
		btn2.addActionListener(this);
		btn3.addActionListener(this);
	}
	public void actionPerformed(ActionEvent e)
	{
		        y=(int)(Math.random()*40)+10;
		        result.setText("目前火柴数:"+y);
		        if(e.getSource()==btn3)
				{
					y=randomNum=(int)(Math.random()*40)+10;
			        result.setText("目前火柴数:"+y);
				}
				if(e.getSource()==btn1)
				{
					while(y>0)
					{
						if(randomNum%4==0)
						{
							r=Integer.parseInt(input1.getText());
							y-=r;
							result.setText("目前火柴数:"+y);
							j=4-r;
							y-=j;
							result.setText("目前火柴数:"+y);
						}
						if(randomNum%4!=0)
						{
							r=Integer.parseInt(input1.getText());
							y-=r;
							result.setText("目前火柴数:"+y);
							if(y%4!=0)
							{
								j=y%4;
								n++;
							}
							else							
								j=(int)(Math.random()*3)+1;							
								y-=j;
								result.setText("目前火柴数:"+y);
							
						}
					}
					if(y==0)
						bonus.setText("           You Lost!");
				}
	}
}

		

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?