首页 > 编程知识 正文

负数的乘方如何计算?,计算器怎么乘以负数

时间:2023-05-04 16:43:38 阅读:214577 作者:1334

共回答了9个问题采纳率:100%

import java.awt.BorderLayout;

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.math.BigDecimal;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.JTextField;

public class TestCalc implements ActionListener {

JTextField jtf = new JTextField("0.",200);

JButtonkndjzg jb = new JButton[20];

private int tag = 0;

private double a;

private double b;

private String operator;

public TestCalc() {

jf.add(jtf,BorderLayout.NORTH);

// jtf.requestFocus();

jtf.setEditable(false);

jtf.setCaretPosition(jtf.getText().length() - 1);

jf.add(jp);

jp.setLayout(new GridLayout(5,4,4,4));

Stringkndjzg str = { "Back","CE","C","+","7","8","9","-","4","5",

"6","*","1","2","3","/","0","+/-",".","=" };

int i = 0;

for (i = 0; i < str.length; i++) {

jb[i] = new JButton(str[i]);

jp.add(jb[i]);

jb[i].addActionListener(this);

}

jf.setSize(300,240);

// jf.setLocation(300,200);

jf.setLocationRelativeTo(null);

jf.setVisible(true);

// jf.pack();//自动调整大小;

jf.setResizable(false);// 不允许别人调大小;

jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

public static void main(Stringkndjzg args) {

TestCalc tc = new TestCalc();

}

public void actionPerformed(ActionEvent e) {

String command = e.getActionCommand();

if (command.matches("^[[0-9].]$")) {

if (tag == 0) {

sb1.append(command);

jtf.setText(sb1.substring(0));

} else {

sb2.append(command);

jtf.setText(sb2.substring(0));

}

} else if (command.matches("^[-/dddbbt]$")) {

tag = 1;

operator = command;

} else {

if (command.matches("=")) {

String str1 = sb1.substring(0);

String str2 = sb2.substring(0);

if (str1 == null) {

a = 0.0;

} else {

a = Double.parseDouble(str1);

}

if (str2 == null) {

b = 0.0;

} else {

b = Double.parseDouble(str2);

}

if (operator.equals("+")) {

jtf.setText("" + (a + b));

} else if (operator.equals("-")) {

BigDecimal bd1 = new BigDecimal(Double.toString(a)); // 必须使用String做参数才可以精确运算

BigDecimal bd2 = new BigDecimal(Double.toString(b));

Double yu1 = bd1.subtract(bd2).doubleValue();

jtf.setText("" + (yu1));

} else if (operator.equals("*")) {

jtf.setText("" + (a * b));

} else if (operator.equals("/")) {

BigDecimal bd1 = new BigDecimal(Double.toString(a)); // 必须使用String做参数才可以精确运算

BigDecimal bd2 = new BigDecimal(Double.toString(b));

Double yu1 = bd1.divide(bd2).doubleValue();

jtf.setText("" + yu1);

}

tag = 0;

sb1.delete(0,sb1.length());

sb2.delete(0,sb2.length());

} else if (command.matches("C")) {

tag = 0;

sb1.delete(0,sb1.length());

sb2.delete(0,sb2.length());

jtf.setText("0.");

jtf.setCaretPosition(jtf.getText().length() - 1);

} else if (command.matches("CE")) {

tag = 0;

sb2.delete(0,sb2.length());

jtf.setText(sb1.substring(0));

} else if (command.matches("Back")) {//Back功能键的实现;

if (tag == 0) {

sb1.deleteCharAt(sb1.length() - 1);

jtf.setText(sb1.substring(0));

} else {

sb2.deleteCharAt(sb2.length() - 1);

jtf.setText(sb2.substring(0));

}

} else if (command.matches("+/-")) {

if (tag == 0) {

if(sb1.substring(0,1).equals("-")){

sb1.replace(0,1,"+");

}else{

sb1.insert(0,"-");

}

jtf.setText(sb1.substring(0));

} else {

if(sb2.substring(0,1).equals("-")){

sb2.replace(0,1,"+");

}else{

sb2.insert(0,"-");

}

jtf.setText(sb2.substring(0));

}

}else {

}

}

}

}

1年前

5

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。