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
I have a question: Can some one give me an example of searching the binary table to find the change from 1 to 0? For example, one line have
0 0 0 1 1 0 0 0 0 1 1 1 0 0
and it should give me 2 changes. I want to search only one line.
for(int i=1; i< binarytableline.length; i++){
if (binarytableline[i - 1] == 1 && line[i] == 0) changes++;
}
Remember that the indexes start at zero:
(Java docs)
Essentially, whats happening in the code above is that we are looping through the line. Now, i is the index. Lets start at the beginning of the loop.
i=1
So, binarytableline[1-1] = the first index of the binary table line, or the very first number. Now, we are seeing if it equals 1, and the second index, which is i, equals 0. To check, we do the following:
binarytableline[i - 1] == 1 && line[i] == 0
This would mean that there is a change in binary digits from one digit to the next, and in our example that is the 0th index to the first. Now, we would iterate our variable, changes by 1 by doing changes++. Again, this is in a for loop, meaning we will loop through all the elements like this. The amount of times it has changed will be recorded in int changes.
Let me know if this helped,
Ruchir
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
int keyCardNum = 10;
for(int x = 0; x<= keyCardNum; x++) {
System.out.println(x);
}
It prints the following:
0
1
2
3
4
5
6
7
8
9
10
Online I read that for loops start from 0. It that true and how can I remove the 0 and start from 1 to 10.
for(int x = 0; x <= keyCardNum; x++)
Defines three properties of the for loop:
int x = 0: one or more loop variables and their initial value. Executed once when the loop starts. Usually, this variable is called i.
x <= keyCardNum: the loop condition. Executed once per iteration. The loop terminates when this condition evaluates false. (the loop never runs if the condition evaluates to false immediately). Usually this is a less-than (<) expression. i=0;i<10;++i will loop 10 times, i=0;i<=10;++i will loop 11 times.
x++: the incrementor. Executed once per iteration. Here you define what happens after each loop iteration. Usually, you increment the loop counter variable, but you could do anything here.
The loop body is executed as many times as the loop condition evaluates to true. If you are interested in the nitty-gritty details, the Java Language Specification has them.
If you want a loop to start at 1, you have to initialize the loop variable with 1. Or, you "normalize" your loop variable when using them in an expression, e.g. x + 1 to produce a value offset by 1 (i.e. 1-11 in your example).
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I need to write a for loop in java that displays all of the numbers ranging from 13 - 93 that end in the number 3. It must include 13 and 93.
Loop from 13 to 93. Increment by 10. Like,
for (int i = 13; i <= 93; i += 10) {
// Print the value
}
If you wanted, you could write a function that achieves this task but with parameters
that allow for more flexible control. Where it takes in arguments representing a
from value and a to value, and something like a endsWith value. It's just a suggestion though.
Any decimal based number mod 10 will get you the last digit.
for (int i = start ; i <= end ; i++) {
if (i % 10 == 3) {
System.out.println(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 2 years ago.
Improve this question
Here is the code that I tried in VS Code:
int i = 1;
while (i < 10)
if((i++) % 2 == 0)
System.out.println(i);
And the output was:
3
5
7
9
At the first iteration of the loop i == 1.
i++ increments i to 2, but returns 1.
Therefore if((i++) % 2 == 0) evaluates to false (since 1 is not even), and nothing is printed.
In the next iteration, i++ increments i to 3, but returns 2.
Therefore if((i++) % 2 == 0) evaluates to true (since 2 is even), and 3 is printed.
1 would not be printed even if i was printed on the first iteration of the loop, since i++ increments i to 2 before the println statement.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have a loop in my java program and I want to run some test code every say 10th iteration in the loop. How do I do this ???
Use something like if (i % 10 == 0) where i is your loop counter. % is the remainder operator.
If i starts from 0 then the condition is true on the first iteration. If you want to start i from 0 but don't want the condition to be true until the 10th trial, then use if (i % 10 == 9).
You can use the % operator with 10 and your counter. See below for example.
for(int i=0;i<=100;i++){
if(i>0&&i%10==0){
System.out.println(i);//replace this with your code.
}
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I have a loop that checks the biggest number so far and adds one to it; but for example 1 2 3 4 5 I press button 6 is created, what if the list was 1 2 3 5 6 and I want it to print 4 so I can get a correct number line then proceed to increase it by one?
EDIT: I should mention that my list has a possiblity not being in order so 2 1 3 5
You should check incrementally that the numbers are good:
So say you have an array of int called numbers[]
you could do:
int placehodler = numbers[0] + 1;
for (int i = 1; i < numbers.length(); i++){
if (placeholder != numbers[i]) // checks if the next number is equal to the previous + 1
break; // breaks out of the loop if it isn't
placeholder++; // increment placeholder by 1
}
System.out.println(placeholder); // prints placeholder
So if the placeholder breaks out of the loop early, it is your 1,2,3,5,6 situation and it would have 4 (succeeds on the loop where placeholder = 3, fails when placeholder = 4) in the 1,2,3,4,5 situation, it would succeed on all and end the loop with a value of 6.
First, I would find the count of your current list. If your list is [1 2 3 5] the count is 4.
Then, I would create a new list that is a range of numbers from 1 to the count => [1 2 3 4]
comparing the two lists will let you see if any numbers are missing, if no numbers are missing then you can add 1 to the current count and append that to the end of the list.
sorting the list before doing any of this will take care of the out of order issue mentioned in the other comment.