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!"); } } } } } } } }
9 odpowiedzi
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
+ 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
+ 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
0
the problem is that it says there is no if connected to the else
0
and the x==x is my attempt at making the else connect
0
Thanks for the help anyway
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.
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!
0
@Okolocha Deborah now the error message is this/ usercode/public class App.java:21: error: class, interface, enum, or record expected
}
^
1 error