I am fairly new to Java and cannot figure out why the value of the booleans in my if statements cannot be passed out into System.out.println(aa + " " + bb + " " + gate); below. The goal would be to set the values of the booleans aa and bb in the if statements, then pass both of the variables out into another method with calculate(aa, bb);. The proper values are returned from each if statements, but not from System.out.println(aa + " " + bb + " " + gate);. How can I save the values of both booleans and pass them out onto something else?
JButton btnCalculate = new JButton("Calculate");
btnCalculate.addActionListener(new ActionListener() {
JFrame error = new JFrame();
public void actionPerformed(ActionEvent arg0) {
try {
int a = Integer.parseInt(textInputA.getText());
int b = Integer.parseInt(textInputB.getText());
String gate = String.valueOf(comboBoxGateSelect.getSelectedItem());
if(a == 1) {
boolean aa = true;
System.out.println("a is " + aa + "(1)");
}
if(a == 0) {
boolean aa = false;
System.out.println("a is " + aa + "(0)");
calculate();
}
if(b == 1) {
boolean bb = true;
System.out.println("b is " + bb + "(1)");
}
if(b == 0) {
boolean bb = false;
System.out.println("b is " + bb + "(0)");
}
if(a > 1 || a < 0) {
JOptionPane.showMessageDialog(error, "Input A must be either 1 or 0. \r\n True = 1, False = 0.", "Error", JOptionPane.ERROR_MESSAGE, null);
}
if(b > 1 || b < 0) {
JOptionPane.showMessageDialog(error, "Input B must be either 1 or 0. \r\n True = 1, False = 0.", "Error", JOptionPane.ERROR_MESSAGE, null);
}
System.out.println(a + " " + b + " " + gate);
System.out.println(aa + " " + bb + " " + gate); // This one +
calculate(aa, bb); // This one.
} catch(NumberFormatException e) {
JOptionPane.showMessageDialog(error, "Inputs A and B must be either 1 or 0. \r\n True = 1, False = 0.", "Error", JOptionPane.ERROR_MESSAGE, null);
}
}
});
btnCalculate.setBackground(Color.GRAY);
btnCalculate.setFont(new Font("Arial", Font.BOLD, 11));
btnCalculate.setForeground(Color.BLACK);
btnCalculate.setBounds(72, 204, 89, 23);
contentPane.add(btnCalculate);
Aucun commentaire:
Enregistrer un commentaire