Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
for(int i =0; i<8;i++){
for(int j =0; j<8;j++){
Ratsuk.getNewtablero().getMesa(i,j).setBackground(matrizcolor[i][j]);
if (Ratsuk.getNewtablero().getMesa(i,j).getBackground()==Color.lightGray);
Ratsuk.getNewtablero().getMesa(i,j).setEnabled(false);
}
}
Ratsuk.getNewtablero().getMesa(i,j) is for calling an a JButton 2d array that is inside newtablero and matrizcolor is a 2d array of colors of the same size.
when it runs this all the buttons in that array are disable not only the lightgray ones. Can any 1 explain me why?
You have a semicolon after the inner if-statement.
if (Ratsuk.getNewtablero().getMesa(i,j).getBackground()==Color.lightGray);
This causes the next line to execute every time. Remember that the compiler will associate either (1) a single statement or (2) a single block with any if-statement. In this case, the compiler is associating a single statement with that if-statement, but the single statement is merely a semicolon that literally does nothing. After the semicolon statement "executes", the program continues as normal by executing the next line:
Ratsuk.getNewtablero().getMesa(i,j).setEnabled(false);
Regardless of true or false value of the if-statement. Remove the semicolon and your problem will be fixed.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
i am trying to make java program that ask number from user and runs following calculation: 1*3*5*7*....*usergivendouble. I think for loop is best for this but not sure how to make such loop. i tried
for(double i=1;i<=n;i+=2)
{
n*= 2;
}
but it just never stops asking new number.
im new to java and all help is appreciated!
Assuming n is the user given number, increasing n inside the loop is the problem. You increase n inside the loop and also use it as the loop's end condition. This causes an infinite loop, as the loop's condition is never met.
You need to change the code to be:
double multiplyRestult=1;
for(double i=1;i<=n;i+=2)
{
multiplyRestult*= i;
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
how does increment the integer in (all) loops work ?
Like in the first time it initialize the integer then it checks the condition then increments the value and after that it does what i told it to do or it initialize the integer then it checks the condition and it does what i told it to do then increment the value ?
In a for loop,
for (a;b;c){
// code d
}
1) It runs statement a first.
2) Then checks statement b.
3a) If b fails it exits the loop.
3b) If it passes, it executes d ONCE. If at any point in d does it exit (eg. break), it will not do anything else in the loop. If it does not break and completes all the code in d, then it runs statement c once.
3) repeat steps 2, 3 until 3a is true.
Assuming you mean a for loop:
for (<initialization>; <condition>; <increment>) {
// loop body
}
The initialization is executed first. Then the condition is evaluated, and if it evaluates to true, the loop's body is executed. Once that's done, the increment is executed. Then, the condition is evaluated again, and the whole process repeats until the condition no longer evaluates to true.
for-loop:
initialize->condition->code->increment->condition──false──>end
└───────true───────┘
while-loop:
condition->code->condition──false──>end
└─true─┘
do-while-loop:
code->condition──false──>end
└─true─┘
PS: If you downvote, do it at least for an objective reason. If I made a mistake please tell me.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
The Boolean array is initialized to true. I need to know how to turn every third value to false and also going through the array over and over. It is essentially duck duck goose without the randomness.
This sounds like a beginners programming exercise, so I'm going to just give you a hint or two:
Go back to your textbook, lecture notes, tutorial and reread the stuff on for loops. Focus on the old style ones.
Think about how to write a for loop that steps through integer index values in the pattern that you require.
Re "... going through the array over and over" - not sure what you mean, but maybe the hint for this is to think about using nested loops; i.e. a loop inside another loop.
But the most important advice is to try and work this out for yourself.
Well I'm really not sure what you mean by going through the array over and over but the following code will turn every third value to false.
for (int i = 0; i < myVar.length; i++) {
if (i % 3 == 0) {
myVar[i] = false;
}
}
Edit: Oops someone beat me to it while I was typing lol.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I need to crate a loop which sums the arrays values which are within the interval (for instance 2-5). My main problem is getting from the first checked value of the array to the next one and so on. Thankyou in advance.
int x=0,y=0,s=0;
int[][] myArray = { {0,1,2,3}, {3,2,1,0}, {3,5,6,1}, {3,8,3,4} };
Scanner scan = new Scanner(System.in);
int b = scan.nextInt();//lowest value
int c = scan.nextInt(); //highest value
if (myArray[x][y]>b || myArray[x][y]<c)
s=s+myArray[x][y]
//then check next one
if (myArray[x][y]>b || myArray[x][y]<c)
s=s+myArray[x][y]
You need to put these in a nested for-loop. Otherwise, they are executed only once, with x=0 and y=0. So all you are doing is essentially:
if (myArray[0][0]>b || myArray[0][0]<c)
s=s+myArray[0][0]
So s could only possibly be 0 or the first element in the 2D array.
Now, this conditional:
if (myArray[x][y]>b || myArray[x][y]<c)
does not mean what you think it means. This would evaluate to true for all numbers as long as b is less than c. For the semantics you are looking for, you want an AND operator (&&).
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
int i=0;
while(!a.isEmpty()) {
if(a.toCharArray()[i]==' ') {
System.out.println(a.toCharArray()[i]);
}
i++;
}
When I run this program it's giving me an error: index out of bounds. How can I solve it?
You're not changing a in the loop, so a.isEmpty() will not change and you'll continue looping until i goes out of bounds. Did you mean:
while (i < a.length()) {
And as an aside, a.toCharArray()[i] can (and should) simply be a.charAt(i) (as #MartijnCourteaux also points out).
There are already a couple of answers that give solutions to the problem. This answer is about how you could have found the problem yourself.
Most array index out of bounds errors in loops are due to incorrect loop condition, so that the condition goes on being true even after the index has reached the end of the loop. Your loop condition did not depend on i, so the only way it could change from true to false is if a were changing inside the loop.
If you expected a to change, you should have put System.out.println(a) inside the loop, and monitored the value of a. You would have seen it not change.
Alternatively, if the isEmpty test were a typo, and you meant to test something that depends on i, desk checking your loop condition should have found it.
You should change your loop to this instead:
char[] c=a.toCharArray();
for(i=0; i<c.length; i++){