关闭

java ---- 猜数字 & 计算器

609人阅读 评论(0) 收藏 举报
分类:



import javax.swing.JOptionPane;

public class S1WrapUp {

	public static void main(String [] arge){
		// 该变量用于记录用户猜的数字。
		int gusee=-1;
		int count =0;
		int num=(int)(Math.random()*100);
		do {
			//用JOptionPane做为输入流
			gusee=Integer.parseInt(JOptionPane.showInputDialog("guess a number between 0 and 100"));
			if(gusee>num){
				JOptionPane.showMessageDialog(null, "too high");
			}
			if(gusee<num){
				JOptionPane.showMessageDialog(null, "too low");
			}
			count++;
		} while (num!=gusee);
		JOptionPane.showMessageDialog(null, "you gusee the number"+num+"in"+count+"guess(es)!!!");
	}
}


import java.applet.Applet;
import java.awt.Button;
import java.awt.Checkbox;
import java.awt.CheckboxGroup;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

public class Calculator extends Applet {
	TextField text1, text2, text3;
	Button button1, button2;
	CheckboxGroup select;
	Checkbox a, s, m, d;			//+-的选框

	public void init() {
		text1 = new TextField("0", 7);
		text2 = new TextField("0", 7);
		text3 = new TextField(10);
		button1 = new Button("计算");
		button2 = new Button("重置");
		select = new CheckboxGroup();
		a = new Checkbox("加", true, select);
		s = new Checkbox("减", false, select);
		m = new Checkbox("乘", false, select);
		d = new Checkbox("除", false, select);
		add(a);
		add(s);
		add(m);
		add(d);
		add(text1);
		add(text2);
		add(text3);
		add(button1);
		add(button2);
/*		text1.addFocusListener(new HandleFocus());//注册监听事件
		text2.addFocusListener(new HandleFocus());*/
		button1.addActionListener(new ButtonAct());
		button2.addActionListener(new ButtonAct());

	}
//计算数值范围
/*	class HandleFocus implements FocusListener {	//内部类1,焦点事件处理
		public void focusGained(FocusEvent e) {
		}

		public void focusLost(FocusEvent e) {
			int operand;
			TextField text = (TextField) (e.getSource()); //getSource()区分响应事件的button
			operand = Integer.parseInt(text.getText());
			if (operand < 0 | operand > 10000) {		//如果输入数据不对
				text.requestFocus();				//使文本框重新获得焦点
				text.setText("0");					//清除有误数据并置0
			}
		}
	}*/

	class ButtonAct implements ActionListener {			//内部类2,按钮处理
		public void actionPerformed(ActionEvent e) {    //方法表示对象发成操作时调用
			int op1, op2, op3;
			if (e.getSource() == button2) {				//“重置按钮”
				text1.setText("0");
				text2.setText("0");
				text3.setText("");
				text1.requestFocus();					//将焦点置于第一个文本框
				a.setState(true);						//加法为初始选择
			} else {									//计算按钮
					op1 = Integer.parseInt(text1.getText());
					op2 = Integer.parseInt(text2.getText());
					if (a.getState())
						op3 = op1 + op2;
					else if (s.getState())
						op3 = op1 - op2;
					else if (m.getState())
						op3 = op1 * op2;
					else
						op3 = op1 / op2;
					text3.setText(Integer.toString(op3));
				}
			}
	}
}



0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:734018次
    • 积分:11786
    • 等级:
    • 排名:第1269名
    • 原创:420篇
    • 转载:267篇
    • 译文:0篇
    • 评论:36条
    文章分类
    最新评论