📄 displayingprimenumbers.java
字号:
import javax.swing.JOptionPane;
public class DisplayingPrimeNumbers{
public static void main (String[] args){
StackOfIntegers stack = new StackOfIntegers();
String sa =JOptionPane.showInputDialog(null,"Enter the decimal number please!","Find the primes",JOptionPane.QUESTION_MESSAGE);
int a = Integer.parseInt(sa);
for (int i = 2;i<=a;i++){
int m = 1;
for(int j= i-1;j>= 2;j--){
if (i%j==0)
m=0;
}
if (m==1)
stack.push(i);
}
System.out.println("The prime numbers are:");
while(!stack.empty())
System.out.print (stack.pop()+" ");
System.out.println();
}
}
class StackOfIntegers{
private int[] elements;
private int size;
/**Construct a stack with the default capacity 16*/
public StackOfIntegers(){
this(16);
}
/**Construct a stack with the specific maximum capacity*/
public StackOfIntegers(int capacity){
elements = new int[capacity];
}
/**push a new integer into the top of the stack*/
public int push (int value){
if (size >= elements.length){
int[] temp = new int[elements.length*2];
System.arraycopy(elements,0,temp,0,elements.length);
elements = temp;
}
return elements[size++]=value;
}
/** return and remove the top element from the stack*/
public int pop(){
return elements[--size];
}
/**return the top element from the stack*/
public int peek(){
return elements[size-1];
}
/**test whether the stack is empty*/
public boolean empty(){
return size==0;
}
/**return the number of elements in the stack */
public int getSize(){
return size;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -