- 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); } } } }
13 Respostas
+ 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
+ 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…
}
+ 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
+ 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);
}
}
+ 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
+ 1
;) you can even simplify it more:
If (!n || n%3) …
+ 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
+ 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
+ 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
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…
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?
0
I meant n to be equal to 0
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) …