Results 1 to 6 of 6
Thread: in need of help (easy)
- 12-30-2009, 07:03 PM #1
Member
- Join Date
- Dec 2009
- Posts
- 3
- Rep Power
- 0
in need of help (easy)
The problem is easy to solve, i cant figure it out myself tho...:D
i got a table with boolean, it got two vars p and q (p = false q = true)
I want to make the true and the false 0 and 1 instead of p and q
p and q is the var for the true false.
Anyways, i know it has something to do with casting, conversion or the if to get.
the conversion should be boolean to int right? kinda lost...
Help is greatly appreciated
- 12-30-2009, 08:00 PM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
You can't cast: boolean values don't have any intrinsic numerical value or vice versa.
"conversion" is an extremely vague term. Certainly you want to start with false and end up with zero, so you could say the false value is changed or converted. But this just begs the question: how?
You are left with using an if statement. Try that. If you get stuck, post your code and describe what goes wrong (compiler error or output that differs from what you want etc).
If you have studied the ternary ?:, it does something similar to if.
- 12-30-2009, 08:15 PM #3
Member
- Join Date
- Dec 2009
- Posts
- 3
- Rep Power
- 0
Heres the code
As you see on the first line i have tried with a if statement but i dont know how to connect it with the f = 0
Java Code:// Project 2-2: a truth table for the logical operators. class LogicalOpTable { public static void main(String args[]) { boolean p, q; int t, f; t = 1; f = 0; System.out.println("P\tQ\tAND\tOR\tXOR\tNOT"); [B]p = true; q = false;[/B] [B]if(p==true); f=0; [/B] System.out.print(p + "\t" + q +"\t"); System.out.print((p&q) + "\t" + (p|q) + "\t"); System.out.println((p^q) + "\t" + (!p)); p = true; q = false; System.out.print(p + "\t" + q +"\t"); System.out.print((p&q) + "\t" + (p|q) + "\t"); System.out.println((p^q) + "\t" + (!p)); p = false; q = true; System.out.print(p + "\t" + q +"\t"); System.out.print((p&q) + "\t" + (p|q) + "\t"); System.out.println((p^q) + "\t" + (!p)); p = false; q = false; System.out.print(p + "\t" + q +"\t"); System.out.print((p&q) + "\t" + (p|q) + "\t"); System.out.println((p^q) + "\t" + (!p)); } }Last edited by fasck; 12-30-2009 at 08:21 PM.
- 12-30-2009, 10:39 PM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
if(p==true); f=0;
Close, but using an if statement it would be
Java Code:if(p) { f = 0; } else { f = 1; }
Notice how if(p) is the same as if(p==true). And how the value of f has to be set for either option. (by the way, didn't you say you wanted zero, not one, for false?)
One way of achieving what you are trying to do would be to use a separate method to do the conversion.
Java Code:public class BooleanDisplay { public static void main(String[] args) { boolean p, q; System.out.println("P\tQ\tXOR"); p = true; q = false; System.out.println(asInt(p) + "\t" + asInt(q) + "\t" + asInt(p^q)); p = false; q = false; System.out.println(asInt(p) + "\t" + asInt(q) + "\t" + asInt(p^q)); } static int asInt(boolean b) { if(b) { return 1; } else { return 0; } // same thing //return b ? 1 : 0; } // not used, but it could be! static void display(boolean p, boolean q, boolean r) { System.out.println(asInt(p) + "\t" + asInt(q) + "\t" + asInt(r)); } }
- 12-30-2009, 10:43 PM #5
Member
- Join Date
- Dec 2009
- Posts
- 3
- Rep Power
- 0
Thanks alot mate =)
- 12-30-2009, 10:45 PM #6
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
Similar Threads
-
Easy task
By Srcee in forum New To JavaReplies: 4Last Post: 12-24-2009, 10:25 PM -
how easy it is?
By ron87 in forum New To JavaReplies: 0Last Post: 04-01-2009, 06:36 PM -
Not so easy is it.
By Roy Gardiner in forum IntroductionsReplies: 0Last Post: 10-24-2008, 04:59 PM -
What does this mean (Very Easy)
By Zebra in forum New To JavaReplies: 6Last Post: 05-01-2008, 01:46 PM -
Easy question
By JavaNoob in forum New To JavaReplies: 10Last Post: 08-03-2007, 10:28 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks