I need help to complete the tasks: | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

I need help to complete the tasks:

Do While Loops Write a program that takes N numbers as input and outputs the numbers from N to 0, skipping the ones that are multiple of 3. Sample Input 7 Sample Output 7 5 4 2 1 0 My code: import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner read = new Scanner(System.in); int number = read.nextInt(); // N while(number > 0) { if (number == 3){ continue; } System.out.println(number); number = number - 1; } System.out.println("0"); } } I can manage a series of numbers that is getting smaller and smaller, but not omitting the 3 and so on. What can I do?

23rd Aug 2021, 1:07 PM
Lisa Kraack
Lisa Kraack - avatar
3 Answers
+ 2
You have written the wrong condition to check multiple of 3 Your code checks if the number is equal to 3 then skip it. Change the condition If(number%3==0) { continue; }
23rd Aug 2021, 1:11 PM
Edwin
Edwin - avatar
+ 2
Lisa Kraack , i have my doubts if you have tested the code that you have posted here - right? if the loop gets the first number that is divisible by 3, the continue statement will be executed, and then start with the next loop cycle. but as number is decremented below the continue statement, it will run in an infinite loop there.
23rd Aug 2021, 3:12 PM
Lothar
Lothar - avatar
0
Would this be the right code? import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner read = new Scanner(System.in); int number = read.nextInt(); // N while(number > 0) { if(number%3==0) { continue; } System.out.println(number); number = number - 1; } System.out.println("0"); } }
23rd Aug 2021, 1:15 PM
Lisa Kraack
Lisa Kraack - avatar