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 1 year ago.
Improve this question
In below Question after entering into 2nd for-loop (for(int j=0;j<=5;j++)) does count have to follow the condition stated inside the while loop?
public class MyClass {
public static void main(String args[]) {
int count=1;
for(int i=1;i<=5;i++){
while(count%2!=0){
for(int j=0;j<=5;j++){
i=i+1;
j=j+1;
count++;
System.out.println(+count);
}
}
}
}
}
Using proper indentation always helps understanding the code better (if you are in Eclipse, this can be done by holding down the ctrl and shift keys, then pressing F).
The second for-loop will keep running and counting 'count' up untill 'j' is <= 5, and then when the loop in finished, the condition inside your while loop will be checked again. If this is still true, the second for-loop will run again
It will first execute the complete (2nd) for-loop and will check the condition of the while loop after completion of this for-loop. Count is 4 when the for-loop is finished, which does not meet the condition from the while and therefore it will not execute the for-loop again
Related
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 10 days ago.
This post was edited and submitted for review 10 days ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I'm starting with recursion in Java, and I'm having a problem with a counter, where I want to print all numbers from 1 to number, but it only prints the value of i. i was defined at the start of the class as:
int i = 1;
I've tried putting
i>=number
number>=i
i>=1
number>=0
number>=1
number>0
number>1
After getting rid of i, I also tried:
public static void ContadorCreciente(int number) {
if (i <= number) {
System.out.println(i);
ContadorCreciente(i++);
}
}
But now, this is what I've written so far:
public static void ContadorCreciente(int number) {
if (number > 1) {
ContadorDecreciente(number - 1);
System.out.println(number);
}
}
Ok, you're implementing ContadorCreciente with one number argument. Recursive, so you're going to call it again with either number+1 or number-1. For the +1 variant you have no way to stop it, that would keep going up forever, so that's no good, the recursive call has to be with -1.
The -1 recursion can stop e.g. at ContadorCreciente(0) which is expected to do nothing. I'm sure you can figure out how to do nothing and return immediately.
Inductive approach:
Assuming we already have ContadorCreciente(number-1) which prints all numbers 1..number-1. How would you use such a function to implement the case ContadorCreciente(number)?
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 3 years ago.
Improve this question
I am trying to update the for loop variable i inside the loop, why I cannot update it and the i is always 0?
public class HelloWorld{
public static void main(String []args){
for (int i = 0; i < 10 ; ){
i = i ++;
System.out.println(i);
}
}
}
I am expecting print out
0 1 2 3 4 5 6 7 8 9
but it is a infinite loop and print out all 0.
You can, but with i = i ++; you're not incrementing the counter. Use i++ instead - it's an operator that does not return a new value, only increments the primitive.
Keep in mind, that for loop is meant to contain the information about looping. If you manipulate the counter outside of loop, you might loose track of what is going on. This is not recommended, better use while in that place.
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 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 6 years ago.
Improve this question
When I run this code:
public class Calc2 {
public static void main(String[] args) {
double result;
result = Double.parseDouble(args[0]) + Double.parseDouble(args[1]);
System.out.print(result);
}
}
I get this:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at Calc2.main(Calc2.java:4)
You receive this error message because the length of the args array is shorter that the position you are tring to access. As the other answers have mentioned, you may not be sending the command line arguments, or you may be sending only one, and when you try to access the second one (args[1]) you receive Array index out of bounds exception.
The best thing you can do, is check for the length of the array before you access the values, or even iterating inside a for instruction until the length of the array is reached.
Use this instruction to print the values:
for(int i=0; i< arr.length;i++)
{
System.out.print(Double.parseDouble(args[i]));
}
And if it does not show anythng is because you are not passing the arguments to the program.
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
So, I'm trying to calculate a list of number to eventually sort, so I only want the final result of this for loop.
for (int anno=startyear; TimePeriod>=anno;anno++) {
System.out.println(anno);
}
Where anno = 1995 and I am counting to the current day, I end up getting a result that slowly counts up, where it first counts at 1995, then it counts 1995 and then 1996, and so on.
How do I only get the end result for use in my program? The result that would simply be 1995-2014. Not the repeats.
edit: Forgot to mention I need every number in between 1995-2014 as well
You shouldn't need a loop for this, assuming your TimePeriod variable equals 2014 then just do the following to print out the desired result:
System.out.println(startyear+"-"+TimePeriod);
That will print out:
1995-2014
You already know the final value: it's TimePeriod. If that's all you need, just use that and get rid of the loop:
System.out.printf("%d-%d", startyear, TimePeriod);