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)?
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 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
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
This is only my third program in java. I'm stuck on this part of the problem.
"The formula is not valid if T > 50 in absolute value or if v > 120 or < 3"
I'm not sure how to translate this into code while restricted to use the following:
no, if statements
no, loops
no, importing
no, new classes to be added
Thank you!
public boolean validateFormula(int T, int v) {
return !(abs(T)>50 || v > 120 || v < 3);
}
In giving you a direct answer to this problem, you might not be able to replicate it next time, or to be able to think for yourself.
In import java.util.* , Math.abs(int n) will return the absolute value of n. The || operator means 'or'. I will not assume that you know the basic structure of an if loop so here:
if(condition || condition || condition){
//execute code here
}
You have everything that you need to solve this, good luck. Furthermore, if you do have any more simple questions like this, I would suggest doing a google search instead.
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
Can you help me to understand what is the meaning of these randomization?
I have found it in a c code that I have to translate, it return always 41:
int main(){
srand(1);
printf("\n%d",rand());
}
How I can emulate the srand(1) and rand() in Java?
My answer assumes that you just want to emulate the behavior of srand() and rand(), but do not care about the '41' you say that you always get.
This is basically just the idea of pseudo-random number generation. If you set the seed to a constant value (1 in this case), then call the random() function, it will always return the same value. This is because it is basically saying "set the index to 1 in the big list of 'random' numbers" so that the next time I call random it returns the n-th value in that 'list'. In reality, it is a bit more complex, but that's how I like to think about it sometimes. To emulate the behavior of your code in Java, you can try the following:
public static void main(String[] args) {
Random rand = new Random(1);
System.out.println(String.valueOf(rand.nextInt()));
}
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
When working with recursion I realized I'm not sure how the return statement works. Does it stop and return true when target.contains(key) returns true or does it fall out and return false, because of the line below? Does the previous iterations of the method get finished so that it instead return false?
The program creates passwords and this method is called to check that the password contains one of the required fields, such as upper case letters, symbols or numbers. It's called with 4 separate sources and they are then used to tell the program to keep the password or to create a new one if it doesn't meet the required standards. I've done this program for fun to refresh my memory of Java, it's not a real program that anyone will ever use.
private static boolean containsKeyword(String target, String source, int placement){
String key = String.valueOf(source.charAt(placement));
if(target.contains(key))
return true;
if(placement==0)
return false;
containsKeyword(target, source, placement-1);
return false;
}
You seem to be missing the whole point of the recursion step.
Change this:
someFunc(a, b, nbr-1);
return false;
To this:
return someFunc(a, b, nbr-1);
By the way, recursively calling this function with the exact same data (the strings a and b) is pointless.
There must be something else that you want to call this function with (perhaps sub-strings of a and b).
Your method will always return false if it doesn't get into the first if. You need to change this:
someFunc(a, b, nbr-1);
return false;
to
return someFunc(a, b, nbr-1);
Maybe if you update your question with what exactly you are trying to do, you will get a more targetted answer that will help you understand the recursion better.
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);