0

I don't understand why this isn't working. Please help

Can someone please tell me why the code isn't working for the problem it follows? Problem: A client wants you to write a program that prints all numbers from 0 to the inputted number that are multipliers of 3. Solution: 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 x = 3 while (x < nextInt) { System.out.println(x); x = x + 3; } } }

21st Dec 2020, 11:53 PM
Tommy Mensah
5 Answers
+ 2
Maybe Im too late to answer your doubt but I will still try to explain it. The main problem is the condition of "while loop" which is "x < number" is with the same parentheses as the "if condition" which are "(x % 10 == 3 || x % 3 ==0)". - - - - - - - - - - - In result of this, the code will only print 0 because the incremention or the "x = x + 1" only runs once For Example: x = 0 so here (x % 3 == 0) is True so the loop will be executed then the "x = x + 1" will be executed therefore the value of "x" will be x = 1. So now. x = 1 so here (x % 3 == 0 OR x % 10 ==3) is both False cause the value of x is now 1 and does not meet the condition. And because of this the "while loop" will never become True and will not be executed. - - - - - - - - - - - - I hope this helps, If my explanation is still not clear to you, feel free to ask. And again, update me if the problem is still not solved. Here is the Solution.👇 https://code.sololearn.com/cBB63F8jFs2e/?ref=app
22nd Dec 2020, 4:49 PM
noteve
noteve - avatar
+ 2
First, you forgot the semicolon on line 9. Second instead of "nextInt" in the while loop, use the variable "number" instead which holds/stores the input value. And last, you forgot to declare "int" to variable x. I hope this helps, if my explanation is not clear to you, feel free to ask. And update me if the problem is still not solved. Happy Coding! https://code.sololearn.com/ca13567a1227
22nd Dec 2020, 12:11 AM
noteve
noteve - avatar
+ 1
Thanks for the help @Nicko12, it worked. There however is a new twist to it : it has to print all numbers from 0 to the inputted number that are either a multiplier of 3 or end with 3. I believed the below should work but it isn't; what could be wrong with it. 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 int x = 0; while (x < number && (x % 10 == 3 || x % 3 == 0)) { System.out.println(x); x = x + 1 ; } } }
22nd Dec 2020, 2:04 PM
Tommy Mensah
+ 1
Thanks again 《 Nicko12 》 . Really helpful
28th Dec 2020, 7:46 PM
Tommy Mensah
+ 1
Try this. import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner read = new Scanner(System.in); int number = read.nextInt(); int x=0; while(x<=number) { if(x%10==3 || x%3==0){ System.out.println(x); } x++; } } }
25th Mar 2021, 8:47 AM
Sanjay Shaji
Sanjay Shaji - avatar