I'm attempting the below:
if(p1S > p2S && p3S)
}
But the IDE doesn't like the && here.
So, how would I compare these 3 ints to find the one with the highest value, thus moving forward.
Aka: If Int 1 is greater than int 2, and int 3, then do this. . .
I suspect I just don't know the syntax well enough just yet (I've only just started college for CS).
Is this what you want?
if(p1S > p2S && p1S > p3S)
}
The reason the previous code didn't work is that the first part
if(p1S > p2S && p3S)
}
evaluates to:
if(boolean && p3S)
}
And you can't do an && between a boolean and an int.
For instance, would this program run:
*loop code.......*
if (variable1 != variable2 != variable3 != variable4){break loop;}
What you're really trying to test is whether there are any duplicate values in your set of variables. Here's a simple method to test that:
static boolean allUnique(Object... values) {
return new HashSet<>(Arrays.asList(values)).size() == values.length;
}
You can call it like this:
if (allUnique(variable1, variable2, variable3, variable4)) break loop;
if (new HashSet(variable1, variable2, variable3, variable4).size == 4) ...
Yes, you need to use the logical && AND operator.
More info in this SO answer.
Essentially you will use something like:
if (variable1 != variable2 && variable1 != variable3 && variable1 != variable4)
This will check that variable 1 is not equal to 2, 3 or 4.
String userGuessParameters = "a,b";
while (!Character.isDigit(userGuessParameters.charAt(0)) ||
!Character.isDigit(userGuessParameters.charAt(2)) ||
userGuessParameters.length() != 3 ||
(int)(userGuessParameters.charAt(0)) >= parameters[0] ||
(int)(userGuessParameters.charAt(2)) >= parameters[1]) {
System.out.print("Mida kaevame (rida, veerg): ");
userGuessParameters = userInput.nextLine();
userGuessParameters = userGuessParameters.replaceAll(" ", "");
}
I'm trying to check if all the required conditions are fulfilled in the while loop. parameters is what user entered in array form. Let's assume parameters = [4, 4] (Parameters is used to create a 4x4 map).
I need userGuessParameters to be:
Numbers
Length equal to 3 (the input looks like this: 2,2 when the spaces are removed)
Smaller than the biggest coordinate (which is 3,3)
But for some reason, the loop never exits. I'm almost certain it is because of the last two conditions in the while loop, but I can't find the mistake.
You are using the logical OR operator when you should be using AND (you want all the conditions to be met, not just at least one of them).
Use the java notation for AND which is &&:
(!Character.isDigit(userGuessParameters.charAt(0)) &&
!Character.isDigit(userGuessParameters.charAt(2)) &&
userGuessParameters.length() != 3 &&
(int)(userGuessParameters.charAt(0)) >= parameters[0] &&
(int)(userGuessParameters.charAt(2)) >= parameters[1])
Try to use && instead of || like this:
while (!Character.isDigit(userGuessParameters.charAt(0)) && !Character.isDigit(userGuessParameters.charAt(2))
&& userGuessParameters.length() != 3 && (int)(userGuessParameters.charAt(0)) >= parameters[0]
&& (int)(userGuessParameters.charAt(2)) >= parameters[1]) {
My code don't work. I expected to get the alert "You have xyz ....". Nothing happen. Its something wrong in the code?
if ((event.values[0] == 1 || event.values[0] == 2 || event.values[0] == 3)&&
(event.values[1] == 3 || event.values[1] == 4 || event.values[1] == 6)&&
(event.values[2] == 7 || event.values[2] == 8 || event.values[2] == 9)) {
values.setText("You have xyz ....");
}
Syntactic I don't see any obvious errors, however why you don't see your alert must be a result of incorrect values in your event[] variable. Have you tried debug your code?? To be sure what the values are?
Most probably the variables are wrong. I don't see a mistake in your code.
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I have started programming a few weeks ago in java/android. I want to write a small tic tac toe game as an android app but I'm having trouble with my method that will check for the winner. It is as follows:
public void checkForWinner() {
if( taken[0] && taken[3] && taken[6] ||
taken[0] && taken[1] && taken[2] ||
taken[2] && taken[5] && taken[8] ||
taken[6] && taken[7] && taken[8] ||
taken[0] && taken[4] && taken[8] ||
taken[2] && taken[4] && taken[6] ||
taken[1] && taken[4] && taken[7] ||
taken[3] && taken[4] && taken[5] == 1 ){}
}
Here I have an array called taken that holds 9 integers, each of those integers being either a one, meaning player one owns that block, or a two, meaning player two ows that block. Current, I am trying trying all possible scenarios in which player one would be the winner but eclipse is telling me that The operator && is undefined for the argument type(s) int, int. The error only seems to be showing for the first logical and operation of each line of the if statement. For example the first error goes up to taken[0] && taken[3] and then disappears until the next line.
Alternatively, you can check taken[n] values to see if they hold 1 or 2, if you think that'd make your code clearer:
(taken[0]==1 && taken[3]==1 && taken[6]==1)
Keep in mind that the && operator expects boolean operands...so it won't work with your int array the way you're expecting it to.
Swap your && to ==, you're trying to see if they're all the same value I assume which would show a winner, and be sure to use parentheses to sort it out, so one win condition would look like
((taken[0] == taken[3]) && (taken[0] == taken[6]))
However, this will only tell you that some player won, not which player. I guess you could check to see which player made the last move once it is determined that some one has won and declare that player as the winner.
In java,
if (a && b) or if (a || b)
works only if a and b are booleans / boolean expressions.
Here I have an array called taken that holds 9 integers, each of those integers being either a one, meaning player one owns that block, or a two, meaning player two owns that block.
In that case, you can replace the logical operators with bitwise operators:
int winner = taken[0] & taken[3] & taken[6]
| taken[0] & taken[1] & taken[2]
| taken[2] & taken[5] & taken[8]
| taken[6] & taken[7] & taken[8]
| taken[0] & taken[4] & taken[8]
| taken[2] & taken[4] & taken[6]
| taken[1] & taken[4] & taken[7]
| taken[3] & taken[4] & taken[5];
Then the variable winner will contain 1 if player 1 won, 2 if player 2 won, 0 if neither of them won, or 3 of both of them won (which probably isn't possible in your game).
1) You cant use && or || operators on int variables because the are meant for boolean values.
2) also use brackets to group conditions like
if (
(taken[0]==1 && taken[3]==1 && taken[6]==1)||
(taken[0]==1 && taken[1]==1 && taken[2]==1)||
...
OK there are two main problems here.
First, taken[0] needs to return either true or false to be able to remain as-is within the if-statement. You have mentioned it is an integer, so for it to return true or false, you'll need to do a comparison, such as taken[0] == 1.
Second, you need to use parentheses and do some groupings. Java does not respect whitespace. Instead of if( taken[0] && taken[3] && taken[6] || ..., you'll need to do if( (taken[0] && taken[3] && taken[6]) || .... That is, you need to put parentheses around each set of groupings.
The reason Eclipse is erroring is because the && operator is only for comparing booleans. That is true && true. You're giving it integers and it doesn't like that.
One way you might want to solve this is to write a function that determines if a solution is reached. So maybe something like:
private boolean isSolutionPresent(int[] taken, int index1, int index2, int index3) {
return (taken[index1] == 1 ) && (taken[index2] == 1) && (taken[index3] == 1);
}
Then you could convert your if statement to something like:
if (isSolutionPresent(taken, 0, 3, 6) ||
isSolutionPresent(taken, 0, 1, 2) ||
isSolutionPresent(taken, 2, 5, 8) || //... etc, removing the final ==1
Since the isSolutionPresent method returns a boolean (that is, true/false) you can apply the || operator to it.