永发信息网

求JAVA计算器和猜数的程序代码

答案:3  悬赏:50  手机版
解决时间 2021-01-02 14:42
  • 提问者网友:像風在裏
  • 2021-01-01 21:30
界面要示:用图形界面实现. 1.设计分析,乘,继续猜; 2,猜不中.程序结构(画流程图) 3猜数游戏 功能要求,能实现整数的加,除四则运算,直至猜到,给出所用时间和评语.计算器模拟程序 功能要求:该程序显示gui用户界面:计算机产生随机数,猜中即胜.各模块的功能及程序说明,提示是大了还是小了,减
最佳答案
  • 五星知识达人网友:行雁书
  • 2021-01-22 04:37
9");
bNumber.setForeground(Color.blue);
bNumber.addActionListener(this);
bNumber.setMargin(new Insets(3.addActionListener(this);7");
bNumber.setForeground(Color.blue);
bNumber.addActionListener(this);
bNumber.setMargin(new Insets(3;
bOperator, 3, 2)).addActionListener(this).getContentPane();
JMenuBar mBar = new JMenuBar(); bOther = new JButton("BackSpace&quot, resflag = 1;
int preOp, 3, 5)).add(bM), 3));
bM = new JButton(&quot.WEST);
public class SunnyCalculator implements ActionListener {
JFrame f;
JButton bM, 3));
pDown.add(bNumber).add(bOther);
pDown;
JPanel pDown = new JPanel().red);
bOther;
Container contentPane = f;
mBar.setOpaque(true);
f.setJMenuBar(mBar);
contentPane.setLayout(new BorderLayout());
JPanel pTop = new JPanel().add(bOther);).setLayout(new BorderLayout()), BorderLayout;
bNumber = new JButton("
bNumber = new JButton("/");
bOperator.setForeground(Color.red).setEnabled(false);
pUp;
boolean isDouble = false, 3, 3;
StringBuffer str = new StringBuffer();
public SunnyCalculator() {
f = new JFrame("计算器&quot.setForeground(Color.NORTH);
JPanel pBottom = new JPanel();
pBottom.setLayout(new BorderLayout());
JPanel pLeft = new JPanel();
pLeft.setLayout(new GridLayout(5, 3, 0)), 26);
tResult.setHorizontalAlignment(JTextField.RIGHT);
pDown;
bOperator = new JButton(" pBottom;
pUp;
pUp;
JPanel pUp = new JPanel().add(pLeft;
tResult, 3));
pDown.add(bNumber).setMargin(new Insets(3, 0;
int opFlag = -1;
static double t1 = 0, t2 = 0, t3 = 0, result = 0;
static int opflag1 = -1;
double n3;
StringBuffer buf = new StringBuffer(20);
pLeft, BorderLayout, 1, 3.add(bOperator);
bOperator = new JButton("sqrt");
bOperator.addActionListener(this);
bOperator.setForeground(Color.red);
bOperator.setMargin(new Insets(3, 0, 3, 0));
pDown.add(bOperator);
bNumber = new JButton("4");
bNumber.setForeground(Color.blue);
bNumber.addActionListener(this);
bNumber.setMargin(new Insets(3, 3, 3, 3));
bNumber.setHorizontalTextPosition(JButton.LEFT);
pDown.add(bNumber);
bNumber = new JButton("5");
bNumber.setForeground(Color.blue);
bNumber.addActionListener(this);
bNumber.setMargin(new Insets(3, 3, 3, 3));
pDown.add(bNumber);
bNumber = new JButton("6");
bNumber.setForeground(Color.blue);
bNumber.addActionListener(this);
bNumber.setMargin(new Insets(3, 3, 3, 3));
pDown.add(bNumber);
bOperator = new JButton("*");
bOperator.setForeground(Color.red);
bOperator.addActionListener(this);
bOperator.setMargin(new Insets(3, 3, 3, 3));
pDown.add(bOperator);
bOperator = new JButton("%");
bOperator.setForeground(Color.blue);
bOperator.addActionListener(this);
bOperator.setMargin(new Insets(3, 3, 3, 3));
pDown.add(bOperator);
bNumber = new JButton("1");
bNumber.setForeground(Color.blue);
bNumber.addActionListener(this);
bNumber.setMargin(new Insets(3, 3, 3, 3));
pDown.add(bNumber);
bNumber = new JButton("2");
bNumber.setForeground(Color.blue);
bNumber.addActionListener(this);
bNumber.setMargin(new Insets(3, 3, 3, 3));
pDown.add(bNumber);
bNumber = new JButton("3");
bNumber.setForeground(Color.blue);
bNumber.addActionListener(this);
bNumber.setMargin(new Insets(3, 3, 3, 3));
pDown.add(bNumber);
bOperator = new JButton("-");
bOperator.setForeground(Color.red);
bOperator.addActionListener(this);
bOperator.setMargin(new Insets(3, 3, 3, 3));
pDown.add(bOperator);
bOperator = new JButton("1/x");
bOperator.setForeground(Color.blue);
bOperator.addActionListener(this);
pDown.add(bOperator);
bNumber = new JButton("0");
bNumber.setForeground(Color.blue);
bNumber.addActionListener(this);
bNumber.setMargin(new Insets(3, 3, 3, 3));
pDown.add(bNumber);
bOperator = new JButton("+/-");
bOperator.setForeground(Color.blue);
bOperator.addActionListener(this);
bOperator.setMargin(new Insets(3, 3, 3, 3));
pDown.add(bOperator);
bOperator = new JButton(".");
bOperator.setForeground(Color.blue);
bOperator.addActionListener(this);
bOperator.setMargin(new Insets(3, 3, 3, 3));
pDown.add(bOperator);
bOperator = new JButton("+");
bOperator.setForeground(Color.blue);
bOperator.addActionListener(this);
bOperator.setMargin(new Insets(3, 3, 3, 3));
pDown.add(bOperator);
bOperator = new JButton("=");
bOperator.setForeground(Color.blue);
bOperator.addActionListener(this);
bOperator.setMargin(new Insets(3, 3, 3, 3));
pDown.add(bOperator);
pRight.add(pUp, BorderLayout.NORTH);
pRight.add(pDown, BorderLayout.SOUTH);
pBottom.add(pRight, BorderLayout.EAST);
contentPane.add(pBottom, BorderLayout.SOUTH);
f.setSize(new Dimension(320, 256));
f.setResizable(false);
f.setVisible(true);
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e) {
String s = e.getActionCommand();
if (s.equals("CE")) {
tResult.setText("0.");
} else if (s.equals("BackSpace")) {
if (!tResult.getText().trim().equals("0.")) {
if (str.length() != 1 && str.length() != 0) {
tResult.setText(str.delete(str.length() - 1, str.length())
.toString());
} else {
tResult.setText("0.");
str.setLength(0);
}
}
op2 = Double.parseDouble(tResult.getText().trim());
} else if (s.equals("1/x")) {
String temp = tResult.getText().trim();
double dtemp = Double.parseDouble(temp);
tResult.setText("" + 1 / dtemp);
} else if (s.equals("sqrt")) {
String temp = tResult.getText().trim();
double dtemp = Double.parseDouble(temp);
tResult.setText("" + Math.sqrt(dtemp));
} else if (s.equals("+")) {
str.setLength(0);
if (currentOp == 0) {
preOp = currentOp = 1;
op2 = 0;
tResult.setText("" + op1);
} else {
currentOp = preOp;
preOp = 1;
switch (currentOp) {
case 1:
op1 += op2;
tResult.setText("" + op1);
break;
case 2:
op1 -= op2;
tResult.setText("" + op1);
break;
case 3:
op1 *= op2;
tResult.setText("" + op1);
break;
case 4:
op1 /= op2;
tResult.setText("" + op1);
break;
}
}
} else if (s.equals("-")) {
str.setLength(0);
if (currentOp == 0) {
preOp = currentOp = 2;// op1=op2;op2=0;
tResult.setText("" + op1);
} else {
currentOp = preOp;
preOp = 2;
switch (currentOp) {
case 1:
op1 = op1 + op2;
tResult.setText("" + op1);
break;
case 2:
op1 = op1 - op2;
tResult.setText("" + op1);
break;
case 3:
op1 = op1 * op2;
tResult.setText("" + op1);
break;
case 4:
op1 = op1 / op2;
tResult.setText("" + op1);
break;
}
}
} else if (s.equals("*"))// *
{
str.setLength(0);
if (currentOp == 0) {
preOp = currentOp = 3;// op1=op2;op2=1;
tResult.setText("" + op1);// op1=op2;
} else {
currentOp = preOp;
preOp = 3;
switch (currentOp) {
case 1:
op1 = op1 + op2;
tResult.setText("" + op1);
break;
case 2:
op1 = op1 - op2;
tResult.setText("" + op1);
break;
case 3:
op1 = op1 * op2;
tResult.setText("" + op1);
break;
case 4:
op1 = op1 / op2;
tResult.setText("" + op1);
break;
}
}
} else if (s.equals("/"))
{
str.setLength(0);
if (currentOp == 0) {
preOp = currentOp = 4;// op2=1;
tResult.setText("" + op1);// op1=op2;
} else {
currentOp = preOp;
preOp = 4;
switch (currentOp) {
case 1:
op1 = op1 + op2;
tResult.setText("" + op1);
break;
case 2:
op1 = op1 - op2;
tResult.setText("" + op1);
break;
case 3:
op1 = op1 * op2;
tResult.setText("" + op1);
break;
case 4:
op1 = op1 / op2;
tResult.setText("" + op1);
break;
}
}
} else if (s.equals("="))// =
{
if (currentOp == 0) {
str.setLength(0);
tResult.setText("" + op2);
} else {
str.setLength(0);
currentOp = preOp;
switch (currentOp) {
case 1:
op1 = op1 + op2;
tResult.setText("" + op1);
break;
case 2:
op1 = op1 - op2;
tResult.setText("" + op1);
break;
case 3:
op1 = op1 * op2;
tResult.setText("" + op1);
break;
case 4:
op1 = op1 / op2;
tResult.setText("" + op1);
break;
}
currentOp = 0;
op2 = 0;
}
} else if (s.equals(".")) {
isDouble = true;
if (tResult.getText().trim().indexOf('.') != -1)
;
else {
if (tResult.getText().trim().equals("0")) {
str.setLength(0);
tResult.setText((str.append("0" + s)).toString());
}
else {
tResult.setText((str.append(s)).toString());
}
}
} else if (s.equals("0"))
{
if (tResult.getText().trim().equals("0.")) {
} else {
tResult.setText(str.append(s).toString());
op2 = Double.parseDouble(tResult.getText().trim());
}
} else {
tResult.setText(str.append(s).toString());
op2 = Double.parseDouble(tResult.getText().trim());
if (currentOp == 0)
op1 = op2;
}
} public static void main(String[] args) {
new SunnyCalculator();
}
}; bOther = new JButton("CE");
bOther, 3, 3, currentOp = 0;
double op1 = 0, op2 = 0;
bOther.setForeground(Color.red), opflag2 = -1;
JTextField tResult;
bOperator, 3, 3.*, 3, 0));
bOther.addActionListener(this);
bOther.setEditable(false);
pTop;8");
bNumber.setForeground(Color.blue);
bNumber.addActionListener(this);
bNumber.setMargin(new Insets(3.setLayout(new GridLayout(4, 5, flag = 0.setLayout(new GridLayout(1, 3; ");
bM;
bNumber = new JButton("
JButton bNumber;
JButton bOperator;
tResult = new JTextField("0.".setMargin(new Insets(3, 0;
JPanel pRight = new JPanel();
pRight计算器代码:package a;import java.awt.*;
import java.awt.event.*;
import javax, 3));
pDown.add(bNumber).swing;
JButton bOther.add(tResult);
contentPane.add(pTop;)
全部回答
  • 1楼网友:人類模型
  • 2021-01-22 06:08
我的回答你没看见吗????
  • 2楼网友:想偏头吻你
  • 2021-01-22 05:34
代码太长.放不上去;/ 电脑给出的数字;欢迎进入猜数字游戏; 0 || yourGuess > } catch (Exception e) { System.out.println("请输入一个1至100之间的整数"您猜大了再来,加油哦!!您只有10次机会!猜的数字在0到100之间" + &quot=======import java;程序运行时间: &quot.自己做点吧!"r\ 100) { System; /.parseInt(br;)!!; + "\.readLine()); i++) { ch.guess(); } */您一共猜了" + times + &quot.println(" + (endTime - startTime) / + (9-times) + "次机会!"); } times++;还有&quot.in); } public void guess() { int times = 0; /\r\n&quot.out;n" + "/ TODO Auto-generated method stub System;请输入一个1至100之间的整数; 1000 + &quot。&quot..;n" + &quot.println("r\。 int yourGuess = 0; continue; } else if (yourGuess > realNumber) { System.out;public class Charades { + "中奖数为:" + realNumber + "\* * for (int i = 0;; if(exit.hasNext("y&quot.println(" + (9-times) + "次机会!"); } else if (yourGuess < realNumber) { System;r\您猜小了再来,干吧爹!;请输入你猜的数字:"); Charades ch = new Charades()..;/ 限制猜10次,10次没中就退出; 你猜的数字。 Random rand = new Random(); + (endTime - startTime) /)){ System.exit(0); } } */ if (yourGuess <// 统计您输入的次数; realNumber = rand;n" + " 从字符输入流中读取文本,缓冲各个字符; + "\ ch.guess(); / 1000 + "程序运行时间: &quot.println(&quot:&quot.. 计算器的麻烦;//.nextInt(100) + 1; long startTime = System.currentTimeMillis(); //次;还有&quot。 int realNumber = 0;/ 获取开始时间 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));嘻嘻,您猜中了。 if (times == 10) { break; } } long endTime = System!!" + &quot!!" + "//.out;s"); } }}界面,从而实现字符、数组和行的高效读取。 while (yourGuess != realNumber) { try { yourGuess = Integer.currentTimeMillis();).println(&quot. ; continue; } /* * if(times >= 10 || times <= 0){ Scanner exit=new * Scanner(System;/ 获取结束时间 if (yourGuess == realNumber) { System; public static void main(String[] args) { /.*.util.*; import java.io;s"); } else { System.out.println("您猜了10次都不中您是猪啊!" + &quot.out.out
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