• Post Reply Bookmark Topic Watch Topic
  • New Topic

Finding a value in array question  RSS feed

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone (or anyone who reads this )

In my intro to java class, I have this program that is giving me a slight problem. I have assigned random numbers to an 2D array, and I had to write a method that prompts the user for a number between 100 and 200. If that number is in the array, it should return 1. If not, 0. It only stays 0 though, even when the number matches one of the numbers in the array.

Thanks!

 
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
first, you should fix that wretched formatting. Your code is very hard to read.

Next, what have you tried to do to find the problem? A few simple things you can do:

Use a smaller array. You don't say what size you have, but maybe start with a 1x1 array.

liberally sprinkle System.out.println statements into your code. You can always remove them later, but print out what you are ACTUALLY doing. "i found this value"..."i'm entering THIS loop"..."comparing <whateve> to <guess>".

 
Brandon Cahrenger
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah sorry, it's still in rough draft mode.

I'm trying to find one number that is guessed by the user in the 4x4 arrays. The random numbers filled the elements.
I'll make it back when I get the chance after work, thanks
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks like you break out of the inner loop straight away regardless of whether the element is a match or not (break statement in both branches), and you never break out of the outer loop.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!