Results 1 to 6 of 6
Thread: Logical Operator query
- 08-18-2011, 10:16 PM #1
Member
- Join Date
- May 2011
- Posts
- 39
- Rep Power
- 0
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); } }
- 08-18-2011, 11:29 PM #2
why does the below work correctly only with the || operator as I thought that I should use the && operatorDefine what "work correctly" means?Java Code:(dice1 != 1 || dice2 != 1)
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.
- 08-19-2011, 06:54 AM #3
Member
- Join Date
- May 2011
- Posts
- 39
- Rep Power
- 0
Hi Sorry I just want the do while loop to stop if both dices numbers are 1 only
- 08-19-2011, 07:01 AM #4
It seems the code works. So what is your problem?
- 08-19-2011, 07:37 AM #5
- 08-19-2011, 10:38 AM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
Similar Threads
-
Logical inheritance issue...
By senca in forum New To JavaReplies: 2Last Post: 06-14-2011, 11:00 AM -
if block with logical or
By Ranu in forum New To JavaReplies: 6Last Post: 07-01-2010, 08:11 AM -
Some Logical problem
By MuslimCoder in forum New To JavaReplies: 4Last Post: 03-01-2010, 08:12 AM -
Logical Gates
By lingz89 in forum New To JavaReplies: 1Last Post: 08-17-2009, 01:11 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks