Hallo, ich bin noch ganz neu in dem Thema Java. Ich hab die Aufgabe einen Taschenrechner zu programmieren jedoch zeigt der mir zwei Fehler an. Sitze schon bisschen dran aber finde nicht woran das liegen kann. Vielleicht kann einer mir helfen würde mich sehr freuen.
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Taschenrechner extends JFrame implements ActionListener {
public static void main(String[] args) {
new Taschenrechner ();
}
private JTextField txtVar1 = new JTextField ();
private JTextField txtVar2 = new JTextField ();
private JTextField txtErg = new JTextField ();
private JLabel lblVar1 = new JLabel ("Variable 1");
private JLabel lblVar2 = new JLabel ("Variable 2");
private JLabel lblErg = new JLabel ("Ergebnis");
private JButton btplus = new JButton ("+");
private JButton btminus = new JButton ("-");
private JButton btmal = new JButton ("*");
private JButton btgeteilt = new JButton ("/");
private JButton btdel = new JButton ("Löschen");
}
public Taschenrechner() {
Container cp = getContentPane ()
cp.setLayout(null)
lblVar1.setBounds(120,10,60,20)
txtvar1.setBounds(190,10,120,120)
lblVar2.setBounds(120,40,190,20)
txtvar2.setBounds(190,40,120,20)
lblErg.setBounds(120,70,190,20)
txtErg.setBounds(190,70,120,20)
txtErg.setEditable(false)
btplus.setBounds(120,120,50,20)
btminus.setBounds(170,120,50,20)
btmal.setBounds(220,120,50,20)
btgeteilt.setBounds(270,120,50,20)
btdel.setBounds(120,150,100,20)
cp.add(lblVar1)
cp.add(lblVar2)
cp.add(txtVar1)
cp.add(txtVar2)
cp.add(lblErg)
cp.add(txtErg)
cp.add(btplus)
cp.add(btminus)
cp.add(btmal)
cp.add(btgeteilt)
cp.add(btdel)
setTitle("Taschenrechner")
setSize(550,350)
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
setVisible(true)
btplus.addActionListener(this)
btminus.addactionListener(this)
btmal.addActionListener(this)
btgeteilt.addActionListener(this)
btdel.addActionListener(this)
}
public void actionPerformed (ActionEvent event) {
double Var1,Var2
Object obj = event.getSource()
if(obj ==btplus) {
Var1 = Double.parseDouble(txtvar1.getText())
Var2 = Double.parseDouble(txtvar2.getText())
Var1 = Var1+Var2
txtErg.setText(""+var1)
// end of if
}
if(obj ==btminus) {
Var1 = Double.parseDouble(txtvar1.getText())
Var2 = Double.parseDouble(txtvar2.getText())
Var1 = Var1-Var2
txtErg.setText(""+var1)
// end of if
}
if(obj ==btmal) {
var1 = Double.parseDouble(txtVar1.getText())
var2 = Double.parseDouble(txtVar2.getText())
Var1 = Var1*Var2
txtErg.setText(""+Var1)
// end of if
}
if(obj ==btgeteilt) {
Var1 = Double.parseDouble(txtVar1.getText())
Var2 = Double.parseDouble(txtVar2.getText())
Var1 = Var1/Var2
txtErg.setText(""+Var1)
// end of if
}
if(obj ==btdel) {
txtVar1.setText("")
txtVar2.setText("")
txtErg.setText("")
// end of if
}
}
}
Fehlermeldung
Taschenrechner.java:30:17: error: class, interface, or enum expected
public Taschenrechner() {
^
Taschenrechner.java:77:17: error: class, interface, or enum expected
public void actionPerformed (ActionEvent event) {
^
2 errors
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Taschenrechner extends JFrame implements ActionListener {
public static void main(String[] args) {
new Taschenrechner ();
}
private JTextField txtVar1 = new JTextField ();
private JTextField txtVar2 = new JTextField ();
private JTextField txtErg = new JTextField ();
private JLabel lblVar1 = new JLabel ("Variable 1");
private JLabel lblVar2 = new JLabel ("Variable 2");
private JLabel lblErg = new JLabel ("Ergebnis");
private JButton btplus = new JButton ("+");
private JButton btminus = new JButton ("-");
private JButton btmal = new JButton ("*");
private JButton btgeteilt = new JButton ("/");
private JButton btdel = new JButton ("Löschen");
}
public Taschenrechner() {
Container cp = getContentPane ()
cp.setLayout(null)
lblVar1.setBounds(120,10,60,20)
txtvar1.setBounds(190,10,120,120)
lblVar2.setBounds(120,40,190,20)
txtvar2.setBounds(190,40,120,20)
lblErg.setBounds(120,70,190,20)
txtErg.setBounds(190,70,120,20)
txtErg.setEditable(false)
btplus.setBounds(120,120,50,20)
btminus.setBounds(170,120,50,20)
btmal.setBounds(220,120,50,20)
btgeteilt.setBounds(270,120,50,20)
btdel.setBounds(120,150,100,20)
cp.add(lblVar1)
cp.add(lblVar2)
cp.add(txtVar1)
cp.add(txtVar2)
cp.add(lblErg)
cp.add(txtErg)
cp.add(btplus)
cp.add(btminus)
cp.add(btmal)
cp.add(btgeteilt)
cp.add(btdel)
setTitle("Taschenrechner")
setSize(550,350)
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
setVisible(true)
btplus.addActionListener(this)
btminus.addactionListener(this)
btmal.addActionListener(this)
btgeteilt.addActionListener(this)
btdel.addActionListener(this)
}
public void actionPerformed (ActionEvent event) {
double Var1,Var2
Object obj = event.getSource()
if(obj ==btplus) {
Var1 = Double.parseDouble(txtvar1.getText())
Var2 = Double.parseDouble(txtvar2.getText())
Var1 = Var1+Var2
txtErg.setText(""+var1)
// end of if
}
if(obj ==btminus) {
Var1 = Double.parseDouble(txtvar1.getText())
Var2 = Double.parseDouble(txtvar2.getText())
Var1 = Var1-Var2
txtErg.setText(""+var1)
// end of if
}
if(obj ==btmal) {
var1 = Double.parseDouble(txtVar1.getText())
var2 = Double.parseDouble(txtVar2.getText())
Var1 = Var1*Var2
txtErg.setText(""+Var1)
// end of if
}
if(obj ==btgeteilt) {
Var1 = Double.parseDouble(txtVar1.getText())
Var2 = Double.parseDouble(txtVar2.getText())
Var1 = Var1/Var2
txtErg.setText(""+Var1)
// end of if
}
if(obj ==btdel) {
txtVar1.setText("")
txtVar2.setText("")
txtErg.setText("")
// end of if
}
}
}
Fehlermeldung
Taschenrechner.java:30:17: error: class, interface, or enum expected
public Taschenrechner() {
^
Taschenrechner.java:77:17: error: class, interface, or enum expected
public void actionPerformed (ActionEvent event) {
^
2 errors
Zuletzt bearbeitet: