您好我试图制作一个基本程序,它会增加一个给定的数字,直到它找到下一个素数,但我有问题让它起作用
PrimeNumber(long eNumb)
{
number= eNumb;
}
public static void incrementNumber()
{
this.number = number + 1;
}
boolean foundNext = false;
PrimeNumber findNext = new PrimeNumber(next);
while (!foundNext)
{
if (PrimeNumber.isPrime())
{
foundNext = true;
}
else
{
FindNext.incrementNumber();
}
}
我使用了isPrime()方法,但我似乎正在进入无限循环。 对不起,如果问题不清楚 编辑:我输错了代码,但仍想回答。
答案 0 :(得分:0)
foundNext = true is always true, I think you have to make it false
any were in your program.
假设,
while (!foundNext) {
// foundNext = false; make it false on basis of any condtion
}
答案 1 :(得分:0)
问题liss:
while (!foundNext) // it schould check if foundNext is prime number
代码可以像这样简化:
PrimeNumber(long eNumb)
{
this.number= eNumb;
}
public void incrementNumber()
{
this.number++;
}
... // some code here. I assume that code below is inside some method.
PrimeNumber findNext = new PrimeNumber(next);
while (!findNext.isPrime()) // in tis solution I assume that number is not static field
{
findNext.incrementNumber();
}