我一直试图创建一个(丑陋)计算器,我遇到“=”符号问题。我知道如何将数字(字符串)转换为数字(在int中),但这里的问题主要是* / - +。当他们被添加到这种情况时,我完全不知道该怎么做。
这是计算器的GIF:http://gyazo.com/98781eaaca0b3152967e6370cad3df15
package megetenkelkalkis;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.*;
import java.text.DecimalFormat;
import javax.swing.*;
class Kalkulator extends JFrame{
private String textfield = "";
private String replace ="";
private JButton btn0 = new JButton("0");
private JButton btn1 = new JButton("1");
private JButton btn2 = new JButton("2");
private JButton btn3 = new JButton("3");
private JButton btn4 = new JButton("4");
private JButton btn5 = new JButton("5");
private JButton btn6 = new JButton("6");
private JButton btn7 = new JButton("7");
private JButton btn8 = new JButton("8");
private JButton btn9 = new JButton("9");
private JButton btnlik = new JButton("=");
private JButton btngange = new JButton("*");
private JButton btndele = new JButton("/");
private JButton btnpluss = new JButton("+");
private JButton btnminus = new JButton("-");
private JTextField tekst = new JTextField();
private JButton btndel= new JButton("DEL");
JPanel p = new JPanel();
JPanel p2 = new JPanel();
JPanel p3 = new JPanel();
public Kalkulator(String tittel){
tekst.setPreferredSize(new Dimension(310,25));
setTitle(tittel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
p.add(btn0);
p.add(btn1);
p.add(btn2);
p.add(btn3);
p.add(btn4);
p.add(btn5);
p.add(btnlik);
p2.add(btn6);
p2.add(btn7);
p2.add(btn8);
p2.add(btn9);
p2.add(btndele);
p2.add(btngange);
p2.add(btnpluss);
p2.add(btnminus);
p3.add(tekst);
p3.add(btndel);
add(p, BorderLayout.SOUTH);
add(p2, BorderLayout.CENTER);
add(p3, BorderLayout.NORTH);
pack();
Knappelytter knappelytteren = new Knappelytter();
btn0.addActionListener(knappelytteren);
btn1.addActionListener(knappelytteren);
btn2.addActionListener(knappelytteren);
btn3.addActionListener(knappelytteren);
btn4.addActionListener(knappelytteren);
btn5.addActionListener(knappelytteren);
btn6.addActionListener(knappelytteren);
btn7.addActionListener(knappelytteren);
btn8.addActionListener(knappelytteren);
btn9.addActionListener(knappelytteren);
btnlik.addActionListener(knappelytteren);
btndele.addActionListener(knappelytteren);
btngange.addActionListener(knappelytteren);
btnminus.addActionListener(knappelytteren);
btnpluss.addActionListener(knappelytteren);
btndel.addActionListener(knappelytteren);
}
class Knappelytter implements ActionListener{
public void actionPerformed (ActionEvent hendelse){
JButton valgtKnapp = (JButton) hendelse.getSource();
String knapp = valgtKnapp.getText();
if (knapp.equals("0")){
textfield += "0";
tekst.setText(textfield);
}else if(knapp.equals("1")){
textfield += "1";
tekst.setText(textfield);
}else if(knapp.equals("2")){
textfield += "2";
tekst.setText(textfield);
}else if(knapp.equals("3")){
textfield += "3";
tekst.setText(textfield);
}else if(knapp.equals("4")){
textfield += "4";
tekst.setText(textfield);
}else if(knapp.equals("5")){
textfield += "5";
tekst.setText(textfield);
}else if(knapp.equals("6")){
textfield += "6";
tekst.setText(textfield);
}else if(knapp.equals("7")){
textfield += "7";
tekst.setText(textfield);
}else if(knapp.equals("8")){
textfield += "8";
tekst.setText(textfield);
}else if(knapp.equals("9")){
textfield += "9";
tekst.setText(textfield);
}else if(knapp.equals("*")){
textfield += "*";
tekst.setText(textfield);
}else if(knapp.equals("/")){
textfield += "/";
tekst.setText(textfield);
}else if(knapp.equals("-")){
textfield += "-";
tekst.setText(textfield);
}else if(knapp.equals("+")){
textfield += "+";
tekst.setText(textfield);
}else if(knapp.equals("DEL")){
tekst.setText(" ");
textfield = " ";
}else if(knapp.equals("=")){
else if(knapp.equals("=")){
/EDIT RIGHT HERE
String[] parts = textfield.split("-*/+");
for (int i = 0; i < textfield.length(); i++){
if (textfield.charAt(i) == ('-')){
String one = parts[0];
String two = parts[1];
int one1 = Integer.parseInt(one);
int one2 = Integer.parseInt(two);
int one3 = one1-one2;
String one4 = String.valueOf(one3);
tekst.setText(one4);
}
}
}
}
public class MegetEnkelKalkis {
public static void main(String[] args) {
Kalkulator Skole = new Kalkulator("Kalkulator");
Skole.setVisible(true);
}
}
答案 0 :(得分:2)
答案 1 :(得分:1)
你需要的是一个字符串计算器。您基本上按照常规数学规则解析文本字段的内容。这有点工作但不太难。
基本上就是这样:
答案 2 :(得分:0)
或者你通过nashorn(javascript)引擎计算;)
private ScriptEngine scriptEngine = new ScriptEngineManager().getEngineByName("nashorn");
double result = (double) scriptEngine.eval(formula)
答案 3 :(得分:0)
您应该构建一个reverse polish notation,这是解析和评估方法公式的最简单方法。有很多例子可以在网上用Java做到这一点。
答案 4 :(得分:0)
有多种方法可以做到这一点,但建议您创建两个双打。当用户单击+, - ,\或*将之前的数字解析为double。然后当他们按=将+和=之间的所有内容解析为double时,然后使用这两个双精度执行计算并输出结果。您可以设置标志以确定要执行的操作。例如,+按钮可以设置一个名为Plus = true的布尔值;然后当你执行操作检查哪个bools是真的。如果多于或少于一个,告诉用户输入无效,否则执行操作
答案 5 :(得分:0)
代码中肯定有很多改进的范围,正如其他人所建议的那样,但是如果你想保持它的原样和工作,那么这里是代码:
.
.
.
}else if(knapp.equals("=")){
String nytekst = textfield;
ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine engine = mgr.getEngineByName("JavaScript");
try {
Double result = (Double) engine.eval(nytekst);
System.out.println(result);
tekst.setText(result.toString());
} catch (ScriptException e) {
e.printStackTrace();
}
}
.
.
.