Results 1 to 6 of 6
  1. #1
    Sapster is offline Member
    Join Date
    Mar 2010
    Posts
    1
    Rep Power
    0

    Default Checking if something is equals to anything in an array

    My code so far
    import java.util.Random;

    Java 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 ?

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    10

    Default

    Major logic flaw. Do it with pencil and paper and I'm sure you'll see it.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,421
    Blog Entries
    7
    Rep Power
    26

    Default

    Quote Originally Posted by Sapster View Post
    My code so far

    Java 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.

  4. #4
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    10

    Default

    Your crap-o-meter is broken if it burps on crap!
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,421
    Blog Entries
    7
    Rep Power
    26

    Default

    Quote Originally Posted by PhHein View Post
    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!*

  6. #6
    fceruti is offline Member
    Join Date
    Mar 2010
    Posts
    20
    Rep Power
    0

    Default

    IŽll write the solution for you, so you can check where you were wrong.

    Java 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);
    		           }
                       }
    	}
    
    }

Similar Threads

  1. == and equals()
    By arefeh in forum New To Java
    Replies: 13
    Last Post: 01-05-2010, 05:56 PM
  2. == is same as .equals()??
    By DrMath in forum New To Java
    Replies: 1
    Last Post: 09-30-2009, 05:57 AM
  3. equals method
    By mani_miit in forum Advanced Java
    Replies: 7
    Last Post: 09-09-2009, 11:26 PM
  4. a little problem with (equals)
    By gamilah in forum New To Java
    Replies: 5
    Last Post: 10-25-2008, 05:08 AM
  5. Replies: 0
    Last Post: 01-26-2008, 12:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •