Prime Numbers

05/26/2015 23:50 kiko298#1
Hi, im new to java and my code isn't working. It was supose to give me "all" prime numbers but it doesnt :/

Code:
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class NumerosPrimos {

	static int numeroatestar = 2;
	static int numeroadividir = 2;
	static int resultado;
	
	
	public static void main(String[] args)
	{
		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 
			while(numeroadividir < numeroatestar)
			{
				if(numeroatestar % numeroadividir == 0)
				{
					//se for divisivel o que fazer
					numeroadividir++;
				}
				else
				{
					System.out.println(numeroatestar + " e um numero primo!");
					
			 
					numeroadividir++;
					
				}
			}
			
			numeroatestar++;
			numeroadividir = 2;
		}
	}
}
05/27/2015 06:24 Mikesch01#2
You have to look at the usage syntax for static variables in java: [Only registered and activated users can see links. Click Here To Register...]

You declaration was right but for usage you need the class name to access the right static variable. Example: MyClass.myStaticVariable
05/28/2015 16:53 ComputerBaer#3
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 View Post
You have to look at the usage syntax for static variables in java: [Only registered and activated users can see links. Click Here To Register...]

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.