Hallo bin gerade am Taschenrechner programmieren und sogut wie fertig 
Hier mal mein Code:
Meine Frage ist nun wie ich es hinbekomme, dass Rechnungen wie 1+1-1 funktionieren. Ich möchte das auf dieselbe Weise wie bishere lösen also wenn ich 1+1- eingebe wird das 1+1 ausgerechnet und dahinter das eingegebene Rechenzeichen gesetzt also 2-
Ich hoffe das war soweit verständlich
Mfg, Zitrus
Hier mal mein Code:
Java:
package rechner;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Rechner
{
public static void main (String[] args)
{
CalculatorFrame frame = new CalculatorFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class CalculatorFrame extends JFrame
{
public CalculatorFrame()
{
setLayout(new BorderLayout());
setResizable(false);
setSize(250,215);
setTitle ("Saltuks Rechner");
setLocation(100,100);
CalculatorPanel hpanel = new CalculatorPanel();
add(hpanel);
}
}
class CalculatorPanel extends JPanel implements ActionListener
{
private JTextArea field;
private JPanel panel;
private JTextArea field2;
public CalculatorPanel() {
setLayout(null);
field = new JTextArea();
field.setBounds (10,10,225,40);
field.setEditable(false);
field.setVisible(true);
field2 = new JTextArea();
field2.setBounds (10,10,225,40);
field2.setEditable(false);
field2.setVisible(true);
panel = new JPanel();
panel.setLayout(null);
panel.setSize(300,300);
addButton0();
addButton1();
addButton2();
addButton3();
addbuttonplus();
addbutton4();
addbutton5();
addbutton6();
addbuttonminus();
addbutton7();
addbutton8();
addbutton9();
addbuttonmal();
addbuttondurch();
addbuttonpkt();
addbuttongleich();
addbuttonlösch();
addbuttonlösch1();
add(field, BorderLayout.NORTH);
add(field2, BorderLayout.NORTH);
add(panel, BorderLayout.CENTER);
}
JButton button0 = new JButton("0");
JButton button1 = new JButton("1");
JButton button2 = new JButton("2");
JButton button3 = new JButton("3");
JButton buttonplus = new JButton("+");
JButton button4 = new JButton("4");
JButton button5 = new JButton("5");
JButton button6 = new JButton("6");
JButton buttonminus = new JButton("*");
JButton button7 = new JButton("7");
JButton button8 = new JButton("8");
JButton button9 = new JButton("9");
JButton buttonmal = new JButton("x");
JButton buttondurch = new JButton("/");
JButton buttonpkt = new JButton(".");
JButton buttongleich = new JButton("=");
JButton buttonlösch = new JButton("C");
JButton buttonlösch1 = new JButton("E");
private void addButton0()
{
button0.addActionListener(this);
button0.setBounds(10,150,45,30);
panel.add(button0);
setFont(new Font("Agency FB Regular", 5, 3));
}
private void addButton1()
{
button1.addActionListener(this);
button1.setBounds(10,60,45,30);
panel.add(button1);
setFont(new Font("Agency FB Regular", 5, 3));
}
private void addButton2()
{
button2.setBounds(55,60,45,30);
button2.addActionListener(this);
panel.add(button2);
setFont(new Font("Arial", 5, 3));
}
private void addButton3()
{
button3.setBounds(100,60,45,30);
button3.addActionListener(this);
panel.add(button3);
setFont(new Font("Agency FB Regular", 5, 3));
}
private void addbuttonplus()
{
buttonplus.setBounds(145,60,45,30);
buttonplus.addActionListener(this);
panel.add(buttonplus);
setFont(new Font("Agency FB Regular", 5, 3));
}
private void addbutton4()
{
button4.setBounds(10,90,45,30);
button4.addActionListener(this);
panel.add(button4);
}
private void addbutton5()
{
button5.setBounds(55,90,45,30);
button5.addActionListener(this);
panel.add(button5);
}
private void addbutton6()
{
button6.setBounds(100,90,45,30);
button6.addActionListener(this);
panel.add(button6);
}
private void addbuttonminus()
{
buttonminus.setBounds(190,60,45,30);
buttonminus.addActionListener(this);
panel.add(buttonminus);
}
private void addbutton7()
{
button7.setBounds(10,120,45,30);
button7.addActionListener(this);
panel.add(button7);
}
private void addbutton8()
{
button8.setBounds(55,120,45,30);
button8.addActionListener(this);
panel.add(button8);
}
private void addbutton9()
{
button9.setBounds(100,120,45,30);
button9.addActionListener(this);
panel.add(button9);
}
private void addbuttonmal()
{
buttonmal.setBounds(145,90,45,30);
buttonmal.addActionListener(this);
panel.add(buttonmal);
}
private void addbuttondurch()
{
buttondurch.setBounds(190,90,45,30);
buttondurch.addActionListener(this);
panel.add(buttondurch);
}
private void addbuttonpkt()
{
buttonpkt.setBounds(145,120,45,30);
buttonpkt.addActionListener(this);
panel.add(buttonpkt);
}
private void addbuttongleich()
{
buttongleich.setBounds(55,150,135,30);
buttongleich.addActionListener(this);
panel.add(buttongleich);
}
private void addbuttonlösch()
{
buttonlösch.setBounds(190,120,45,30);
buttonlösch.addActionListener(this);
panel.add(buttonlösch);
}
private void addbuttonlösch1()
{
buttonlösch1.setBounds(190,150,45,30);
buttonlösch1.addActionListener(this);
panel.add(buttonlösch1);
}
public void actionPerformed (ActionEvent ae){
String i = field.getText();
int counterP = 1;
int counterD = 1;
int counterM = 1;
int counterMi = 1;
for (int a = 0; a < i.length(); a++) {
if(i.charAt(a) == '*') counterMi++;
}
for (int a = 0; a < i.length(); a++) {
if(i.charAt(a) == '+') counterP++;
}
for (int a = 0; a < i.length(); a++) {
if(i.charAt(a) == 'x') counterM++;
}
for (int a = 0; a < i.length(); a++) {
if(i.charAt(a) == '/') counterD++;
}
if(ae.getSource() == this.button0){
field.setText(field.getText() + "0");
}
if(ae.getSource() == this.button1){
field.setText(field.getText() + "1");
}
if(ae.getSource() == this.button2) {
field.setText(field.getText() + "2");
}
if(ae.getSource() == this.button3) {
field.setText(field.getText() + "3");
}
if(ae.getSource() == this.buttonplus) {
field.setText(field.getText() + "+");
if(counterP == 2) {
String[] parts = i.split("\\+");
String part1 = parts[0];
String part2 = parts[1];
double d1 = Double.parseDouble(part1);
double d2 = Double.parseDouble(part2);
double res = d1+d2;
field.setText(String.valueOf(res) + "+");
}
}
if(ae.getSource() == this.button4) {
field.setText(field.getText() + "4");
}
if(ae.getSource() == this.button5) {
field.setText(field.getText() + "5");
}
if(ae.getSource() == this.button6) {
field.setText(field.getText() + "6");
}
if(ae.getSource() == this.buttonminus) {
field.setText(field.getText() + "*");
if(counterMi == 2) {
String[] parts = i.split("\\*");
String part1 = parts[0];
String part2 = parts[1];
double d1 = Double.parseDouble(part1);
double d2 = Double.parseDouble(part2);
double res = d1-d2;
field.setText(String.valueOf(res) + "*");
}
}
if(ae.getSource() == this.button7) {
field.setText(field.getText() + "7");
}
if(ae.getSource() == this.button8) {
field.setText(field.getText() + "8");
}
if(ae.getSource() == this.button9) {
field.setText(field.getText() + "9");
}
if(ae.getSource() == this.buttonmal) {
field.setText(field.getText() + "x");
if(counterM == 2) {
String[] parts = i.split("x");
String part1 = parts[0];
String part2 = parts[1];
double d1 = Double.parseDouble(part1);
double d2 = Double.parseDouble(part2);
double res = d1*d2;
field.setText(String.valueOf(res) + "x");
}
}
if(ae.getSource() == this.buttondurch) {
field.setText(field.getText() + "/");
if(counterD == 2) {
String[] parts = i.split("\\/");
String part1 = parts[0];
String part2 = parts[1];
double d1 = Double.parseDouble(part1);
double d2 = Double.parseDouble(part2);
double res = d1/d2;
field.setText(String.valueOf(res) + "");
}
}
if(ae.getSource() == this.buttonpkt) {
field.setText(field.getText() + ".");
}
if(ae.getSource() == this.buttonlösch) {
field.setText("");
}
if (ae.getSource() == this.buttonlösch1) {
field.setText(i.substring(0, i.length()-1));
}
if(ae.getSource() == this.buttongleich) {
if(i.contains("+")) {
String[] parts = i.split("\\+");
String part1 = parts[0];
String part2 = parts[1];
double d1 = Double.parseDouble(part1);
double d2 = Double.parseDouble(part2);
double res = d1+d2;
String result = String.valueOf(res);
field.setText(result);
}
if(i.contains("*")) {
String[] parts = i.split("\\*");
String part1 = parts[0];
String part2 = parts[1];
double d1 = Double.parseDouble(part1);
double d2 = Double.parseDouble(part2);
double res = d1-d2;
String result = String.valueOf(res);
field.setText(result);
}
if(i.contains("x")) {
String[] parts = i.split("x");
String part1 = parts[0];
String part2 = parts[1];
double d1 = Double.parseDouble(part1);
double d2 = Double.parseDouble(part2);
double res = d1*d2;
String result = String.valueOf(res);
field.setText(result);
}
if(i.contains("/")) {
String[] parts = i.split("\\/");
String part1 = parts[0];
String part2 = parts[1];
double d1 = Double.parseDouble(part1);
double d2 = Double.parseDouble(part2);
double res = d1/d2;
String result = String.valueOf(res);
field.setText(result);
}
}
}
}
Meine Frage ist nun wie ich es hinbekomme, dass Rechnungen wie 1+1-1 funktionieren. Ich möchte das auf dieselbe Weise wie bishere lösen also wenn ich 1+1- eingebe wird das 1+1 ausgerechnet und dahinter das eingegebene Rechenzeichen gesetzt also 2-
Ich hoffe das war soweit verständlich
Mfg, Zitrus