Java 埃拉托什尼(质数查找器)
这个程序的目标是找到低于某个最大值的素数。Java 埃拉托什尼(质数查找器),java,arrays,methods,bluej,Java,Arrays,Methods,Bluej,这个程序的目标是找到低于某个最大值的素数。 cancelNonPrimes()方法的行为就像它正在工作一样(在分步调试程序中),但不会将假值写入列表 public class Era2 { int m; int i; boolean[] list; /** * Constructor for objects of class Era2 */ public Era2(int max) { m = max; list = new
cancelNonPrimes()
方法的行为就像它正在工作一样(在分步调试程序中),但不会将假值写入列表
public class Era2
{
int m;
int i;
boolean[] list;
/**
* Constructor for objects of class Era2
*/
public Era2(int max) {
m = max;
list = new boolean[m];
initialize();
}
/**
* Constructor for objects of class Era2 with standard-maximum-value = 25
*/
public Era2() {
m = 25;
list = new boolean[m];
initialize();
}
/**
* Method for initializing an object: all values to true except 0 and 1
*/
void initialize() {
while (i < m) {
setList(i, true);
i = i + 1;
}
setList(0, false);
setList(1, false);
}
/**
* Method for setting a bool to a number on the list
*/
void setList(int p, boolean b) {
list[p] = b;
}
/**
* Method for reading the entire list, printed in terminal
*/
void getList() {
for (int p = 0; p < m; p++){
if (readList(p) == true){
System.out.println(p + "true");
}
else {
System.out.println(p + "false");
}
}
}
/**
* Method for reading a specific shizl on the list
*/
boolean readList(int p) {
return list[p];
}
/**
* Method for setting all non-primes to false
*/
void cancelNonPrimes() {
i = 2;
while (i < m) {
if (readList(i) == true) {
int c = i;
int n = 2;
while (c < m) {
c = (i * n);
setList(c, false);
n = n + 1;
}
}
i = i + 1;
}
}
}
公共类Era2
{
int m;
int i;
布尔[]列表;
/**
*类Era2对象的构造函数
*/
公共Era2(整数最大值){
m=最大值;
列表=新布尔值[m];
初始化();
}
/**
*Era2类对象的构造函数,标准最大值=25
*/
公共资源2(){
m=25;
列表=新布尔值[m];
初始化();
}
/**
*初始化对象的方法:除0和1之外的所有值均为true
*/
void initialize(){
而(我
为了可读性,您应该删除==true
,并使用正确的缩进为什么要将2设置为false?另外,能否显示调用(/实例化)方法代码?(旁注:BlueJ不是Java的“翻版”,它是一个IDE。尽管有可怕的编辑器支持,但您确实在编写常规Java)在测试后在我这边工作(当设置为false时,c