⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lucky52.java

📁 用java 编的Lucky52 游戏 没有提供图片 要运行请自己准备
💻 JAVA
字号:
/*
 * @(#)Lucky52.java 1.0 04/11/05
 *
 * You can modify the template of this file in the
 * directory ..\JCreator\Templates\Template_2\Project_Name.java
 *
 * You can also create your own project template by making a new
 * folder in the directory ..\JCreator\Template\. Use the other
 * templates as examples.
 *
 */

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


public class Lucky52 extends Applet implements ActionListener {
	Panel pNorth = new Panel(); //装载上面得标签
	PicPanel pCenter = new PicPanel();//装载图片
	Panel pBottom = new Panel();      //装载button 
	
	TextField txField = new TextField(10);
	TextField numField = new TextField(4);
	Label label = new Label("请输入你对商品价格得估计:");
	
	private int [] nPrice = {1005,2065,1060,985,560,3910,980,1205,3335,1680,1005,2005};
	private final int GUESS_NUM = 8;
	private	int guessNum = 0;
	
	public void init() {
		//super();
		lucky52();
	}
	public void lucky52() {
		//super();
		this.setLayout(new BorderLayout());
		pNorth.add(label);
		pNorth.add(txField);
		pNorth.add(new Label("剩余的次数:"));
		pNorth.add(numField);
		add(pNorth, BorderLayout.NORTH);
		add(pCenter, BorderLayout.CENTER);
		
		Button btStart = new Button("Start Game");
		Button btCancel = new Button("Cancel");
		Button btOK = new Button("OK");
		
		btStart.setActionCommand("Start");
		btStart.addActionListener(this);
		btOK.setActionCommand("OK");
		btOK.addActionListener(this);
		btCancel.setActionCommand("Cancel");
		btCancel.addActionListener(this);
		
		pBottom.add(btStart);
		pBottom.add(btOK);
		pBottom.add(btCancel);
		add(pBottom, BorderLayout.SOUTH);
		
		setBackground(Color.white);
	}
	
	public void actionPerformed(ActionEvent e) {
		if(e.getActionCommand().equals("Start")) {
			pCenter.initImg();
			label.setText("请输入你对商品价格的估价:");
			numField.setText ("GUESS_NUM");//剩余的可以猜的次数
			guessNum = GUESS_NUM;
			pCenter.repaint();	
		}	else if(e.getActionCommand().equals("OK")) {
			int guessPrice = 0;
			numField.setText (new Integer(--guessNum).toString ());
			if(guessNum > -1){
				try{
					String guess;
					try{
						guessPrice = Integer.parseInt(txField.getText().trim());
						guess = comparePrice(guessPrice);
					}catch(NumberFormatException nExp){//处理输入非法数据:
						System.out.println ("Wrong!!");
						guess = "Something must be Wrong!!";	
					}
					new MsgDlg(guess);
				}	catch(Exception exp){
					exp.printStackTrace();	
				}
			} else{
				new MsgDlg("No time is left : new Game !Plase enter :StartGame:");		
			}
		}else if(e.getActionCommand().equals("Cancel")){
			txField.setText("");	
		}
	}
	
	public String comparePrice(int guessPrice){
		//Math math = new Math();
		
		int i = pCenter.getPictureNumber ();
		if(guessPrice == nPrice[i]){
			return "你猜对了,恭喜你!!";	
		}	else if(guessPrice > nPrice[i]) {
			 if((guessPrice - nPrice[i]) >= 500 )
				 return "啊!你很有钱吗?出价这么高 资助我点吧!^_^开玩笑啦!价格过高你重来吗?";
			 else if( (guessPrice - nPrice[i]) >= 50)
			 	return "啊!有点接近了!不过还有点高,加油啊!";
			 else if((guessPrice - nPrice[i]) >= 30 )	
			 	return "^_^ !接近了 再来一次吧!";
			 return  "就差一点了!减一点吧!";
		} else if(guessPrice < nPrice[i]){
			if(( nPrice[i] - guessPrice) > 500)
				return "haha 价格太低了!你真小看人了!加价吧!";
			if(( nPrice[i] - guessPrice) > 50)
				return "好一些了 不过还有点低!再来一次吧!";
			if(( nPrice[i] - guessPrice) > 20)
				return "yhee 马上就要对了 再加一点就ok了!";
			return "再加一点点 OK?";	
		}else
		return "!!Something must be Wrong!!";
	}

	public void paint(Graphics g) {
		g.drawString("Welcome to Java!!", 50, 60 );
	}
}

⌨️ 快捷键说明

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