使用MVC Java显示Prime数字

时间:2022-06-09 08:21:56

I have a Prime class which extends JFrame and it has a simple JSpinner for displaying prime numbers.

我有一个扩展JFrame的Prime类,它有一个简单的JSpinner来显示素数。

I want to create a Model for displaying prime numbers infinitely (until long ends). Here is the model class I have written:

我想创建一个无限显示素数的模型(直到长尾)。这是我写的模型类:

public class PrimeSpinnerModel extends AbstractSpinnerModel{

    long current;

    public PrimeSpinnerModel() {
        this.current = 2;
    }

    @Override
    public Object getValue() {
        return current;
    }

    @Override
    public Object getNextValue() {

        long newLatest = current + 1;

        if(isPrime(newLatest)){
            current = newLatest;
        }else{
            System.out.println(newLatest + "no prime");
            newLatest ++;
            current = newLatest;  
        }
        fireStateChanged(); 
        return getValue();
    }

    @Override
    public Object getPreviousValue() {

        fireStateChanged();
        return getValue(); // without this the component wouldn't know to update.
    }

    @Override
    public void setValue(Object value) {
        throw new IllegalArgumentException("Static spinner model Prime does not support editing.");
    }
    static boolean isPrime(long n) {
        if (n == 1) return false;

        for(long i = 2; i <= n/2; i++)
            if(n % i == 0)
                return false;

        return true;
    }
}

When I run the code, it displays prime numbers as 2,3,5,7,9,11,13 etc.

当我运行代码时,它显示素数为2,3,5,7,9,11,13等。

Why 9 is displayed?

为什么显示9?

2 个解决方案

#1


Suppose current is 7 when getNextValue() is invoked. Then newLatest is set to 8. isPrime(8) is obviously false, so then you increment newLatest, making it 9. You assign it to current and return it. This makes 9 the next number after seven, regardless of whether it is prime or not.

假设调用getNextValue()时current为7。然后newLatest被设置为8. isPrime(8)显然是假的,所以然后你增加newLatest,使它成为9.你将它分配给当前并返回它。这使得9成为7之后的下一个数字,无论它是否是素数。

To solve this, you should increment newLatest while it is not prime (in a loop). That way, you ensure you continue until you found a prime number. See below:

要解决这个问题,你应该增加newLatest而不是素数(在循环中)。这样,您确保继续,直到找到素数。见下文:

newLatest = current + 1;
while (!isPrime(newLatest)) {
    newLatest++;
}
// newLatest now contains the new prime number.
current = newLatest;
// etc..

#2


Try this :)

试试这个 :)

  public static void showPrimeNumbers(int n){
        for (int i=2; i<n; i++){
          boolean istrue = true;
          for (int x=2; x<i; x++){
            if  (i%x==0){
                istrue = false;
            }
           }

           if  (istrue==true){
               System.out.println(i);
            }
        }
    }

#1


Suppose current is 7 when getNextValue() is invoked. Then newLatest is set to 8. isPrime(8) is obviously false, so then you increment newLatest, making it 9. You assign it to current and return it. This makes 9 the next number after seven, regardless of whether it is prime or not.

假设调用getNextValue()时current为7。然后newLatest被设置为8. isPrime(8)显然是假的,所以然后你增加newLatest,使它成为9.你将它分配给当前并返回它。这使得9成为7之后的下一个数字,无论它是否是素数。

To solve this, you should increment newLatest while it is not prime (in a loop). That way, you ensure you continue until you found a prime number. See below:

要解决这个问题,你应该增加newLatest而不是素数(在循环中)。这样,您确保继续,直到找到素数。见下文:

newLatest = current + 1;
while (!isPrime(newLatest)) {
    newLatest++;
}
// newLatest now contains the new prime number.
current = newLatest;
// etc..

#2


Try this :)

试试这个 :)

  public static void showPrimeNumbers(int n){
        for (int i=2; i<n; i++){
          boolean istrue = true;
          for (int x=2; x<i; x++){
            if  (i%x==0){
                istrue = false;
            }
           }

           if  (istrue==true){
               System.out.println(i);
            }
        }
    }