public class PrimeNumber1 {
public static void main(String args[]) {
int N = 100;
int p[]=new int[50];
p[0] = 2;// 放入第一個素數,有人說2不是素數,如果你是其中一員,就改成3吧
int nu = 1;
boolean bflag = true;
for(int i = 3;i <N; ++i)
{
bflag = true;
int iTemp = (int)Math.sqrt((float)i);
for (int j = 2; j <=iTemp; ++j)
{
if(i%j == 0)
{
bflag = false;
break;
}
}
if(bflag)
{
p[nu++] =(int)i;
}
}
for(int l=0;l<=nu-1;l++)
System.out.print(p[l]+" ");
}
}
import javax.swing.JOptionPane;
public class PrimeNumber1Pane
{
public static void main(String args[])
{
while (true)
{
String b = JOptionPane.showInputDialog("Enter figure that you want to beg prime number!");
int N = Integer.parseInt(b);
int p[] = new int[50];
PrimeNumber1Pane a = new PrimeNumber1Pane();
int nu = a.primeNumber(p, N);
for (int l = 0; l <= nu - 1; l++)
System.out.print(p[l] + " ");
int answer = JOptionPane.showConfirmDialog(null, "End program?", "Want to end?", JOptionPane.YES_NO_OPTION);
//字符串“End program?”可以被別的字符串取代,這將是出現在帶Yes/No按鈕的窗口中的字符口串,字符串“Want to end?”也可被取代,並將出現在窗口的標題中。
//JOptionPane.YES_NO_OPTION表示你需要一個帶Yes/No的窗口。
if (answer == JOptionPane.YES_OPTION)
System.exit(0);
else if (answer == JOptionPane.NO_OPTION)
System.out.println("/nOne more time");
}
//else
//System.out.println("This is impossible");
}
public int primeNumber(int p[],int N)//p爲放入素數的首地址,N爲要求素數的數,返回爲素數的個數
{
p[0] = 2;// 放入第一個素數,有人說2不是素數,如果你是其中一員,就改成3吧
int nu = 1;
boolean bflag = true;
for (int i = 3; i < N; ++i)
{
bflag = true;
int iTemp = (int)Math.sqrt((float)i);
for (int j = 2; j <= iTemp; ++j)
{
if (i % j == 0)
{
bflag = false;
break;
}
}
if (bflag)
{
p[nu++] = (int)i;
}
}
return nu;
}
}