Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By brynpttrsn

Thread: A little problem about a Nine-times Table

  1. #1
    blackdiz is offline Member
    Join Date
    Apr 2012
    Posts
    16
    Rep Power
    0

    Default A little problem about a Nine-times Table

    I try to make a nine-times 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' : ' '));
    		}
    	}
    }
    it works ok, but If I change the code:
    Java Code:
    i = (i == 9) ? ((++j / j) + 1) : (i + 1)
    to
    Java Code:
    i = (i == 9) ? ((j++ / j) + 1) : (i + 1)
    When i == 9 , it always makes 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

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default Re: A little problem about a Nine-times Table

    j++ is as same as ++j
    No
    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.

  3. #3
    blackdiz is offline Member
    Join Date
    Apr 2012
    Posts
    16
    Rep Power
    0

    Default Re: A little problem about a Nine-times 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 = 1
    Last edited by blackdiz; 05-04-2012 at 04:09 AM.

  4. #4
    brynpttrsn is offline Member
    Join Date
    Sep 2011
    Posts
    59
    Rep Power
    0

    Default Re: A little problem about a Nine-times Table

    Quote Originally Posted by blackdiz View Post
    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 = 1
    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
    1
    Last edited by brynpttrsn; 05-04-2012 at 04:39 AM.
    yellowledbet likes this.

  5. #5
    blackdiz is offline Member
    Join Date
    Apr 2012
    Posts
    16
    Rep Power
    0

    Default Re: A little problem about a Nine-times Table

    Thank you very much for your answering!
    Now I understand that.
    Thanks again!!
    Best Regards

Similar Threads

  1. Problem with ComboBox in a table
    By xyknight in forum New To Java
    Replies: 8
    Last Post: 04-25-2011, 03:05 AM
  2. Problem with table
    By fivig in forum SWT / JFace
    Replies: 0
    Last Post: 10-21-2009, 06:03 AM
  3. Replies: 3
    Last Post: 10-02-2008, 05:48 AM
  4. Replies: 2
    Last Post: 11-11-2007, 08:07 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
  •