Hallo,
ik moet voor school een JAVA programma schrijven, maar ik heb een klein probleempje.
Mijn project is een rekenmachine, en hij werkt al helemaal, maar ik zou graag de foutcodes Infinity (oneindig) (door een getal te delen door 0) en NaN (Not a Number) (0/0) eruit willen halen, en vervangen door een zelf in te stellen tekst
Wie kan me helpen?
Dit is de code die ik tot nu toe heb:
---------------
import java.awt.*;
import java.applet.*;
public class Calc extends Applet
{
TextField text;
String Tekst1, Tekst2;
double Getal1, Getal2;
String Operator;
boolean isFixReg;
public Calc()
{
Panel pFrame = new Panel();
pFrame.setLayout(new FlowLayout());
text = new TextField("");
text.setForeground(Color.black);
text.setEditable(false);
Panel pCalc = new Panel();
pCalc.setLayout(new BorderLayout(0, 10));
pCalc.add("North", text);
pFrame.add("Center", pCalc);
Dimension dSize= pCalc.size();
dSize.width = dSize.width + 30;
dSize.height = dSize.height + 30;
pFrame.resize(dSize);
Panel pKey = new Panel();
pKey.setLayout(new GridLayout(6, 3, 5, 5));
add("center", pKey);
pKey.add(new Button("7")); pKey.add(new Button("8")); pKey.add(new Button("9"));
pKey.add(new Button("4")); pKey.add(new Button("5")); pKey.add(new Button("6"));
pKey.add(new Button("1")); pKey.add(new Button("2")); pKey.add(new Button("3"));
pKey.add(new Button("+")); pKey.add(new Button("0")); pKey.add(new Button("-"));
pKey.add(new Button(":")); pKey.add(new Button("=")); pKey.add(new Button("x"));
pKey.add(new Button("")); pKey.add(new Button("Clear")); pKey.add(new Button(""));
pCalc.add("South", pKey);
setLayout(new BorderLayout(0, 0));
add("North", pFrame);
setBackground(Color.green);
Getal1 = 0d;
Getal2 = 0d;
Operator = "";
text.setText("0");
isFixReg = true;
}
public boolean action(Event evt, Object arg)
{
if ("Clear".equals(arg))
{
Getal1 = 0d;
Getal2 = 0d;
Operator = "";
text.setText("0");
isFixReg = true;
}
else if
( ("0".equals(arg))
| ("1".equals(arg))
| ("2".equals(arg))
| ("3".equals(arg))
| ("4".equals(arg))
| ("5".equals(arg))
| ("6".equals(arg))
| ("7".equals(arg))
| ("8".equals(arg))
| ("9".equals(arg))
| (".".equals(arg))
)
{
if (isFixReg)
Tekst2 = (String) arg;
else
Tekst2 = text.getText() + arg;
text.setText(Tekst2);
isFixReg = false;
}
else if (("+".equals(arg))
| ("-".equals(arg))
| ("x".equals(arg))
| (":".equals(arg))
| ("=".equals(arg)))
{
Tekst1 = text.getText();
Getal2 = (Double.valueOf(Tekst1)).doubleValue();
Getal1 = Calculation(Operator, Getal1, Getal2);
Double dTemp = new Double(Getal1);
Tekst2 = dTemp.toString();
text.setText(Tekst2);
Operator = (String) arg;
isFixReg = true;
}
return true;
}
private double Calculation(String Operator, double Getal1, double Getal2)
{
if ("+".equals(Operator)) Getal1 = Getal1 + Getal2;
else if ("-".equals(Operator)) Getal1 = Getal1 - Getal2;
else if ("x".equals(Operator)) Getal1 = Getal1 * Getal2;
else if (":".equals(Operator)) Getal1 = Getal1 / Getal2;
else Getal1 = Getal2;
return Getal1;
}
}
ik moet voor school een JAVA programma schrijven, maar ik heb een klein probleempje.
Mijn project is een rekenmachine, en hij werkt al helemaal, maar ik zou graag de foutcodes Infinity (oneindig) (door een getal te delen door 0) en NaN (Not a Number) (0/0) eruit willen halen, en vervangen door een zelf in te stellen tekst
Wie kan me helpen?
Dit is de code die ik tot nu toe heb:
---------------
import java.awt.*;
import java.applet.*;
public class Calc extends Applet
{
TextField text;
String Tekst1, Tekst2;
double Getal1, Getal2;
String Operator;
boolean isFixReg;
public Calc()
{
Panel pFrame = new Panel();
pFrame.setLayout(new FlowLayout());
text = new TextField("");
text.setForeground(Color.black);
text.setEditable(false);
Panel pCalc = new Panel();
pCalc.setLayout(new BorderLayout(0, 10));
pCalc.add("North", text);
pFrame.add("Center", pCalc);
Dimension dSize= pCalc.size();
dSize.width = dSize.width + 30;
dSize.height = dSize.height + 30;
pFrame.resize(dSize);
Panel pKey = new Panel();
pKey.setLayout(new GridLayout(6, 3, 5, 5));
add("center", pKey);
pKey.add(new Button("7")); pKey.add(new Button("8")); pKey.add(new Button("9"));
pKey.add(new Button("4")); pKey.add(new Button("5")); pKey.add(new Button("6"));
pKey.add(new Button("1")); pKey.add(new Button("2")); pKey.add(new Button("3"));
pKey.add(new Button("+")); pKey.add(new Button("0")); pKey.add(new Button("-"));
pKey.add(new Button(":")); pKey.add(new Button("=")); pKey.add(new Button("x"));
pKey.add(new Button("")); pKey.add(new Button("Clear")); pKey.add(new Button(""));
pCalc.add("South", pKey);
setLayout(new BorderLayout(0, 0));
add("North", pFrame);
setBackground(Color.green);
Getal1 = 0d;
Getal2 = 0d;
Operator = "";
text.setText("0");
isFixReg = true;
}
public boolean action(Event evt, Object arg)
{
if ("Clear".equals(arg))
{
Getal1 = 0d;
Getal2 = 0d;
Operator = "";
text.setText("0");
isFixReg = true;
}
else if
( ("0".equals(arg))
| ("1".equals(arg))
| ("2".equals(arg))
| ("3".equals(arg))
| ("4".equals(arg))
| ("5".equals(arg))
| ("6".equals(arg))
| ("7".equals(arg))
| ("8".equals(arg))
| ("9".equals(arg))
| (".".equals(arg))
)
{
if (isFixReg)
Tekst2 = (String) arg;
else
Tekst2 = text.getText() + arg;
text.setText(Tekst2);
isFixReg = false;
}
else if (("+".equals(arg))
| ("-".equals(arg))
| ("x".equals(arg))
| (":".equals(arg))
| ("=".equals(arg)))
{
Tekst1 = text.getText();
Getal2 = (Double.valueOf(Tekst1)).doubleValue();
Getal1 = Calculation(Operator, Getal1, Getal2);
Double dTemp = new Double(Getal1);
Tekst2 = dTemp.toString();
text.setText(Tekst2);
Operator = (String) arg;
isFixReg = true;
}
return true;
}
private double Calculation(String Operator, double Getal1, double Getal2)
{
if ("+".equals(Operator)) Getal1 = Getal1 + Getal2;
else if ("-".equals(Operator)) Getal1 = Getal1 - Getal2;
else if ("x".equals(Operator)) Getal1 = Getal1 * Getal2;
else if (":".equals(Operator)) Getal1 = Getal1 / Getal2;
else Getal1 = Getal2;
return Getal1;
}
}