📄 primzahl.java
字号:
// Primzahl.java // // input: natural number n// output: smallest prime number p with p > n//// method: apply prime test to n+1, n+2, ... until prime is found,// prime testing of z is done by testing numbers k = 2 ... with// k * k <= z whether they are factors of z//import java.awt.*;import java.applet.Applet;import java.awt.event.ActionListener;import java.awt.event.ActionEvent;public class Primzahl extends Applet { Label inputPrompt; // declare Label TextField input, output; // declare textfields for input and output int n, // the input integer read from user z, // candidate for the prime, set to n+1, n+2 etc. k; // possible divisor of z boolean divisorFound; // Boolean, indicates that a divisor // k of z has been found // setup the graphical user interface components // and initialize labels and text fields public void init() { // set layout setLayout(new FlowLayout( FlowLayout.LEFT )); // set Font Font myFont = new Font("Times", Font.PLAIN, 18); setFont( myFont ); setSize( 800, 100 ); inputPrompt = new Label("Geben Sie die Zahl n ein " + "und dr\u00fccken Sie Return."); input = new TextField( 10 ); input.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { // call method for finding next prime findPrime(); } }); // action will be on input field // output will be text in a field output = new TextField(60); output.setEditable(false); output.setBackground(Color.yellow); add(inputPrompt); // put prompt on applet add(input); // put input on applet add(output); // put output on applet } // init() // function for finding next prime number public void findPrime() { // get input number n = Integer.parseInt(input.getText()); z = n; do { z++; k = 2; divisorFound = false; // so far no divisor of z has been found while ((!divisorFound) && (k*k <= z)) { // check if k is a divisor of z if (z % k == 0) { divisorFound = true; } k++; } //end while } while (divisorFound); // define the output string String str = "Die n\u00e4chstgr\u00f6\u00dfere Primzahl nach " + n + " ist " + z + '.'; // put the sting into the output field output.setText( str ); } // findPrime()} // Primzahl
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -