Jump to content

Help with returning.

darth_bubbles
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
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

Intel Z390 Rig ( *NEW* Primary )

Intel X99 Rig (Officially Decommissioned, Dead CPU returned to Intel)

  • i7-8086K @ 5.1 GHz
  • Gigabyte Z390 Aorus Master
  • Sapphire NITRO+ RX 6800 XT S.E + EKwb Quantum Vector Full Cover Waterblock
  • 32GB G.Skill TridentZ DDR4-3000 CL14 @ DDR-3400 custom CL15 timings
  • SanDisk 480 GB SSD + 1TB Samsung 860 EVO +  500GB Samsung 980 + 1TB WD SN750
  • EVGA SuperNOVA 850W P2 + Red/White CableMod Cables
  • Lian-Li O11 Dynamic EVO XL
  • Ekwb Custom loop + 2x EKwb Quantum Surface P360M Radiators
  • Logitech G502 Proteus Spectrum + Corsair K70 (Red LED, anodized black, Cheery MX Browns)

AMD Ryzen 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 Z97 Rig (Decomissioned)

  • Intel i5-4690K 4.8 GHz
  • ASUS ROG Maximus VII Hero Z97
  • Sapphire Vapor-X HD 7950 EVGA GTX 1070 SC Black Edition ACX 3.0
  • 20 GB (8GB X 2 + 4GB X 1) Corsair Vengeance DDR3 1600 MHz
  • Corsair A50 air cooler  NZXT X61
  • Crucial MX500 1TB SSD + SanDisk Ultra II 240GB SSD + WD Caviar Black 1TB HDD + Kingston V300 120GB SSD [non-gimped version]
  • Antec New TruePower 550W EVGA G2 650W + White CableMod cables
  • Cooler Master HAF 912 White NZXT S340 Elite w/ white LED stips

AMD 990FX Rig (Decommissioned)

  • FX-8350 @ 4.8 / 4.9 GHz (given up on the 5.0 / 5.1 GHz attempt)
  • ASUS ROG Crosshair V Formula 990FX
  • 12 GB (4 GB X 3) G.Skill RipJawsX DDR3 @ 1866 MHz
  • Sapphire Vapor-X HD 7970 + Sapphire Dual-X HD 7970 in Crossfire  Sapphire NITRO R9-Fury in Crossfire *NONE*
  • Thermaltake Frio w/ Cooler Master JetFlo's in push-pull
  • Samsung 850 EVO 500GB SSD + Kingston V300 120GB SSD + WD Caviar Black 1TB HDD
  • Corsair TX850 (ver.1)
  • Cooler Master HAF 932

 

<> Electrical Engineer , B.Eng <>

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

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

Link to comment
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
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.

Intel Z390 Rig ( *NEW* Primary )

Intel X99 Rig (Officially Decommissioned, Dead CPU returned to Intel)

  • i7-8086K @ 5.1 GHz
  • Gigabyte Z390 Aorus Master
  • Sapphire NITRO+ RX 6800 XT S.E + EKwb Quantum Vector Full Cover Waterblock
  • 32GB G.Skill TridentZ DDR4-3000 CL14 @ DDR-3400 custom CL15 timings
  • SanDisk 480 GB SSD + 1TB Samsung 860 EVO +  500GB Samsung 980 + 1TB WD SN750
  • EVGA SuperNOVA 850W P2 + Red/White CableMod Cables
  • Lian-Li O11 Dynamic EVO XL
  • Ekwb Custom loop + 2x EKwb Quantum Surface P360M Radiators
  • Logitech G502 Proteus Spectrum + Corsair K70 (Red LED, anodized black, Cheery MX Browns)

AMD Ryzen 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 Z97 Rig (Decomissioned)

  • Intel i5-4690K 4.8 GHz
  • ASUS ROG Maximus VII Hero Z97
  • Sapphire Vapor-X HD 7950 EVGA GTX 1070 SC Black Edition ACX 3.0
  • 20 GB (8GB X 2 + 4GB X 1) Corsair Vengeance DDR3 1600 MHz
  • Corsair A50 air cooler  NZXT X61
  • Crucial MX500 1TB SSD + SanDisk Ultra II 240GB SSD + WD Caviar Black 1TB HDD + Kingston V300 120GB SSD [non-gimped version]
  • Antec New TruePower 550W EVGA G2 650W + White CableMod cables
  • Cooler Master HAF 912 White NZXT S340 Elite w/ white LED stips

AMD 990FX Rig (Decommissioned)

  • FX-8350 @ 4.8 / 4.9 GHz (given up on the 5.0 / 5.1 GHz attempt)
  • ASUS ROG Crosshair V Formula 990FX
  • 12 GB (4 GB X 3) G.Skill RipJawsX DDR3 @ 1866 MHz
  • Sapphire Vapor-X HD 7970 + Sapphire Dual-X HD 7970 in Crossfire  Sapphire NITRO R9-Fury in Crossfire *NONE*
  • Thermaltake Frio w/ Cooler Master JetFlo's in push-pull
  • Samsung 850 EVO 500GB SSD + Kingston V300 120GB SSD + WD Caviar Black 1TB HDD
  • Corsair TX850 (ver.1)
  • Cooler Master HAF 932

 

<> Electrical Engineer , B.Eng <>

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

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

