Java:
import java.applet.Applet;
import java.awt.Button;
import java.awt.Label;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Mittelwert extends Applet implements ActionListener {
Label label;
TextField text1, text2, text3;
Button button1, button2;
String ziffer1, ziffer2, result;
Float float1, float2, sum;
public void init() {
// Setzen der Labels
label = new Label("Mittelwerte");
label.setBounds(100, 160, 30, 20);
add(label);
// Setzen der Textareas
text1 = new TextField("", 20);
text1.setBounds(110, 180, 30, 150);
text2 = new TextField("", 20);
text2.setBounds(300, 170, 30, 150);
add(text1);
add(text2);
button1 = new Button("Rechne Mittelwert");
button2 = new Button("Löschen");
add(button1);
add(button2);
text3 = new TextField("", 20);
add(text3);
}
public void actionPerformed(ActionEvent e) {
ziffer1 = text2.getText();
ziffer2 = text1.getText();
// Umwandeln der Strings in floats
float1 = new Float(ziffer1);
float1 = new Float(ziffer2);
// m = (x+y)/2;
if (e.getSource().equals(button1)) {
System.out.println(text2.getText());
sum = (float1 + float2) / 2;
result = sum.toString();
text3.setText(result);
text3.getText();
} else if (e.getSource().equals(button2)) {
text1.setText("");
text2.setText("");
text3.setText("");
}
}
}
Der Compiler bemängelt:
operator + cannot be applied to java.lang.Float,java.lang.Float
sum = (float1 + float2) / 2;
^
incompatible types
found : int
required: java.lang.Float
sum = (float1 + float2) / 2;
^