
import java.awt.*
import java.applet.*
import java.awt.event.*
import javax.swing.*
public class Calculator extends Applet implements ActionListener
{
String a=null,b=null,c=null,fl=null
double x=0.0f,y=0.0f,z=0.0f
static JFrame f
JPanel pnlUp
JPanel pnlDown
GridLayout gblDown
JButton b0
JButton b1
JButton b2
JButton b3
JButton b4
JButton b5
JButton b6
JButton b7
JButton b8
JButton b9
JButton badd
JButton bsub
JButton bmu
JButton bdiv
JButton bc
JButton beq
JTextField t1
public void init()
{
gblDown=new GridLayout(4,4)
pnlUp=new JPanel()
pnlDown=new JPanel(gblDown)
t1=new JTextField("",15)
t1.setHorizontalAlignment(JTextField.RIGHT)
t1.setEditable(false)
b0=new JButton("0")
b1=new JButton("1")
b2=new JButton("2")
b3=new JButton("3")
b4=new JButton("4")
b5=new JButton("5")
b6=new JButton("6")
b7=new JButton("7")
b8=new JButton("8")
b9=new JButton("9")
badd=new JButton("+")
bsub=new JButton("-")
bmu=new JButton("。チ")
bdiv=new JButton("。ツ")
bc=new JButton("C")
beq=new JButton("=")
b0.addActionListener(this)
b1.addActionListener(this)
b2.addActionListener(this)
b3.addActionListener(this)
b4.addActionListener(this)
b5.addActionListener(this)
b6.addActionListener(this)
b7.addActionListener(this)
b8.addActionListener(this)
b9.addActionListener(this)
badd.addActionListener(this)
bsub.addActionListener(this)
bmu.addActionListener(this)
bdiv.addActionListener(this)
bc.addActionListener(this)
beq.addActionListener(this)
pnlUp.add(t1)
pnlDown.add(b7)
pnlDown.add(b8)
pnlDown.add(b9)
pnlDown.add(badd)
pnlDown.add(b4)
pnlDown.add(b5)
pnlDown.add(b6)
pnlDown.add(bsub)
pnlDown.add(b1)
pnlDown.add(b2)
pnlDown.add(b3)
pnlDown.add(bmu)
pnlDown.add(bc)
pnlDown.add(b0)
pnlDown.add(beq)
pnlDown.add(bdiv)
add(pnlUp)
add(pnlDown)
}
public static void main(String[] args)
{
f=new JFrame("Calculator!")
Applet thisApplet=new Calculator()
thisApplet.init()
f.setBackground(Color.blue)
f.setBounds(300,300,200,200)
f.setResizable(false)
f.add(thisApplet)
f.setVisible(true)
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==badd)
{
if(t1.getText()!="")
{
x=Double.parseDouble(t1.getText())
fl="+"
t1.setText("")
}
else fl="+"
}
else if(e.getSource()==bsub)
{
if(t1.getText()!="")
{
x=Double.parseDouble(t1.getText())
fl="-"
t1.setText("")
}
else fl="-"
}
else if(e.getSource()==bmu)
{
if(t1.getText()!="")
{
x=Double.parseDouble(t1.getText())
fl="*"
t1.setText("")
}
else fl="*"
}
else if(e.getSource()==bdiv)
{
if(t1.getText()!="")
{
x=Double.parseDouble(t1.getText())
fl="/"
t1.setText("")
}
else fl="/"
}
else if(e.getSource()==bc)
{
x=0.0f
y=0.0f
z=0.0f
a=null
b=null
c=null
fl=null
t1.setText("")
}
else if(e.getSource()==beq)
{
y=Double.parseDouble(t1.getText())
if(fl=="+")
{
z=x+y
c=Double.toString(z)
t1.setText(c)
}
else if(fl=="-")
{
z=x-y
c=Double.toString(z)
t1.setText(c)
}
else if(fl=="*")
{
z=x*y
c=Double.toString(z)
t1.setText(c)
}
else if(fl=="/")
{
z=x/y
c=Double.toString(z)
t1.setText(c)
}
}
else if(e.getSource()==b0)
{
t1.setText(t1.getText()+"0")
}
else if(e.getSource()==b1)
{
t1.setText(t1.getText()+"1")
}
else if(e.getSource()==b2)
{
t1.setText(t1.getText()+"2")
}
else if(e.getSource()==b3)
{
t1.setText(t1.getText()+"3")
}
else if(e.getSource()==b4)
{
t1.setText(t1.getText()+"4")
}
else if(e.getSource()==b5)
{
t1.setText(t1.getText()+"5")
}
else if(e.getSource()==b6)
{
t1.setText(t1.getText()+"6")
}
else if(e.getSource()==b7)
{
t1.setText(t1.getText()+"7")
}
else if(e.getSource()==b8)
{
t1.setText(t1.getText()+"8")
}
else if(e.getSource()==b9)
{
t1.setText(t1.getText()+"9")
}
}
}
微信游戏玩法 微信加减大师玩法介绍佚名
2018-05-10 11:38:21
关注
微信中最近上线了“加减大师”这个小程序,在这个游戏中,我们不仅能测试自己的反映能力,而且还能获得奖励哦!那么加减大师小程序怎么玩呢?下面小编就来为大家介绍一下。
微信加减大师怎么玩?
1、微信搜索小程序名称“加减大师”进入小程序。
2、点击开始挑战即可开始,向下翻阅可以查看各榜榜单。
3、游戏很简单,玩家只需要根据题目点击“√”或“×”即可进入下一题,错误就失败了哦。
4、挑战成功即可获得萌萌的奖品娃娃。
5、点击个人中心可以查看个人挑战机会,通过邀请好友还可以增加额外的挑战机会。
import javax.swing.*import java.awt.*
import java.awt.event.*
public class Calculator extends JFrame implements ActionListener{
private static final long serialVersionUID = 8199443193151152362L
private JButton bto_s=new JButton("sqrt"),bto_zf=new JButton("+/-"),bto_ce=new JButton("CE"),bto_c=new JButton("C"),bto_7=new JButton("7"),
bto_8=new JButton("8"),bto_9=new JButton("9"),bto_chu=new JButton("/"),bto_4=new JButton("4"),bto_5=new JButton("5"),
bto_6=new JButton("6"),bto_cheng=new JButton("*"),bto_1=new JButton("1"),bto_2=new JButton("2"),bto_3=new JButton("3"),
bto_jian=new JButton("-"),bto_0=new JButton("0"),bto_dian=new JButton("."),bto_deng=new JButton("="),bto_jia=new JButton("+")
JButton button[]={bto_s,bto_zf,bto_ce,bto_c,bto_7,bto_8,bto_9,bto_chu,bto_4,bto_5,bto_6,bto_cheng,bto_1,bto_2,bto_3,bto_jian,
bto_0,bto_dian,bto_deng,bto_jia}
private JTextField text_double// = new JTextField("0")
private String operator = "="//当前运算的运算符
private boolean firstDigit = true// 标志用户按的是否是整个表达式的第一个数字,或者是运算符后的第一个数字
private double resultNum = 0.0// 计算的中间结果
private boolean operateValidFlag = true//判断 *** 作是否合法
public Calculator()
{
super("Calculator")
this.setBounds(300, 300, 300, 300)
this.setResizable(false)
this.setBackground(Color.orange)
this.setDefaultCloseOperation(EXIT_ON_CLOSE)
this.getContentPane().setLayout(new BorderLayout())//设置布局
text_double=new JTextField("0",20)//设置文本区
text_double.setHorizontalAlignment(JTextField.RIGHT)//设置水平对齐方式未右对齐
this.getContentPane().add(text_double,BorderLayout.NORTH)//将文本区添加到Content北部
JPanel panel=new JPanel(new GridLayout(5,4))//在内容窗口添加一个网格布局
this.getContentPane().add(panel)//添加panel面板
for(int i=0i<button.lengthi++)//在面板上添加按钮
panel.add(button[i])
for(int i=0i<button.lengthi++)
button[i].addActionListener(this)//为按钮注册
text_double.setEditable(false)//文本框不可编辑
text_double.addActionListener(this)//
this.setVisible(true)
}
public void actionPerformed(ActionEvent e)//
{
String c= e.getActionCommand()//返回与此动作相关的命令字符串。
System.out.println("##########command is "+c)
if(c.equals("C")){
handleC() //用户按了“C”键
}
else if (c.equals("CE")) // 用户按了"CE"键
{
text_double.setText("0")
}
else if ("0123456789.".indexOf(c) >= 0) // 用户按了数字键或者小数点键
{
handleNumber(c)// handlezero(zero)
} else//用户按了运算符键
{
handleOperator(c)
}
}
private void handleC() // 初始化计算器的各种值
{
text_double.setText("0")
firstDigit = true
operator = "="
}
private void handleNumber(String button) {
if (firstDigit)//输入的第一个数字
{
text_double.setText(button)
} else if ((button.equals(".")) &&(text_double.getText().indexOf(".") <0))//输入的是小数点,并且之前没有小数点,则将小数点附在结果文本框的后面
//如果字符串参数作为一个子字符串在此对象中出现,则返回第一个这种子字符串的第一个字符的索引;如果它不作为一个子字符串出现,则返回 -1
{
text_double.setText(text_double.getText() + ".")
} else if (!button.equals("."))// 如果输入的不是小数点,则将数字附在结果文本框的后面
{
text_double.setText(text_double.getText() + button)
}
// 以后输入的肯定不是第一个数字了
firstDigit = false
}
private void handleOperator(String button) {
if (operator.equals("/")) {
// 除法运算
// 如果当前结果文本框中的值等于0
if (getNumberFromText() == 0.0){
// *** 作不合法
operateValidFlag = false
text_double.setText("除数不能为零")
} else {
resultNum /= getNumberFromText()
}
} else if (operator.equals("+")){
// 加法运算
resultNum += getNumberFromText()
} else if (operator.equals("-")){
// 减法运算
resultNum -= getNumberFromText()
} else if (operator.equals("*")){
// 乘法运算
resultNum *= getNumberFromText()
} else if (operator.equals("sqrt")) {
// 平方根运算
if(getNumberFromText()<0){
operateValidFlag = false
text_double.setText("被开方数不能为负数")}
else
resultNum = Math.sqrt(resultNum)
}
else if (operator.equals("+/-")){
//正数负数运算
resultNum = resultNum * (-1)
} else if (operator.equals("=")){
//赋值运算
resultNum = getNumberFromText()
}
if (operateValidFlag) {
//双精度浮点数的运算
long t1
double t2
t1 = (long) resultNum
t2 = resultNum - t1
if (t2 == 0) {
text_double.setText(String.valueOf(t1))
} else {
text_double.setText(String.valueOf(resultNum))
}
}
operator = button//运算符等于用户按的按钮
firstDigit = true
operateValidFlag = true
}
private double getNumberFromText() //从结果的文本框获取数字
{
double result = 0
try {
result = Double.valueOf(text_double.getText()).doubleValue()// ValueOf()返回表示指定的 double 值的 Double 实例
} catch (NumberFormatException e){
}
return result
}
public static void main(final String[] args) {
new Calculator()
}
}
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)