Results 1 to 5 of 5
 05032012, 05:17 PM #1Member
 Join Date
 Apr 2012
 Posts
 16
 Rep Power
 0
A little problem about a Ninetimes Table
I try to make a ninetimes table program example on a book and here's the code.
Java Code:public class NineTable2 { public static void main(String[] args) { for (int i = 2, j = 1; j < 10; i = (i == 9) ? ((++j / j) + 1) : (i + 1)) { System.out.printf("%d*%d=%2d%c", i, j, i * j, (i == 9 ? '\n' : ' ')); } } }
Java Code:i = (i == 9) ? ((++j / j) + 1) : (i + 1)
Java Code:i = (i == 9) ? ((j++ / j) + 1) : (i + 1)
But does not j++ is as same as ++j?if ++j can make i = 2,so should j++,but it doesn't happen and i always equals 1.Can somebody explain it why
Sorry for my poor English, hope you can understand my question.
And thanks for replying.
Best Regard
 05032012, 05:22 PM #2
Re: A little problem about a Ninetimes Table
j++ is as same as ++j
The position of the ++ determines when the value of the variable is returned.
If it is before the variable the value returned is after the ++ is done, if after, the value is before the ++ is done.If you don't understand my response, don't ignore it, ask a question.
 05032012, 05:49 PM #3Member
 Join Date
 Apr 2012
 Posts
 16
 Rep Power
 0
Re: A little problem about a Ninetimes Table
First , Thank your for answering.
So if j = 1,then (++j/j) + 1 means j will add 2 first , then return value, so (++j/j) + 1 will run as ((1+1)/2) + 1 and equals 2,right?
And ((j++)/j) +1 means ((1+1)/1)+ 1 and equals 3,right?
But when I run the program with ((j++)/j) +1, I guess it runs (0/1)+1=1,so i is always 1, I don't know why ((j++/j)+1 = 1Last edited by blackdiz; 05042012 at 04:09 AM.
 05042012, 04:29 AM #4Member
 Join Date
 Sep 2011
 Posts
 59
 Rep Power
 0
Re: A little problem about a Ninetimes Table
Almost but not quite.
++j will add 1 to j before the result of j is returned.
j++ adds 1 to j after the result of j is returned
Therefore:
if j = 1
++j will return 2.
j++ will return 1 but will be 2 after.
Demonstration:
(++j/j)
if j = 1 this will translate to:
(2/2)
No matter what j is, this will always evaluate to 1
(j++/j)
if j = 1 this will translate to:
(1/2)
now this is the important part
at first glance this appears to evaluate to .5
BUT! The data type you are using is int therefore it will evaluate to 0!
Reason:
int cannot hold numbers with decimal places therefore truncates the decimals leaving the hole number(in this case 0).
Conclusion:
(++j/j) + 1
(2/2)+1
1+1
2
(j++/j) + 1
(1/2) + 1
0 + 1
1Last edited by brynpttrsn; 05042012 at 04:39 AM.
 05042012, 07:09 AM #5Member
 Join Date
 Apr 2012
 Posts
 16
 Rep Power
 0
Similar Threads

Problem with ComboBox in a table
By xyknight in forum New To JavaReplies: 8Last Post: 04252011, 03:05 AM 
Problem with table
By fivig in forum SWT / JFaceReplies: 0Last Post: 10212009, 06:03 AM 
SwingWorker problem!!! How can I run it 2 times or more?
By davigre in forum AWT / SwingReplies: 3Last Post: 10022008, 05:48 AM 
Problem calling classes to flip coin x number of times and record heads or tails
By adlb1300 in forum New To JavaReplies: 2Last Post: 11112007, 08:07 AM
Bookmarks