Re: GUI Calculator: Adding Answer Button? Please Help
Quote:
outside the if statements, it has no value.
No, the answer variable will have a value anywhere in the method. It will default to zero and then can be changed anywhere by an assignment statement.
Add printlns to show its value before and after all the if statements where it has a value assigned to it.
Re: GUI Calculator: Adding Answer Button? Please Help
Quote:
Originally Posted by
Norm
Add printlns to show its value before and after all the if statements where it has a value assigned to it.
I'm confused right now.. Can you show me an example of how this works?
Re: GUI Calculator: Adding Answer Button? Please Help
Code :
public void aMethod() {
int aMethodVar = 0; // define this variable at the method level
System.out.println("1aMethodVar=" + aMethodVar); // show its value here
if (cond) {
int x = 0; // define this variable inside of the if{} it goes away at the ending }
// aMethodVar is known here
aMethodVar = 234; // change the value
} // end of if x is now gone
System.out.println("2aMethodVar=" + aMethodVar); // show its value here
int anotherVar = 33; // define this variable at the method level
if(cond2) {
// anotherVar and aMethodVar are known here. x is NOT known
aMethodVar = 432; // change the value
}
} // end aMethod()
System.out.println("3aMethodVar=" + aMethodVar); // show its value here
1 Attachment(s)
Re: GUI Calculator: Adding Answer Button? Please Help
I still don't understand how the println's make a difference.. It's fine though, I've moved onto a new project. Here's my completed calculator.
Attachment 666
Thank you for all your help, and patience.
Re: GUI Calculator: Adding Answer Button? Please Help
Quote:
I still don't understand how the println's make a difference
They don't effect how your code executes.
The printlns are for debugging. To show you what the values of variables are as the code executes.