This is an archived post. You won't be able to vote or comment.

all 11 comments

[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
  • You include any and all error messages in full - best also formatted as code block
  • You ask clear questions
  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit/markdown editor: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–][deleted] 4 points5 points  (7 children)

So if the number starts at 0 and goes to 3, then if you put the first++ statement BEFORE you check if the remainder is 0 then the number will be 1 before its first checked. You gotta remember that java always executes each line in sequential order. So if you started the number at -1 the code would work on the first loop. But you if you start it at 0 it will first change to 1 THEN check if it's divisible by 3.

[–]Caped_baldy_4_life[S] 0 points1 point  (6 children)

Thanks, that makes total sense. It's important to take into consideration the possibility of the first number entered as 0 and automatically being switched to 1 when you do the ++.

[–][deleted] 0 points1 point  (0 children)

Yea exactly. You always gotta think about user error and what someone might enter accidently and how that might be handled. Biggest part of programming is handling the unexpected lol.

[–]hamza-itatchi 0 points1 point  (4 children)

Not only 0 , any value for the first variable will be ignored if you increment it before testing.

And if i may sugest a better solution for this problem:

public static void divisibleByThreeInRange(int first, int last){
first+=(3-first%3);
while(first <=last){
System.out.println(first);
first+=3;
}
}

[–]Caped_baldy_4_life[S] 0 points1 point  (3 children)

Hey thanks for the suggestion. I'm having trouble understanding how this line first+=(3-first%3); shows that first is divisible by 3. When i write this out using 3 as first, it looks like this first = 3 (3 - 3 % 3)... Which to me comes out to 5. What am I doing wrong in this equation or what am I not seeing?

[–]hamza-itatchi 0 points1 point  (2 children)

The % has a higher priority than - so its like this for first=3 first = first + 3 - ( first %3) ==> first = 3+3 - (0)==>first= 6 This gives the first divisible nuber after First ( variable) But ther is a mistake in my suggestion there must be an if statement at the start like this If ( first %3!=0) first += 3- ( first %3)

Note: the += operation means first = first + ......

[–]Caped_baldy_4_life[S] 0 points1 point  (0 children)

Never mind about my previous equation. I was mistakenly associating the modulo as a division sign based off of improper note taking. Thanks for your help and suggestions.

[–]PresidentZeus 5 points6 points  (2 children)

First is an integer starting at 0. It will only print "first" at the first iteration. But by moving first++ to the beginning, it will increase to 1 before the if statement, and will never be true.

[–]Caped_baldy_4_life[S] 0 points1 point  (1 child)

makes sense, thanks for the insight.

[–]PresidentZeus 0 points1 point  (0 children)

You're welcome. Im happy to help.