I can't Debug It. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

I can't Debug It.

I can't Debug it https://www.sololearn.com/compiler-playground/cRJW3eaRZPNt or // Enter input in x // Bugged Code I Made From Visual Studio Code public class App { public static void main(String[] args) { int x=5; if (x % 2 = 1) { if (x % 3 = 1) { if (x % 5 = 1) { if (x % 7 = 1) { if (x % 9 = 1) { System.out.println("Prime!"); else if (x=x) { System.out.println("Not Prime!"); } } } } } } } }

28th Aug 2023, 3:54 PM
Darsh srivastava
Darsh srivastava - avatar
9 Answers
0
public class App { public static void main(String[] args) { int x = 5; if (x % 2 == 1) { if (x % 3 == 1) { if (x % 5 == 1) { if (x % 7 == 1) { if (x % 9 == 1) { System.out.println("Prime!"); } else { System.out.println("Not Prime!"); } } } } } } } This should work
30th Aug 2023, 1:10 PM
Okolocha Deborah
Okolocha Deborah - avatar
+ 5
You have to repeat the lession about if else statements: equals means == So the code looks like: if (x % 2 == 1) { …. etc. } Otherwise you cannot write if (x=x). From other hand if (x == x) has no sense, because it is always true. Here can you see more details about the prime numbers: https://en.wikipedia.org/wiki/Prime_number
28th Aug 2023, 4:38 PM
JaScript
JaScript - avatar
+ 3
What is the bug here? (single equal (=) is used to assign value not compare value) 5%2 = 1 but 5 % 3 != 1 so inner condition will not execute
28th Aug 2023, 4:35 PM
A͢J
A͢J - avatar
0
the problem is that it says there is no if connected to the else
30th Aug 2023, 2:41 AM
Darsh srivastava
Darsh srivastava - avatar
0
and the x==x is my attempt at making the else connect
30th Aug 2023, 2:42 AM
Darsh srivastava
Darsh srivastava - avatar
0
Thanks for the help anyway
30th Aug 2023, 2:44 AM
Darsh srivastava
Darsh srivastava - avatar
0
1. In your if statements, you need to use == instead of = to compare values. The single equal sign is used for assignment, while double equal signs are used for equality comparison. 2. The else if statement you used (else if (x = x)) doesn’t make sense in the context. Instead, I used an else statement to handle the case when the condition in the innermost if is not satisfied. With these changes, your code should work as expected and determine whether the given number x is prime.
30th Aug 2023, 1:09 PM
Okolocha Deborah
Okolocha Deborah - avatar
0
public class App { public static void main(String[] args) { int x = 5; if (x <= 1) { System.out.println("Not Prime!"); } else if (x <= 3) { System.out.println("Prime!"); } else if (x % 2 == 0 || x % 3 == 0) { System.out.println("Not Prime!"); } else { int i = 5; while (i * i <= x) { if (x % i == 0 || x % (i + 2) == 0) { System.out.println("Not Prime!"); return; } i += 6; } System.out.println("Prime!"); } } } Try this one. It will work!
30th Aug 2023, 2:25 PM
Budidha Dinesh Goud
Budidha Dinesh Goud - avatar
0
@Okolocha Deborah now the error message is this/ usercode/public class App.java:21: error: class, interface, enum, or record expected } ^ 1 error
30th Aug 2023, 4:05 PM
Darsh srivastava
Darsh srivastava - avatar