- 1

Skipping 3

This code is supposed to take input, and print all the numbers from the input down to zero skipping numbers that are multiples of 3, but it doesn't print anything. import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner read = new Scanner(System.in); int number = read.nextInt(); //your code goes here for (int n = number; n==0; n--){ if (n % 3 != 0){ System.out.println(n); } } } }

25th Oct 2021, 2:33 PM
Natanael
13 odpowiedzi
+ 3
The mistake is inside the for loop condition: “n==0” so it will be executed when n is 0 and 0 % 3 is 0. Fix the condition to “n >= 0” to be executed on each number until 0
25th Oct 2021, 2:41 PM
Guillem Padilla
Guillem Padilla - avatar
+ 2
It doesn’t print 0 because 0 % 3 is 0 and you have an if checking this. You could modify your condition like If(n == 0 || n % 3 != 0) { Print… }
25th Oct 2021, 4:20 PM
Guillem Padilla
Guillem Padilla - avatar
+ 1
Thank you, I thought n==0 would mean repeat as long as n is not 0. I understand now that it's only looking for n to be 0 in this case
25th Oct 2021, 3:55 PM
Natanael
+ 1
Here's the code again, but I had to make more changes because it would not print the final number 0. So I added a second print statement that only prints 0 outside of the loop and now it works, I thought that this condition n>=0 meant that it would repeat as long as n is greater or equal to zero, with the last iteration it is equal to zero so it should print zero, but it only prints down to number 1 import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner read = new Scanner(System.in); int number = read.nextInt(); //your code goes here for (int n = number; n>=0; n--){ if (n % 3 != 0){ System.out.println(n); } } System.out.println(0); } }
25th Oct 2021, 4:18 PM
Natanael
+ 1
Thanks, the code works both ways, but I think the intention was for me to add the if statement you mentioned, this way it prints the final number zero, and all the other numbers that are not multiples of 3
25th Oct 2021, 4:35 PM
Natanael
+ 1
;) you can even simplify it more: If (!n || n%3) …
25th Oct 2021, 4:37 PM
Guillem Padilla
Guillem Padilla - avatar
+ 1
It’s a little bit tricky. “!” Is a negation prefix It converts the boolean evaluation of something into the opposite If you negate something false it becomes true, and something true becomes false If you evaluate 0 in a condition it is equals to false Any other value is true (forget about null) For example: Let n = 0 So if(n) { Print “hi” } else { Print “bye” } It will print bye because n = 0 and 0 means false. So if you use ! Prefix you get the opposite, it converts your false into a true and then it prints hi
25th Oct 2021, 5:27 PM
Guillem Padilla
Guillem Padilla - avatar
+ 1
Be aware: n != 0 is not the same as !n, but n == 0 is. You can read it as “if not n” Both are correct, advanced developers tends to use ! Prefix directly when don’t need to compare with a specific value
27th Oct 2021, 6:29 AM
Guillem Padilla
Guillem Padilla - avatar
+ 1
Ok thanks, this is good to know especially if you have to read code from some developers who prefer to use the abreviated method, I prefer whichever way is easier to understand
27th Oct 2021, 4:55 PM
Natanael
0
You’re correct now. For an easier understanding: The code only executes if the result of condition is true N>=0?? True, then the code executes. The same principle about conditions is applied on if, while, for…
25th Oct 2021, 4:09 PM
Guillem Padilla
Guillem Padilla - avatar
0
It's weird how n%3 would mean no multiples of 3, also !n for n not to be equal to 0 is pretty strange, so I'm guessing in a different code could have something like !3 to mean not equal to 3 or this rule only works with variables?
25th Oct 2021, 5:15 PM
Natanael
0
I meant n to be equal to 0
25th Oct 2021, 5:17 PM
Natanael
0
So if(n) actually means if n = true(any number other than 0) but because it is 0 it is false so it will print bye. Interesting but complicated, I prefer a simple style Like if n !=0 || n % 3 != 0 // not exactly part of the last code Instead of If (!n || n%3) …
27th Oct 2021, 12:54 AM
Natanael