Checking if something is equals to anything in an array

Printable View

• 03-18-2010, 08:30 PM
Sapster
Checking if something is equals to anything in an array
My code so far
import java.util.Random;

Code:

```class Test{                 public static void main(String[] args){                 Random Attack = new Random();                 int rud = 0;                 int rod[]={2,5,4,3,6};                 for(int counter=1; counter<=1; counter++){                         rud=Attack.nextInt(10);                                        }                 if(rud==rod[]){                         System.out.println("You hit");                         System.out.println(rud);                 }else{                         System.out.println("You missed");                         System.out.println(rud);                 }         } }```
I am trying to check if rud is equals to anything in rod but it doesnt work any idea on how to solve it ?
• 03-18-2010, 08:41 PM
PhHein
Major logic flaw. Do it with pencil and paper and I'm sure you'll see it.
• 03-18-2010, 09:56 PM
JosAH
Quote:

Originally Posted by Sapster
My code so far

Code:

`                if(rud==rod[]){`
I am trying to check if rud is equals to anything in rod but it doesnt work any idea on how to solve it ?

My crap-o-meter barfed on this line; when will an entire array ever equal one little single int value?

kind regards,

Jos

ps. my same crap-o-meter started to burp already on that useless loop a bit earlier in your code.
• 03-18-2010, 10:00 PM
PhHein
Your crap-o-meter is broken if it burps on crap!
• 03-18-2010, 10:05 PM
JosAH
Quote:

Originally Posted by PhHein
Your crap-o-meter is broken if it burps on crap!

Yes, it's a very old crap-o-meter; it doesn't even react to my own code ;-)

kind regards,

Jos *bwuck!*
• 03-19-2010, 01:26 AM
fceruti
IŽll write the solution for you, so you can check where you were wrong.

Code:

```class Test{                 public static void main(String[] args){                 Random Attack = new Random();                 int rud = 0;                 int rod[]={2,5,4,3,6};                       int nTries = 5;                 for(int counter=[B]0[/B]; counter<nTries; counter++){                         rud=Attack.nextInt(10);                                                                  if(rud==rod[counter]){                                     System.out.println("You hit");                                     System.out.println(rud);                           }else{                                     System.out.println("You missed");                                     System.out.println(rud);                           }                   }         } }```