首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

质数(java两个有关问题)

2012-03-26 
质数(java两个问题)importjava.awt.*importjava.awt.event.*importjava.applet.*importjava.io.*publi

质数(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){}
                                }


                                }
}

1、比如输入一个11,运行此程序在eclipse的控制台得到的结果为:3   5   5   5   7   7   7   7   7   9   11   11   11   11   11   11   11   11   11   ;
2、怎么让他在applet小程序中显示出来???

急!!!!


[解决办法]
if (i % j == 0)
break;
else if(i == j + 1)
System.out.print(i+ " ");
[解决办法]
http://community.csdn.net/Expert/topic/5477/5477418.xml?temp=.8386804
[解决办法]
1.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.io.*;

import javax.swing.JOptionPane;

public class AppletPrime extends Applet {
Label num = new Label( "请输入一个自然数: ");

TextField tfnum = new TextField(5);

Button show = new Button( "显示所有的质数: ");

TextArea txtprime = new TextArea();

public void init() {
setLayout(new FlowLayout());
add(num);
add(tfnum);
add(show);
add(txtprime);
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++) {
if (isPrimeNumber(i))// 如果是素数,则输出它
{
txtprime.append(String.valueOf(i)+ ", ");
}
}
}
} catch (Exception e1) {
}
}
}

public static boolean isPrimeNumber(int number) {
if (number < 2) {
return false;
}
for (int i = 2; i <= Math.sqrt(number); i++) {
if (number % i == 0 && number != 2) {
return false;
}
}
return true;
}
}
2.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN ">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME= "Generator " CONTENT= "EditPlus ">
<META NAME= "Author " CONTENT= " ">
<META NAME= "Keywords " CONTENT= " ">
<META NAME= "Description " CONTENT= " ">
</HEAD>

<BODY>
<APPLET CODE= "AppletPrime.class " WIDTH= "500 " HEIGHT= "500 ">
</APPLET>
</BODY>
</HTML>

热点排行
Bad Request.