Results 1 to 6 of 6
  1. #1
    aconti is offline Member
    Join Date
    May 2011
    Posts
    39
    Rep Power
    0

    Default Logical Operator query

    Hello,

    why does the below work correctly only with the || operator as I thought that I should use the && operator

    Java Code:
    import java.util.Random;
    
    
    public class Dice {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		
    		int dice1;
    		int dice2;
    		int count;
    		int totalcount = 0;
    		
    		
    		do{
    			Random rand = new Random();
    			
    			dice1 = rand.nextInt(6) + 1;
    			
    			dice2 = rand.nextInt(6) + 1;
    			
    			System.out.println("Dice1 " + dice1);
    			System.out.println("Dice2 " + dice2);
    			
    			count = dice1 + dice2;
    			
    			totalcount = totalcount + 1;
    			
    			
    			
    			System.out.println("Count for this throw is " + count);
    			
    			
    			
    		[B]}while(dice1 != 1 || dice2 != 1);[/B]
    		
    		System.out.println("");
    		System.out.println("");
    		
    		System.out.println("You have obtained the Snake Eyes by doing a total count of " + totalcount);
    			
    		
    			
    	
    		
    
    		
    		
    		
    		
    		
    	}
    
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,463
    Rep Power
    25

    Default

    why does the below work correctly only with the || operator as I thought that I should use the && operator
    Java Code:
    (dice1 != 1 || dice2 != 1)
    Define what "work correctly" means?
    Please explain what the conditions are that you want the condition to be true?
    Make a truth table listing all the values for the two die with the desired results.

  3. #3
    aconti is offline Member
    Join Date
    May 2011
    Posts
    39
    Rep Power
    0

    Default

    Hi Sorry I just want the do while loop to stop if both dices numbers are 1 only

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    It seems the code works. So what is your problem?

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,254
    Rep Power
    19

    Default

    Quote Originally Posted by aconti View Post
    Hi Sorry I just want the do while loop to stop if both dices numbers are 1 only
    So you do want the loop to continue as long as either of the dice is not 1.

    db

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,016
    Rep Power
    20

    Default

    Or, put another way, you want to exit the loop when both dice equal 1:
    dice1 == 1 && dice 2 == 1
    which means to continue the loop you want that to be false:
    !(dice1 == 1 && dice 2 == 1)
    which is, of course, the logical equivalent to the OR one you have in your functioning code.

Similar Threads

  1. Logical inheritance issue...
    By senca in forum New To Java
    Replies: 2
    Last Post: 06-14-2011, 11:00 AM
  2. if block with logical or
    By Ranu in forum New To Java
    Replies: 6
    Last Post: 07-01-2010, 08:11 AM
  3. Some Logical problem
    By MuslimCoder in forum New To Java
    Replies: 4
    Last Post: 03-01-2010, 08:12 AM
  4. Logical Gates
    By lingz89 in forum New To Java
    Replies: 1
    Last Post: 08-17-2009, 01:11 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
  •