Code inside If statement hidden from rest of code
in
Programming Questions
•
2 years ago
Hi all
Can anybody please help with this code?
Why can't the code in line 96 "println(y1);" not see the code in line 95 and how can I solve that?
Thanks
Bertus
Can anybody please help with this code?
Why can't the code in line 96 "println(y1);" not see the code in line 95 and how can I solve that?
Thanks
Bertus
import javax.swing.JOptionPane;//For user input dialogs
boolean isInteger;
String y = "0";
class Button {
int x, y; // The x- and y-coordinates
int size; // Dimension (width and height)
color baseGray; // Default gray value
color overGray; // Value when mouse is over the button
color pressGray; // Value when mouse is over and pressed
boolean over = false; // True when the mouse is over
boolean pressed = false; // True when the mouse is over and pressed
Button(int xp, int yp, int s, color b, color o, color p) {
x = xp;
y = yp;
size = s;
baseGray = b;
overGray = o;
pressGray = p;
}
// Updates the over field every frame
void update() {
if ((mouseX >= x) && (mouseX <= x+size) &&
(mouseY >= y) && (mouseY <= y+size)) {
over = true;
} else {
over = false;
}
}
boolean press() {
if (over == true) {
pressed = true;
return true;
} else {
return false;
}
}
void release() {
pressed = false; // Set to false when the mouse is released
}
void display() {
if (pressed == true) {
fill(pressGray);
} else if (over == true) {
fill(overGray);
} else {
fill(baseGray);
}
stroke(255);
rect(x, y, size, size);
}}
Button button1, button2, button3;
int mode = 0;
void setup() {
frameRate (2);
size(100, 100);
smooth();
color gray = color(204);
color white = color(255);
color black = color(0);
button1 = new Button(10, 80, 10, gray, white, black);
}
void draw(){
frameRate (2);
background(204);
manageButtons();
noStroke();
fill(0);
isInteger = true;
if (mode == 1) {
y = JOptionPane.showInputDialog(null,
"Enter new setpoint (0 to 120):",
"Setpoint adjustment",
JOptionPane.PLAIN_MESSAGE);}
if (y == null) {
isInteger = false;
} int length = y.length();
if (length == 0) {
isInteger = false;
}
int i = 0;
if (length == 1) {
if (y.charAt(0) == '-') {
isInteger = false;
}
i = 0;
}
for (; i < length; i++) {
char c = y.charAt(i);
if (c <= '/' || c >= ':') {
isInteger = false;
}
}
mode = 0;
if(isInteger == true){
int y1 = Integer.parseInt(y);}
println(y1); //This line
boolean isInteger;
String y = "0";
class Button {
int x, y; // The x- and y-coordinates
int size; // Dimension (width and height)
color baseGray; // Default gray value
color overGray; // Value when mouse is over the button
color pressGray; // Value when mouse is over and pressed
boolean over = false; // True when the mouse is over
boolean pressed = false; // True when the mouse is over and pressed
Button(int xp, int yp, int s, color b, color o, color p) {
x = xp;
y = yp;
size = s;
baseGray = b;
overGray = o;
pressGray = p;
}
// Updates the over field every frame
void update() {
if ((mouseX >= x) && (mouseX <= x+size) &&
(mouseY >= y) && (mouseY <= y+size)) {
over = true;
} else {
over = false;
}
}
boolean press() {
if (over == true) {
pressed = true;
return true;
} else {
return false;
}
}
void release() {
pressed = false; // Set to false when the mouse is released
}
void display() {
if (pressed == true) {
fill(pressGray);
} else if (over == true) {
fill(overGray);
} else {
fill(baseGray);
}
stroke(255);
rect(x, y, size, size);
}}
Button button1, button2, button3;
int mode = 0;
void setup() {
frameRate (2);
size(100, 100);
smooth();
color gray = color(204);
color white = color(255);
color black = color(0);
button1 = new Button(10, 80, 10, gray, white, black);
}
void draw(){
frameRate (2);
background(204);
manageButtons();
noStroke();
fill(0);
isInteger = true;
if (mode == 1) {
y = JOptionPane.showInputDialog(null,
"Enter new setpoint (0 to 120):",
"Setpoint adjustment",
JOptionPane.PLAIN_MESSAGE);}
if (y == null) {
isInteger = false;
} int length = y.length();
if (length == 0) {
isInteger = false;
}
int i = 0;
if (length == 1) {
if (y.charAt(0) == '-') {
isInteger = false;
}
i = 0;
}
for (; i < length; i++) {
char c = y.charAt(i);
if (c <= '/' || c >= ':') {
isInteger = false;
}
}
mode = 0;
if(isInteger == true){
int y1 = Integer.parseInt(y);}
println(y1); //This line
//----------------------------------------------------------------------------------
}
void manageButtons() {
button1.update();
button1.display();
}
void mousePressed() {
if (button1.press() == true) { mode = 1; }
}
void mouseReleased() {
button1.release();
}
}
void manageButtons() {
button1.update();
button1.display();
}
void mousePressed() {
if (button1.press() == true) { mode = 1; }
}
void mouseReleased() {
button1.release();
}
1