Jump to content

Help with returning.

Go to solution Solved by -rascal-,

EDIT: Oh...didn't read the question. Gimme a minmute.

 

So from this little bit of code, if you are setting belowThis to 10, are you trying to return 3, 5, 10?

What do you want the program to return?

 

Your OR that is an issue. It's double horizontal bars, not one.

if ( i % 3 == 0 || i % 5 == 0)

 

You sum all the values that makes the if statement true.

numbers = number + i;

 

Return the total value once it has finished with everything inside the for loop.

return numbers;

 

Make sure you declare your static int number. Otherwise, you could get crazy numbers depending on what was stored the block of memory in RAM when it was previously used for by the OS / another application.

static int numbers = 0;

 

If you were using your variable static int numbers like

numbers = i;

Then you don't NEED to initialize your variable as you'll be storing a new value into it anyways. Over-writing if you will.

 

You are NOT including 10, so your for loop should be this

for ( int i = 1; i < num; i++)

 

So...for belowThis = 10

 static int numbers = 0;/*	Other parts of your code	Other parts of your code	Other parts of your code*/for (int i = 1; i < num; i++){	if(i % 3 == 0 || i % 5 == 0)	{		numbers = numbers + i;	}}return numbers;

When i is 1, if statement is FALSE, doesn't run what is inside the if

When i is 2, if statement is FALSE, doesn't run what is inside the if

When i is 3, if statement is TRUE, numbers = 0 + 3

When i is 4, if statement is FALSE, doesn't run what is inside the if

When i is 5, if statement is TRUE, numbers = 3 + 5

When i is 6, if statement is TRUE, numbers = 8 + 6

When i is 7, if statement is FALSE, doesn't run what is inside the if

When i is 8, if statement is FALSE, doesn't run what is inside the if

When i is 9, if statement is TRUE, numbers = 14 + 9

total is 23

I am doing this problem: Problem 1 - Project Euler

 

I need help with how to face some trouble I'm having. First of all, Here's my code. The variable and method names ARE NOT GOOD. I am going to change them later.

class Multiples{	// Variable set up:	// FIX ALL VARIABLE NAMES	static int numbers;		static int belowThis = 10;	// Check all numbers below (and including) this number		public static void main(String[] args){		System.out.println( foo(belowThis));					}		// Change this method name please	public static int foo(int num){				for(int i = 1; i <= num; i++)		{			if (i % 3 == 0 | i % 5 == 0)			{				numbers = i;				return numbers;			}		}	}}

Here's my problem: When I try to build this code, It says: error: missing return statement

 

So I know I need to fix this by putting a return statement inside the foo class but not in the for statement, but what's the best way of doing this? Thanks!

Link to comment
https://linustechtips.com/topic/285307-help-with-returning/
Share on other sites

Link to post
Share on other sites

EDIT: Oh...didn't read the question. Gimme a minmute.

 

So from this little bit of code, if you are setting belowThis to 10, are you trying to return 3, 5, 10?

What do you want the program to return?

 

Your OR that is an issue. It's double horizontal bars, not one.

if ( i % 3 == 0 || i % 5 == 0)

 

You sum all the values that makes the if statement true.

numbers = number + i;

 

Return the total value once it has finished with everything inside the for loop.

return numbers;

 

Make sure you declare your static int number. Otherwise, you could get crazy numbers depending on what was stored the block of memory in RAM when it was previously used for by the OS / another application.

static int numbers = 0;

 

If you were using your variable static int numbers like

numbers = i;

Then you don't NEED to initialize your variable as you'll be storing a new value into it anyways. Over-writing if you will.

 

You are NOT including 10, so your for loop should be this

for ( int i = 1; i < num; i++)

 

So...for belowThis = 10

 static int numbers = 0;/*	Other parts of your code	Other parts of your code	Other parts of your code*/for (int i = 1; i < num; i++){	if(i % 3 == 0 || i % 5 == 0)	{		numbers = numbers + i;	}}return numbers;

When i is 1, if statement is FALSE, doesn't run what is inside the if

When i is 2, if statement is FALSE, doesn't run what is inside the if

When i is 3, if statement is TRUE, numbers = 0 + 3

When i is 4, if statement is FALSE, doesn't run what is inside the if

When i is 5, if statement is TRUE, numbers = 3 + 5

When i is 6, if statement is TRUE, numbers = 8 + 6

When i is 7, if statement is FALSE, doesn't run what is inside the if

When i is 8, if statement is FALSE, doesn't run what is inside the if

When i is 9, if statement is TRUE, numbers = 14 + 9

total is 23

AMD Ryzen 9000 Rig