Link to comment
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
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?

Intel Z390 Rig ( *NEW* Primary )

Intel X99 Rig (Officially Decommissioned, Dead CPU returned to Intel)

  • i7-8086K @ 5.1 GHz
  • Gigabyte Z390 Aorus Master
  • Sapphire NITRO+ RX 6800 XT S.E + EKwb Quantum Vector Full Cover Waterblock
  • 32GB G.Skill TridentZ DDR4-3000 CL14 @ DDR-3400 custom CL15 timings
  • SanDisk 480 GB SSD + 1TB Samsung 860 EVO +  500GB Samsung 980 + 1TB WD SN750
  • EVGA SuperNOVA 850W P2 + Red/White CableMod Cables
  • Lian-Li O11 Dynamic EVO XL
  • Ekwb Custom loop + 2x EKwb Quantum Surface P360M Radiators
  • Logitech G502 Proteus Spectrum + Corsair K70 (Red LED, anodized black, Cheery MX Browns)

AMD Ryzen 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 Z97 Rig (Decomissioned)

  • Intel i5-4690K 4.8 GHz
  • ASUS ROG Maximus VII Hero Z97
  • Sapphire Vapor-X HD 7950 EVGA GTX 1070 SC Black Edition ACX 3.0
  • 20 GB (8GB X 2 + 4GB X 1) Corsair Vengeance DDR3 1600 MHz
  • Corsair A50 air cooler  NZXT X61
  • Crucial MX500 1TB SSD + SanDisk Ultra II 240GB SSD + WD Caviar Black 1TB HDD + Kingston V300 120GB SSD [non-gimped version]
  • Antec New TruePower 550W EVGA G2 650W + White CableMod cables
  • Cooler Master HAF 912 White NZXT S340 Elite w/ white LED stips

AMD 990FX Rig (Decommissioned)

  • FX-8350 @ 4.8 / 4.9 GHz (given up on the 5.0 / 5.1 GHz attempt)
  • ASUS ROG Crosshair V Formula 990FX
  • 12 GB (4 GB X 3) G.Skill RipJawsX DDR3 @ 1866 MHz
  • Sapphire Vapor-X HD 7970 + Sapphire Dual-X HD 7970 in Crossfire  Sapphire NITRO R9-Fury in Crossfire *NONE*
  • Thermaltake Frio w/ Cooler Master JetFlo's in push-pull
  • Samsung 850 EVO 500GB SSD + Kingston V300 120GB SSD + WD Caviar Black 1TB HDD
  • Corsair TX850 (ver.1)
  • Cooler Master HAF 932

 

<> Electrical Engineer , B.Eng <>

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

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

Link to comment
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
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
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.

Intel Z390 Rig ( *NEW* Primary )

Intel X99 Rig (Officially Decommissioned, Dead CPU returned to Intel)

  • i7-8086K @ 5.1 GHz
  • Gigabyte Z390 Aorus Master
  • Sapphire NITRO+ RX 6800 XT S.E + EKwb Quantum Vector Full Cover Waterblock
  • 32GB G.Skill TridentZ DDR4-3000 CL14 @ DDR-3400 custom CL15 timings
  • SanDisk 480 GB SSD + 1TB Samsung 860 EVO +  500GB Samsung 980 + 1TB WD SN750
  • EVGA SuperNOVA 850W P2 + Red/White CableMod Cables
  • Lian-Li O11 Dynamic EVO XL
  • Ekwb Custom loop + 2x EKwb Quantum Surface P360M Radiators
  • Logitech G502 Proteus Spectrum + Corsair K70 (Red LED, anodized black, Cheery MX Browns)

AMD Ryzen 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 Z97 Rig (Decomissioned)

  • Intel i5-4690K 4.8 GHz
  • ASUS ROG Maximus VII Hero Z97
  • Sapphire Vapor-X HD 7950 EVGA GTX 1070 SC Black Edition ACX 3.0
  • 20 GB (8GB X 2 + 4GB X 1) Corsair Vengeance DDR3 1600 MHz
  • Corsair A50 air cooler  NZXT X61
  • Crucial MX500 1TB SSD + SanDisk Ultra II 240GB SSD + WD Caviar Black 1TB HDD + Kingston V300 120GB SSD [non-gimped version]
  • Antec New TruePower 550W EVGA G2 650W + White CableMod cables
  • Cooler Master HAF 912 White NZXT S340 Elite w/ white LED stips

AMD 990FX Rig (Decommissioned)

  • FX-8350 @ 4.8 / 4.9 GHz (given up on the 5.0 / 5.1 GHz attempt)
  • ASUS ROG Crosshair V Formula 990FX
  • 12 GB (4 GB X 3) G.Skill RipJawsX DDR3 @ 1866 MHz
  • Sapphire Vapor-X HD 7970 + Sapphire Dual-X HD 7970 in Crossfire  Sapphire NITRO R9-Fury in Crossfire *NONE*
  • Thermaltake Frio w/ Cooler Master JetFlo's in push-pull
  • Samsung 850 EVO 500GB SSD + Kingston V300 120GB SSD + WD Caviar Black 1TB HDD
  • Corsair TX850 (ver.1)
  • Cooler Master HAF 932

 

<> Electrical Engineer , B.Eng <>

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

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

Link to comment
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

×