gpt4 book ai didi

java - 如何使 "="符号在我的 java 计算器上工作

转载 作者:行者123 更新时间:2023-12-01 22:14:47 25 4
gpt4 key购买 nike

我一直在尝试创建一个(丑陋的)计算器,但我遇到了“=”符号的问题。我知道如何将数字(字符串形式)转换为数字(整数形式),但这里的问题主要是*/-+。当他们加入到这种情况中时,我完全不知道该怎么办。

这是计算器的 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);


}

}

最佳答案

  1. 获取输入
  2. 将其与操作数(*、/、-、+)分开
  3. 将字符串转换为 int(Integer.valueOf() 会抛出 NumberFormatException)
  4. 对操作数执行 if else,然后对整数进行乘法、除法、加法或减法

关于java - 如何使 "="符号在我的 java 计算器上工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31295326/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com