Results 1 to 3 of 3
Thread: a small method
- 08-16-2010, 01:50 PM #1
Member
- Join Date
- Feb 2010
- Posts
- 36
- Rep Power
- 0
a small method
hey,
I've written a method that needs to return an object "Wagon".
The method needs to pick out the wagon with the biggest amount of weight and return this wagon.
But the problem is that its saying that "kar" is already defined.
I know I defined it in the beginning of the method but each time when I run through the forloop and there is a bigger amount af wait in the wagon "i", I want kar to be this wagon so that in the end the wagon with the most weight is going to be returned.
This is the code:
Java Code:public Wagon zwaarsteWagon(){ Wagon kar = new Wagon(); double gewicht= 0.0; for(int i=0; i<=getAantalWagons(); i++) if(wagons[i] != null){ if(wagons[i].getGewichtGoederen()>gewicht){ Wagon kar = wagons[i]; gewicht = wagons[i].getGewichtGoederen(); gewicht = wagons[i].getGewichtGoederen();}} return kar;}
- 08-16-2010, 01:57 PM #2
Java Code:public Wagon zwaarsteWagon(){ Wagon kar = new Wagon(); double gewicht= 0.0; for(int i=0; i<=getAantalWagons(); i++) if(wagons[i] != null){ if(wagons[i].getGewichtGoederen()>gewicht){ kar = wagons[i]; // <----------- just assign gewicht = wagons[i].getGewichtGoederen(); gewicht = wagons[i].getGewichtGoederen();}} return kar;}Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
- 08-16-2010, 06:38 PM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
That's because of these two lines.
You cannot use the same variable name in the same scope of it. It's a fundamental thing actually.Java Code:public Wagon zwaarsteWagon(){ Wagon kar = new Wagon(); ...... for(......) if(....){ if(...){ Wagon kar = wagons[i]; .... .......}} return kar;}
Similar Threads
-
small method isn't supposed to go under 0
By senca in forum New To JavaReplies: 2Last Post: 08-09-2010, 01:23 PM -
small problem
By rawan in forum Java AppletsReplies: 2Last Post: 04-07-2010, 03:32 AM -
small code, need help
By p0rnstar in forum New To JavaReplies: 22Last Post: 01-22-2010, 01:57 PM -
small problem
By barusk in forum NetworkingReplies: 4Last Post: 03-21-2009, 06:19 AM -
Small problem
By ayoood in forum New To JavaReplies: 2Last Post: 06-06-2008, 12:27 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks