日期:2014-05-20  浏览次数:20747 次

质数(java两个问题)
import   java.awt.*;
import   java.awt.event.*;
import   java.applet.*;
import   java.io.*;
  public   class   AppletPrime   extends   Applet
{
        Label   num=new   Label( "请输入一个自然数: ");
        TextField   tfnum=new   TextField(5);
       
        Button   show   =   new   Button   ( "显示所有的质数: ");
             
        public   void   init(){
                setLayout(new   FlowLayout());
                add(num);
                add(tfnum);
                add(show);
                show.addActionListener(new   ShowActionAdapter());
                }
        class   ShowActionAdapter   implements   ActionListener
                {
                          public   void     actionPerformed(ActionEvent   e)
                          {
                              try{
                          String   s1=tfnum.getText();
                                int   d=Integer.parseInt(s1);
       
                                {for(int   i=2;i <=d;i++)
                                {
                                for(int   j=2;j <i;j++)
                                {
                                if(i%j!=0)
                                System.out.print(i+ "   ");
                                else
                                break;
                                }
                                }
                                }
                                }catch(Exception   e1){}
                                }