I would like to know why I get this following error in the terminal:
Users-MBP:~ user$ javac Testing2.java
Testing2.java:18: error: variable result is already defined in method main(String[])
boolean result = true;
^
1 error
When I try to run the following program:
public class Testing2 {
private static int TwiceRepetition(int[] elems) {
///
}
public static void main(String[] args) {
///
}
}
In fact, the desired output is that it prints true if there's a consecutive repetition length two of any element. Otherwise, it prints false.
The error states that variable result is already defined. Yes. But can't you redefine it just like in Python?
For example, in python, I'm allowed to do this (meaning there's no errors that prevent me from redefining the variable result to true if the conditions is satisfied):
def p1(x):
result = False
i = 0
while i < len(x) - 1:
if x[i]== x[i+1]:
result = True
i = i + 1
return result
But in java, it doesn't work, why?