Jesper Young wrote:Please PostRealCode, you're posting a few loose lines and snippets, which makes it harder to see what you're trying to do and what exactly the problem is.
In Java, variables are passed by value. That means that the arguments to a method are new variables, that are initialized with the values of the variables that you pass when you call the method. Changing the value of an argument variable inside the method will not change the original variable in the place where you called the method. Have a look at this:
The variable x in myMethod() is a new variable that is initialized with the value of i (2) when the method is called. Assigning a different value to x inside the method has no effect on variable i in the main method.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
fred rosenberger wrote:you can change the original value if you use the return value to set the variable:
Don't understand the question.Joe Laviguer wrote: . . . Can I return through a second method where there are if statements?
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
fred rosenberger wrote:If i understand what you're asking, no.
You're 'myBool' on line 15 will change, but then that gets lost when you return back to your main() method.
You MAY be able to get around this by changing from boolean primitives to Boolean objects/references. I don't recall if they are immutable or not, and can't test right now.
or you could do this:
Some people prefer only one 'return' statement in a method, so it could be adapted to:
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
fred rosenberger wrote:
This doesn't make sense. You can't overlap a for and an if like this. You have to completely nest them. Where you're comment says "end for", you're actually closing your 'if' and vice versa.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
fred rosenberger wrote:can't you just change the signature so it returns a Boolean?
Joe Laviguer wrote:Can I have a public static boolean if there is a string array and an int as well as a boolean variable in the method?
Paul Clapham wrote:
Joe Laviguer wrote:Can I have a public static boolean if there is a string array and an int as well as a boolean variable in the method?
You can declare your method to return whatever you like. There aren't any rules which say "You can't return X if your parameters include Y or Z"... why would any language have a rule like that?
Joe Laviguer wrote:
Paul Clapham wrote:
Joe Laviguer wrote:Can I have a public static boolean if there is a string array and an int as well as a boolean variable in the method?
You can declare your method to return whatever you like. There aren't any rules which say "You can't return X if your parameters include Y or Z"... why would any language have a rule like that?
That's why I'm here in Beginning Java. I don't know these things yet.
Paul Clapham wrote:
Joe Laviguer wrote:
Paul Clapham wrote:
Joe Laviguer wrote:Can I have a public static boolean if there is a string array and an int as well as a boolean variable in the method?
You can declare your method to return whatever you like. There aren't any rules which say "You can't return X if your parameters include Y or Z"... why would any language have a rule like that?
That's why I'm here in Beginning Java. I don't know these things yet.
Well, okay. But you're just making things harder for yourself if you start imagining weird stuff like that. If you decide you want your method to return a boolean, just try it. Don't go looking for trouble by trying to dream up reasons why you might not be able to do that, just do it. The trouble will find you soon enough if it actually exists.
And believe it or not, the Java language was designed to be simple. As such, you'll generally find the rules are simple. So for a method, it can be a void method (doesn't return anything) or it can return some type. That's all. It's simple, and there's no reason for the rule to be more complicated than that.
Joe Laviguer wrote:
Paul Clapham wrote:
Joe Laviguer wrote:
Paul Clapham wrote:
Joe Laviguer wrote:Can I have a public static boolean if there is a string array and an int as well as a boolean variable in the method?
You can declare your method to return whatever you like. There aren't any rules which say "You can't return X if your parameters include Y or Z"... why would any language have a rule like that?
That's why I'm here in Beginning Java. I don't know these things yet.
Well, okay. But you're just making things harder for yourself if you start imagining weird stuff like that. If you decide you want your method to return a boolean, just try it. Don't go looking for trouble by trying to dream up reasons why you might not be able to do that, just do it. The trouble will find you soon enough if it actually exists.
And believe it or not, the Java language was designed to be simple. As such, you'll generally find the rules are simple. So for a method, it can be a void method (doesn't return anything) or it can return some type. That's all. It's simple, and there's no reason for the rule to be more complicated than that.
So are you saying the following code would work (I can't test it here at work...have to wait until I get home)? Note: I have boolean bWin = false; initialized in Main.
This is a bad idea. method (including main) should be kept as small as possible. Most people would tell you it's better to pull stuff OUT of a big method to make several smaller ones, rather than vice versa.Another thing I tried was to completely get away from the CheckWin method and imbed that code into the main method,
((sBoard[0][i] == "X") && (sBoard[1][i] == "X") && (sBoard[2][i] == "X"))
|| ((sBoard[i][0] == "X") && (sBoard[i][1] == "X") && (sBoard[i][2] == "X"))
|| ((sBoard[0][0] == "X") && (sBoard[1][1] == "X") && (sBoard[2][2] == "X"))
|| ((sBoard[0][2] == "X") && (sBoard{1][1] == "X") && (sBoard[2][0] == "X"))
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
fred rosenberger wrote:Java requires you to have a return for every possible execution path. I don't think the compiler is smart enough to know that you will ALWAY run in your for loop, so it needs to have something to return just in case the loop is skipped.
Why won't the code run correctly with the return outside the loop? The way it's written now, you will ALWAYS return on the first iteration through the loop:
REPLY: Well that's the thing. I only want the boolean changed to true when the if conditions are met. If the user has three X's in a row, he wins, and the game is over. If changing the boolean to true is outside of the if loop, it will change the boolean no matter what happens, so theoretically, the game will be over after the very first move. Is my logic correct?
This is a bad idea. method (including main) should be kept as small as possible. Most people would tell you it's better to pull stuff OUT of a big method to make several smaller ones, rather than vice versa.Another thing I tried was to completely get away from the CheckWin method and imbed that code into the main method,
((sBoard[0][i] == "X") && (sBoard[1][i] == "X") && (sBoard[2][i] == "X"))
|| ((sBoard[i][0] == "X") && (sBoard[i][1] == "X") && (sBoard[i][2] == "X"))
|| ((sBoard[0][0] == "X") && (sBoard[1][1] == "X") && (sBoard[2][2] == "X"))
|| ((sBoard[0][2] == "X") && (sBoard{1][1] == "X") && (sBoard[2][0] == "X"))
This is REALLY ugly. it's hard to read. I'd probably pull this into a method by itself...something like
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Proudly marching to the beat of a different kettle of fish... while reading this tiny ad
Smokeless wood heat with a rocket mass heater
https://woodheat.net
|