Hoi allen,
Ik ben bezig een simpel rekenmachientje te maken in JAVA. Nu schijn ik problemen te hebben met wat wanneer wordt uitgevoerd. Oftewel, wanneer ik een 'if' (conditie?) inbouw kan ik dan meerdere gevolgen opnemen en vervolgens verder gaan naar de volgende 'if', of dien ik de conditie voor ieder gevolg afzonderlijk aan te geven?
Bijgevoegd mijn brouwsel tot op heden. Indien mogelijk een reactie op wat ik hier fout doe...... Niet allemaal tegelijk
Dank!!!!!
/*
* Calculator.java
*
* Created on 14 september 2004, 20:21
*/
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
/**
*
* @author DrDwarf
*/
public class Calculator extends Applet implements ActionListener {
private Button x1, x2, x3, x4, x5, x6, x7, x8, x9, x0, xClear, xPlus, xMinus, xIs;
private int memory = 0;
private int value = 0;
private int sign = 1;
public void init() {
x1 = new Button("1");
add(x1);
x1.addActionListener(this);
x2 = new Button("2");
add(x2);
x2.addActionListener(this);
x3 = new Button("3");
add(x3);
x3.addActionListener(this);
x4 = new Button("4");
add(x4);
x4.addActionListener(this);
x5 = new Button("5");
add(x5);
x5.addActionListener(this);
x6 = new Button("6");
add(x6);
x1.addActionListener(this);
x7 = new Button("7");
add(x7);
x7.addActionListener(this);
x8 = new Button("8");
add(x8);
x8.addActionListener(this);
x9 = new Button("9");
add(x9);
x9.addActionListener(this);
x0 = new Button("0");
add(x0);
x0.addActionListener(this);
xClear = new Button("Clear");
add(xClear);
xClear.addActionListener(this);
xPlus = new Button("+");
add(xPlus);
xPlus.addActionListener(this);
xMinus = new Button("-");
add(xMinus);
xMinus.addActionListener(this);
xIs = new Button("=");
add(xIs);
xIs.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == x1)
value = value * 10 + 1;
if (e.getSource() == x2)
value = value * 10 + 2;
if (e.getSource() == x3)
value = value * 10 + 3;
if (e.getSource() == x4)
value = value * 10 + 4;
if (e.getSource() == x5)
value = value * 10 + 5;
if (e.getSource() == x6)
value = value * 10 + 6;
if (e.getSource() == x7)
value = value * 10 + 7;
if (e.getSource() == x8)
value = value * 10 + 8;
if (e.getSource() == x9)
value = value * 10 + 9;
if (e.getSource() == x0)
value = value * 10;
if (e.getSource() == xPlus)
sign = 1;
memory = value;
value = 0;
if (e.getSource() == xMinus)
sign = -1;
memory = value;
value = 0;
if (e.getSource() == xIs)
value = value * sign;
value = value + memory;
if (e.getSource() == xClear)
memory = 0;
value = 0;
repaint();
}
public void paint (Graphics g) {
g.drawString (""+value, 40, 150);
}
}
Ik ben bezig een simpel rekenmachientje te maken in JAVA. Nu schijn ik problemen te hebben met wat wanneer wordt uitgevoerd. Oftewel, wanneer ik een 'if' (conditie?) inbouw kan ik dan meerdere gevolgen opnemen en vervolgens verder gaan naar de volgende 'if', of dien ik de conditie voor ieder gevolg afzonderlijk aan te geven?
Bijgevoegd mijn brouwsel tot op heden. Indien mogelijk een reactie op wat ik hier fout doe...... Niet allemaal tegelijk
Dank!!!!!
/*
* Calculator.java
*
* Created on 14 september 2004, 20:21
*/
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
/**
*
* @author DrDwarf
*/
public class Calculator extends Applet implements ActionListener {
private Button x1, x2, x3, x4, x5, x6, x7, x8, x9, x0, xClear, xPlus, xMinus, xIs;
private int memory = 0;
private int value = 0;
private int sign = 1;
public void init() {
x1 = new Button("1");
add(x1);
x1.addActionListener(this);
x2 = new Button("2");
add(x2);
x2.addActionListener(this);
x3 = new Button("3");
add(x3);
x3.addActionListener(this);
x4 = new Button("4");
add(x4);
x4.addActionListener(this);
x5 = new Button("5");
add(x5);
x5.addActionListener(this);
x6 = new Button("6");
add(x6);
x1.addActionListener(this);
x7 = new Button("7");
add(x7);
x7.addActionListener(this);
x8 = new Button("8");
add(x8);
x8.addActionListener(this);
x9 = new Button("9");
add(x9);
x9.addActionListener(this);
x0 = new Button("0");
add(x0);
x0.addActionListener(this);
xClear = new Button("Clear");
add(xClear);
xClear.addActionListener(this);
xPlus = new Button("+");
add(xPlus);
xPlus.addActionListener(this);
xMinus = new Button("-");
add(xMinus);
xMinus.addActionListener(this);
xIs = new Button("=");
add(xIs);
xIs.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == x1)
value = value * 10 + 1;
if (e.getSource() == x2)
value = value * 10 + 2;
if (e.getSource() == x3)
value = value * 10 + 3;
if (e.getSource() == x4)
value = value * 10 + 4;
if (e.getSource() == x5)
value = value * 10 + 5;
if (e.getSource() == x6)
value = value * 10 + 6;
if (e.getSource() == x7)
value = value * 10 + 7;
if (e.getSource() == x8)
value = value * 10 + 8;
if (e.getSource() == x9)
value = value * 10 + 9;
if (e.getSource() == x0)
value = value * 10;
if (e.getSource() == xPlus)
sign = 1;
memory = value;
value = 0;
if (e.getSource() == xMinus)
sign = -1;
memory = value;
value = 0;
if (e.getSource() == xIs)
value = value * sign;
value = value + memory;
if (e.getSource() == xClear)
memory = 0;
value = 0;
repaint();
}
public void paint (Graphics g) {
g.drawString (""+value, 40, 150);
}
}