Results 1,081 to 1,093 of 1093
Thread: Quiz Time
- 01-16-2012, 03:53 AM #1081
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Re: Quiz Time
Shall we refresh this?
Is it able to compile the following code, and if so what is the output and why?
Java Code:int i = 10; int j = 10; boolean b = false; if(b = i == j) { System.out.println("True"); } else { System.out.println("False"); }
- 01-16-2012, 06:01 AM #1082
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
Re: Quiz Time
The Java compiler sometimes disallows things we might be tempted to write in C, and thereby delivers us from the evil thereof. It's not always possible though.
- 01-16-2012, 08:55 AM #1083
Member
- Join Date
- Aug 2011
- Location
- INDIA
- Posts
- 64
- Rep Power
- 0
Re: Quiz Time
answer is "yes"
I can and I will.gif)
- 01-16-2012, 01:20 PM #1084
Re: Quiz Time
I agree. == has higher precedence as is executed before =.
The Java Tutorial. Read it.
- 01-17-2012, 05:02 AM #1085
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Re: Quiz Time
Accepted.
Anyone else?
- 01-17-2012, 09:46 AM #1086
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
Re: Quiz Time
It's no different to the classic loop for reading files:
Java Code:String line; while ((line = reader.readLine()) != null) { }
- 01-23-2012, 08:22 AM #1087
Re: Quiz Time
It will compile and print "True" as the "==" will be performed first and then "=" giving a true for the condition .... as said its same as any of the checks we do in a for,while or if ..the operation done and the check executed.
_______________________________________________
give me beans .........
- 06-22-2012, 08:53 PM #1088
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Re: Quiz Time
I dusted off this thread; as you all probably know the Math.random() method returns a number in the range [0, 1) (the range includes 0), so for two numbers x and y in that range, the result x^y (^ denotes the power operator here) is in the range [0, 1] (including both 0 and 1); the number 1 is only reached iff y == 0. An observation: x^y > x for all values of x and y in that particular first range. Have a look at the following code snippet:
This little method calculates x^y0^y1^y2 ... until the result equals 1; theoreticaly that number is never reached if all y values are unequal to zero. Run the snippet and see for yourself that this program always terminates within 50 iterations or so for al values of y not equal to zero. Why?Java Code:public class T { public static void main(String[] args) { int count= 1; for (double x= Math.random(); x < 1; count++, x= Math.pow(x, Math.random())) System.out.println(count+": "+x); System.out.println(count); } }
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 06-23-2012, 07:57 AM #1089
- 06-23-2012, 08:22 AM #1090
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
- 06-23-2012, 08:32 AM #1091
- 06-23-2012, 08:35 AM #1092
Re: Quiz Time
Not important as far as the result is concerned, but your code doesn't show the value of y, only of x^y
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 06-23-2012, 08:55 AM #1093
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Re: Quiz Time
But it is important for the final result; you're right about not dispaying the value of y; here's the modified code:
kind regards,Java Code:public class T { public static void main(String[] args) { int count= 1; for (double x= Math.random(), y; x < 1; count++, x= Math.pow(x, y)) { y= Math.random(); System.out.println(count+": "+x+" "+y); } System.out.println(count); } }
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
Hello, first time here.
By ludragon in forum IntroductionsReplies: 2Last Post: 01-03-2008, 05:03 AM -
Help pls with a quiz
By saytri in forum New To JavaReplies: 3Last Post: 12-23-2007, 06:09 AM -
Time method
By carderne in forum New To JavaReplies: 5Last Post: 11-05-2007, 09:34 AM -
DataObject with the time given by me
By garinapavan in forum New To JavaReplies: 2Last Post: 08-07-2007, 06:33 PM


LinkBack URL
About LinkBacks
Reply With Quote


Bookmarks