Results 1 to 5 of 5
- 04-19-2012, 09:23 AM #1
Member
- Join Date
- Apr 2012
- Posts
- 6
- Rep Power
- 0
Why isn't this snipet of code working correctly
I have a calculate max & calculate min class. The maximum class is working just fine, but the minimum one is always returning 0, even though 0 is not a value going through it. Is there something wrong?
Java Code:public static int calculateMin(int data[]){ int min = 50*50; for(int i = 0; i < data.length; ++i){ min = Math.min(min, data[i]); } // end for return min; } // end calculateMin
- 04-19-2012, 09:39 AM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: Why isn't this snipet of code working correctly
Print out the values in the data array as you go around that loop.
Please do not ask for code as refusal often offends.
- 04-19-2012, 09:42 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: Why isn't this snipet of code working correctly
Possibly there is a zero in the data array. You can check using System.out.println():
But without seeing some runnable code that's only a guess.Java Code:for(int i = 0; i < data.length; ++i){ if(data[i] == 0) { System.out.println("data[" + i + "] is zero"); min = Math.min(min, data[i]); } }
- 04-19-2012, 09:48 AM #4
Member
- Join Date
- Apr 2012
- Posts
- 6
- Rep Power
- 0
Re: Why isn't this snipet of code working correctly
Yeah sorry, my bad. The problem was elsewhere in my code but causing problems here. Thanks for your help
- 04-19-2012, 09:59 AM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
Help with If/Else not working correctly?
By kalaiss in forum New To JavaReplies: 8Last Post: 04-16-2012, 06:32 AM -
method not working correctly
By r1b in forum New To JavaReplies: 4Last Post: 01-04-2012, 08:09 PM -
Applet not working correctly?
By Beavotropper2 in forum Java AppletsReplies: 2Last Post: 04-18-2011, 06:32 AM -
Gueesing Game Almost done, but not working correctly
By mbnumba6 in forum New To JavaReplies: 5Last Post: 03-18-2009, 03:01 AM -
[SOLVED] \t not working correctly?
By Gakusei in forum New To JavaReplies: 5Last Post: 05-06-2008, 04:45 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks