Code:
public class NumerosPrimos
{
public static void main(String[] args)
{
int numeroatestar = 2;
int numeroadividir = 2;
boolean found;
while (true)
{
try {
Thread.sleep(500);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//Mostra o numero a testar
System.out.println("A testar numero: " + numeroatestar);
//Loop, vai dividindo ate que o numero pelo qual se esta a dividir iguale o que estamos a dividir
found = false;
while (numeroadividir < numeroatestar)
{
if (numeroatestar % numeroadividir == 0)
{
found = true;
break;
}
//se for divisivel o que fazer
numeroadividir++;
}
if(!found) // found == false
{
System.out.println(numeroatestar + " e um numero primo!");
}
numeroatestar++;
numeroadividir = 2;
}
}
}
It is a logical error, but I can not explain it in English. I have added the boolean 'found'.
Your Code:
4 % 3 == 1 -> Prime Numer
But 4 % 2 == 0 -> Not prime Number
Quote:
Originally Posted by Mikesch01
You have to look at the usage syntax for static variables in java:
You declaration was right but for usage you need the class name to access the right static variable. Example: MyClass.myStaticVariable
|
No, all right!
Das Problem im Code ist ein Logikfehler und kein Syntaxfehler. Man muss bei statischen Variablen nicht nicht Klasse dazu schreiben, wenn man aus der selben Klasse darauf zugreift.