#Why does IJ say there need to be a return statement?
75 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @gilded hare! Please use
/closeor theClose Postbutton above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
Please format your code to make it more readable.
For java, it should look like this:
````java
public void foo() {
}
````
public static int oddPositive(int n) { int total = 0; for (int x = 0; x <= n; x++) { if (x % 2 != 0) { total += x; } else { total += 0; } } System.out.print(total); }
Do you know how methods work?
uh maybe?
See i tried thinking of it like python.. cause thats what i learned first, dont really understand why theres a ++
What are you trying to accomplish with this method?
Write a short Java method that takes an integer n and returns the sum of all the odd positive integers less than or equal to n.*/
Alright. So it's telling you that you need to return a sum, which in this case is represented by a single integer value
The method should return an integer, denoted by the return type in the method declaration. That means that when the flow of execution exits the method, it should do so with an integer value, which can then be assigned to a variable in the place where this method is called from
oh yea i also have this in main Exercises.oddPositive(3);
All non-void methods must return a value in all possible branches the flow of execution takes
In your case, after the scope of the for loop, you need to add the return keyword, followed by the value you want to return, which in this case is contained within the variable total
As a sidenote, you don't need the else branch there that just increments the variable by 0. I'm pretty sure you know why that's useless. An if statement does not need to be followed by an else branch
it doesnt in java?
Finally, the ++ is known as the increment operator. It is a single operand operator which can be used on numeric data types, and it basically just increments the variable by 1. Depending on whether you write the ++ before or after the variable, it is called the post and pre increment operator respectively. The former increments the variable first, then it retrieves the value from the variable, the former gets the value first, then increments
No xd
so basically i can delete eveything in the else land
In this case, the post increment operator is used to increment the looping variable x by one per each iteration
Yes, since it is not doing anything (useless)
Just as there is the pre and post increment operators, there is also the pre and post decrement operator, written as --
so it should look like this? int total = 0; for (int x = 0; x <= n; x++) { if (x % 2 != 0) { total += x; return total; } System.out.print(total); }
idk why it isnt lined up
but we dont talk about that
No. The return should be after the scope of the loop, not inside the scope of the if branch
so basically im learning.. that i need to turn off python brain
Because you want it to iterate over all numbers before n, then return the sum
If you leave it like that, it would just start iterating at 0, the if check will fail, iterate to 1, the if statement passes, it increments total by 1, and then it returns, leaving you with only 1
In this context, scope means the lines of code surrounded by the curly braces {} of an expression
int total = 0;
for (int x = 0; x <= n; x++) {
if (x % 2 != 0) {
total += x;}
return total;
System.out.print(total);
}```
I think you are missing a closing curly brace before the return
Since you have two tested control flow statements
int total = 0;
for (int x = 0; x <= n; x++) {
if (x % 2 != 0) {
total += x;}
}return total;
System.out.print(total);
}```
see now its telling me the system.out.print is unreachable
these brackets are gonna be the fuckin death of me
OHH
IT HAS TO BE THE OPPOSITE WAU
Python being the only language that defines scope with white space and indentation 😔
I have python brain mann
why is it outputting like this Here is the sum of the odd:0 Here is the sum of the odd:1 Here is the sum of the odd:1 Here is the sum of the odd:4
Can you show me your current code?
main Exercises.oddPositive(3); other file ```public static int oddPositive(int n) {
int total = 0;
for (int x = 0; x <= n; x++) {
if (x % 2 != 0) {
total += x;}
System.out.print("\nHere is the sum of the odd:" + total);
}return total;
}
at least the last one of the output is correct
nvm
i fixed
Think about the flow of execution in your program. You have your print statement within the scope of the for loop. This means that for every iteration of the loop, you are printing out the value of the total variable that it has at that moment
I did int odd = Exercises.oddPositive(3); System.out.print("\nHere is the sum of the odd:" + odd); instead
And what are you getting now?
Here is the sum of the odd:4
our teacher showed us that way with boolean so.. i thought it could work
Well, that's the correct output given an input of 3
1 + 3 = 4
WOO HOO it works
two down two more to go...
out of curosity would there have been an easier way to do that
this way makes sense but knowing other ways is nice
I don't know about easier way, but I do know about a shorter way
I know 2 shorter ways, one easy to understand, and one harder to understand. Do you want to see them xd
ye
cause this was supposed to be "short" and idk the definition of short
cause i had to do sum of all positive intergers and that was uh 3 lines
short easy to understand
private static int oddPositive(int n){
int total = 0;
for(int x = 1; x <= n; x += 2){
total += x;
}
return total;
}
short hard to understand
private static int oddPositive(int n){
int total = IntStream.rangeClosed(1, n).filter(i -> i % 2 != 0).sum();
return total;
}
first one starts iterating from one instead of 0, but instead of incrementing the looping variable by 1 for each iteration, it increments it by 2. That way you know that x will never be an even number, so you can avoid the extra check
the second one is based on a programming paradigm called functional programming. Essnetially, I'm creating a list of numbers than range from 1 to the inputted number inclusive, then applying a filter function that only leaves the odd numbers, then summing them all up
the second one is like the for x in range, in python
maybe? I don't know? Im not familiar with Python
in python, in range returns a list of numbers, which then you can do whatever you want with
anyways. Let me know if you need further help
yes person! I might be back tomorrow to deal with a different one I dont know how to do
how does one close this
.