  • AMD R7 9800X3D + Alphacool CORE 1 w/ Performance Mount Kit + Thermal Grizzly AM5 Contact Frame
  • Gigabyte X870E Aorus Pro Ice
  • 32GB (16GB X2) G.Skill Trident Z5 Neo RGB DDR5-6400
  • Sapphire NITRO+ 6800 XT Special Edition + EKwb Full Cover Block
  • Custom Loop w/ 2x 360mm Radiators
  • WD SN850X + WD SN750 + Samsung 980
  • EVGA P2 850W + Red/White CableMod Cables
  • Lian-Li O11 Dynamic EVO XL

AMD Ryzen 5000 Rig

  • AMD R7-5800X
  • Gigabyte B550 Aorus Pro AC
  • 32GB (16GB X 2) Crucial Ballistix RGB DDR4-3600
  • Gigabyte Vision RTX 3060 Ti OC
  • EKwb D-RGB 360mm AIO
  • Intel 660p NVMe 1TB + Crucial MX500 1TB + WD Black 1TB HDD
  • EVGA P2 850W + White CableMod cables
  • Lian-Li LanCool II Mesh - White

Intel i7-8086K / Z390 Rig (Decommissioned Q2' 2025)

Intel i7-6800K / X99 Rig (Officially Decommissioned, Dead CPU returned to Intel)
Intel i5-4690K / Z97 Rig (Decommissioned)

AMD FX-8350 / 990FX Rig (Decommissioned)

AMD Phenom II X6 1090T / 890FX Rig (Decommissioned)

 

<> Electrical Engineer , B.Eng <>

<> Electronics & Computer Engineering Technologist (Diploma + Advanced Diploma) <>

<> Electronics Engineering Technician for the Canadian Department of National Defence <>

Link to comment
https://linustechtips.com/topic/285307-help-with-returning/#findComment-3873851
Share on other sites

Link to post
Share on other sites

EDIT: Oh...didn't read the question. Gimme a minmute.

So from this little bit of code, if you are setting belowThis to 10, are you trying to return 3, 5, 10?

What do you want the program to return?

Your OR that is an issue. It's double horizontal bars, not one.

if ( i % 3 == 0 || i % 5 == 0)

You sum all the values that makes the if statement true.

numbers = number + i;

Return the total value once it has finished with everything inside the for loop.

return numbers;

Make sure you declare your static int number. Otherwise, you could get crazy numbers depending on what was stored the block of memory in RAM was used for by the OS.

static int numbers = 0;

If you were using your variable static int numbers like

numbers = i;

Then you don't NEED to initialize your variable as you'll be storing a new value into it anyways. Over-writing if you will.

So...for belowThis = 10

static int numbers = 0;/*	Other parts of your code	Other parts of your code	Other parts of your code*/for (int i = 1; i <= num; i++){	if(i % 3 == 0 || i % 5 == 0)	{		numbers = numbers + i;	}}return numbers;
this right here. Also, the reason it was complaining was because your return statement was in an if block. Any method that returns a value must have all possible paths return a value, even if it's just a default one. Your return statement within the loop was not needed, it would just return the first number divisible by 3 or 5 it finds. You would want to either add them to the numbers variable, as in the quoted post, and then return at the end of the loop, so it has looped through all the numbers.

EDIT: Also, @-rascal- the OR was fine. You can use single or double vertical bars. The double vertical bars, however, are better practice, as the rest of the statement is ignored if the first condition is true. A single bar statement would read both statements regardless

Link to comment
https://linustechtips.com/topic/285307-help-with-returning/#findComment-3874007
Share on other sites

Link to post
Share on other sites

I almost forgot this:

 

You are NOT including 10, so your for loop should be this

for ( int i = 1; i < num; i++)

 

<= will include 10, so your final result will be 33 instead of 23.

 

 

I've gone through a few 1st ,2nd, and 3rd year programming courses, and some professors are picky about the "standards and best practices."

Like..."proper" place for the curly braces should be..

for( ...stuff here...){    //code    //code}

not

for (..stuff here...){   // code   // code}

This came from one of my programming prof that used to work for Mathworks / MATLAB.

AMD Ryzen 9000 Rig

  • AMD R7 9800X3D + Alphacool CORE 1 w/ Performance Mount Kit + Thermal Grizzly AM5 Contact Frame
  • Gigabyte X870E Aorus Pro Ice
  • 32GB (16GB X2) G.Skill Trident Z5 Neo RGB DDR5-6400
  • Sapphire NITRO+ 6800 XT Special Edition + EKwb Full Cover Block
  • Custom Loop w/ 2x 360mm Radiators
  • WD SN850X + WD SN750 + Samsung 980
  • EVGA P2 850W + Red/White CableMod Cables
  • Lian-Li O11 Dynamic EVO XL

AMD Ryzen 5000 Rig

  • AMD R7-5800X
  • Gigabyte B550 Aorus Pro AC
  • 32GB (16GB X 2) Crucial Ballistix RGB DDR4-3600
  • Gigabyte Vision RTX 3060 Ti OC
  • EKwb D-RGB 360mm AIO
  • Intel 660p NVMe 1TB + Crucial MX500 1TB + WD Black 1TB HDD
  • EVGA P2 850W + White CableMod cables
  • Lian-Li LanCool II Mesh - White

Intel i7-8086K / Z390 Rig (Decommissioned Q2' 2025)

Intel i7-6800K / X99 Rig (Officially Decommissioned, Dead CPU returned to Intel)
Intel i5-4690K / Z97 Rig (Decommissioned)

AMD FX-8350 / 990FX Rig (Decommissioned)

AMD Phenom II X6 1090T / 890FX Rig (Decommissioned)

 

<> Electrical Engineer , B.Eng <>

<> Electronics & Computer Engineering Technologist (Diploma + Advanced Diploma) <>

<> Electronics Engineering Technician for the Canadian Department of National Defence <>

Link to comment
https://linustechtips.com/topic/285307-help-with-returning/#findComment-3874086
Share on other sites

Link to post
Share on other sites

I almost forgot this:

 

You are NOT including 10, so your for loop should be this

for ( int i = 1; i < num; i++)

 

<= will include 10, so your final result will be 33 instead of 23.

 

 

I've gone through a few 1st ,2nd, and 3rd year programming courses, and some professors are picky about the "standards and best practices."

Like..."proper" place for the curly braces should be..

for( ...stuff here...){    //code    //code}

not

for (..stuff here...){   // code   // code}

This came from one of my programming prof that used to work for Mathworks / MATLAB.

Thank you so much! What is the difference between | and || ? It seems to work with both. Should I do my curly brackets like that all the time? When should I and when shouldn't I?

 

Here is my final code. I changed the class and method name, and changed 10 to 1000.

class Problem{	// Variable set up:	static int numbers;		static int belowThis = 1000;	// Check all numbers below this number		public static void main(String[] args){		System.out.println( multiples(belowThis));					}		// Change this method name please	public static int multiples(int num){				for(int i = 1; i < num; i++){						if (i % 3 == 0 || i % 5 == 0)			{				numbers += i;			}		}		return numbers;	}} 

However, something is odd... When I run through CMD it says 23, but here it says the correct answer (233168)

Link to comment
https://linustechtips.com/topic/285307-help-with-returning/#findComment-3874167
Share on other sites

Link to post
Share on other sites

Thank you so much! What is the difference between | and || ? It seems to work with both. Should I do my curly brackets like that all the time? When should I and when shouldn't I?

Here is my final code. I changed the class and method name, and changed 10 to 1000.

class Problem{	// Variable set up:	static int numbers;		static int belowThis = 1000;	// Check all numbers below this number		public static void main(String[] args){		System.out.println( multiples(belowThis));					}		// Change this method name please	public static int multiples(int num){				for(int i = 1; i < num; i++){						if (i % 3 == 0 || i % 5 == 0)			{				numbers += i;			}		}		return numbers;	}}
However, something is odd... When I run through CMD it says 23, but here it says the correct answer (233168)
IMO, either way you choose to do the curly braces is fine. Whatever you do, KEEP IT CONSISTENT THROUGHOUT.

Did you recompile your code before running it again with 1000?

AMD Ryzen 9000 Rig

  • AMD R7 9800X3D + Alphacool CORE 1 w/ Performance Mount Kit + Thermal Grizzly AM5 Contact Frame
  • Gigabyte X870E Aorus Pro Ice
  • 32GB (16GB X2) G.Skill Trident Z5 Neo RGB DDR5-6400
  • Sapphire NITRO+ 6800 XT Special Edition + EKwb Full Cover Block
  • Custom Loop w/ 2x 360mm Radiators
  • WD SN850X + WD SN750 + Samsung 980
  • EVGA P2 850W + Red/White CableMod Cables
  • Lian-Li O11 Dynamic EVO XL

AMD Ryzen 5000 Rig

  • AMD R7-5800X
  • Gigabyte B550 Aorus Pro AC
  • 32GB (16GB X 2) Crucial Ballistix RGB DDR4-3600
  • Gigabyte Vision RTX 3060 Ti OC
  • EKwb D-RGB 360mm AIO
  • Intel 660p NVMe 1TB + Crucial MX500 1TB + WD Black 1TB HDD
  • EVGA P2 850W + White CableMod cables
  • Lian-Li LanCool II Mesh - White

Intel i7-8086K / Z390 Rig (Decommissioned Q2' 2025)

Intel i7-6800K / X99 Rig (Officially Decommissioned, Dead CPU returned to Intel)
Intel i5-4690K / Z97 Rig (Decommissioned)

AMD FX-8350 / 990FX Rig (Decommissioned)

AMD Phenom II X6 1090T / 890FX Rig (Decommissioned)

 

<> Electrical Engineer , B.Eng <>

<> Electronics & Computer Engineering Technologist (Diploma + Advanced Diploma) <>

<> Electronics Engineering Technician for the Canadian Department of National Defence <>

Link to comment
https://linustechtips.com/topic/285307-help-with-returning/#findComment-3874299
Share on other sites

Link to post
Share on other sites

IMO, either way you choose to do the curly braces is fine. Whatever you do, KEEP IT CONSISTENT THROUGHOUT.

Did you recompile your code before running it again with 1000?

Oh, I still had the Multiples.class and I call running that instead of Problem.class

 

My bad.

Link to comment
https://linustechtips.com/topic/285307-help-with-returning/#findComment-3874803
Share on other sites

Link to post
Share on other sites

IMO, either way you choose to do the curly braces is fine. Whatever you do, KEEP IT CONSISTENT THROUGHOUT.

Did you recompile your code before running it again with 1000?

 

Should I use different curly bracket styles depending on what I'm doing? IE for loops, if statements, etc

Link to comment
https://linustechtips.com/topic/285307-help-with-returning/#findComment-3874811
Share on other sites

Link to post
Share on other sites

Should I use different curly bracket styles depending on what I'm doing? IE for loops, if statements, etc

 

Nope.

 

From what I know,

for (...) {  // code  // code}

was the ORIGINAL method when programming first started with Fortran (?). It also helps prevent semi-colon errors. If you did this...

if (...) { ;  // code  // code}

...The complier will spit out an error, while...

if(...);{  // code  // code}

...depending on how it is coded, it will actually run the "if" line and skip everything is inside the curly braces. Depending on the complier, it may think all the code in the curly brace is unused code and will actually ignore it. If that's the case, you will have a reeeaaally big headache trying to find out what the problem is.

 

I've used the

if (...){  // code  // code}

method the longest, and it spaces things out a bit, making it some-what easier to read. Depending on the IDE / program you use for your programming, it's easier to do read doing it this way.

A lot of the old programming wise ones (people who started programming back in the 1950's  / 60's / 70's) will typically use the other curly braces method.

 

Regardless, just don't alternate back and forth between the two in a program. Stick with one, and be consistent throughout!!!

 

 

Useful tip:

Even for one-liners like

if(value == 100)    break;

Use curly braces!!

if(value == 100){  break;}OR (whichever way you like)if(value == 100){  break;}

Curly braces are not needed, but my god it can save HOURS you're when debugging code.

AMD Ryzen 9000 Rig

  • AMD R7 9800X3D + Alphacool CORE 1 w/ Performance Mount Kit + Thermal Grizzly AM5 Contact Frame
  • Gigabyte X870E Aorus Pro Ice
  • 32GB (16GB X2) G.Skill Trident Z5 Neo RGB DDR5-6400
  • Sapphire NITRO+ 6800 XT Special Edition + EKwb Full Cover Block
  • Custom Loop w/ 2x 360mm Radiators
  • WD SN850X + WD SN750 + Samsung 980
  • EVGA P2 850W + Red/White CableMod Cables
  • Lian-Li O11 Dynamic EVO XL

AMD Ryzen 5000 Rig

  • AMD R7-5800X
  • Gigabyte B550 Aorus Pro AC
  • 32GB (16GB X 2) Crucial Ballistix RGB DDR4-3600
  • Gigabyte Vision RTX 3060 Ti OC
  • EKwb D-RGB 360mm AIO
  • Intel 660p NVMe 1TB + Crucial MX500 1TB + WD Black 1TB HDD
  • EVGA P2 850W + White CableMod cables
  • Lian-Li LanCool II Mesh - White

Intel i7-8086K / Z390 Rig (Decommissioned Q2' 2025)

Intel i7-6800K / X99 Rig (Officially Decommissioned, Dead CPU returned to Intel)
Intel i5-4690K / Z97 Rig (Decommissioned)

AMD FX-8350 / 990FX Rig (Decommissioned)

AMD Phenom II X6 1090T / 890FX Rig (Decommissioned)

 

<> Electrical Engineer , B.Eng <>

<> Electronics & Computer Engineering Technologist (Diploma + Advanced Diploma) <>

<> Electronics Engineering Technician for the Canadian Department of National Defence <>

Link to comment
https://linustechtips.com/topic/285307-help-with-returning/#findComment-3874946
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×