简体   繁体   English

逻辑错误导致可能相对于for循环丢失/错误输出

[英]Logic error resulting in missing/incorrect output possibly relative to for-loop

The PrimeDetector class shown below has documentation which describes the uses of each method. 下面显示的PrimeDetector类具有描述每种方法用法的文档。 The tester class creates an object of the PrimeDetector class and attempts to print a set of generated integer values from an ArrayList returned from the hasPrime() method. tester类创建PrimeDetector类的对象,并尝试从hasPrime()方法返回的ArrayList中打印一组生成的整数值。 I do not see my error, as I have created a procedural version of it which works just fine. 我没有看到我的错误,因为我已经创建了一个程序版本,它工作正常。 It prints only the first 3 prime numbers, and returns as having only detected 3 prime numbers--leading me to believe that the issue lies within the PrimeDetector class, somewhere within the for-loop, though I can't be certain, as it is nearly the same structure as my procedural version--so far as I can tell. 它只打印前3个素数,并返回为只检测到3个素数 - 让我相信问题在于PrimeDetector类,在for循环中的某个地方,虽然我不能确定,因为它与我的程序版本几乎相同的结构 - 据我所知。 I will include the single procedural class, as well as the OOP version and its tester. 我将包括单个过程类,以及OOP版本及其测试程序。

/**
 * The PrimeDetector class detects prime numbers within a user's
 * given set [0,n] where n is a user-given upper limit.
 * 
 * @author A. Mackey
 * @version 06/05/14
 */
import java.util.*;
public class PrimeDetector {
    private int n;
    private int primeCounter;
    private ArrayList<Integer> primeList = new ArrayList<Integer>();

    /**
     * Constructor for objects of class PrimeDetector
     * @param n is the upper limit in the set [0,n] tested with the hasPrime() method.
     */
    public PrimeDetector(int n) {
        this.n = n;
    }

    /**
     * @return an ArrayList of type Integer containing all prime values within the set [0,n].
     */
    public ArrayList<Integer> hasPrime() {
        primeCounter = 0;
        for (int i = 1; i <= n; i++) {
            boolean isPrime = true;
            for (int j = 2; j <= i / 2; j++) {
                if (i % j == 0) {
                    isPrime = false;
                    break;
                }
            }
            if (isPrime) {
                primeCounter++;
                primeList.add(i);
            } else {
                break;
            }
        }
        return primeList;
    }

    /**
     * @return primeCounter variable which holds and integer value equivalent to the number of prime values in
     * the [0,n] set evaluated in the hasPrime() method.
     */
    public int getPrimeCounter() {
        return primeCounter;
    }
}

Tester: 测试:

/**
 * The PrimeDetectorTest class tests the PrimeDetector class, which detects prime numbers within a user's
 * given set [0,n] where n = a user-given upper limit.
 * 
 * @author A. Mackey
 * @version 06/05/14
 */
import java.util.*;
public class PrimeDetectorTest
{
    public static void main(String [] args)
    {
        Scanner in = new Scanner(System.in);

        System.out.print("Enter a positive integer you wish to find primes up to: ");
        int n = in.nextInt();
        System.out.println("The following list is prime within the range [0, " + n + "]: ");
        PrimeDetector list = new PrimeDetector(n);

        ArrayList<Integer> primeList = list.hasPrime();

        for (int s : primeList)
        {
            System.out.println(s + " is prime.");
        }

        System.out.println(list.getPrimeCounter() + " prime numbers within this set.");
    }
}

Procedural version: 程序版本:

import java.util.*;
public class PrimeDetectorV1
{
    public static void main(String [] args)
    {
        Scanner in = new Scanner(System.in);

        System.out.print("Enter a positive integer you wish to find primes up to: ");
        int n = in.nextInt();
        int primeCounter = 0;
        System.out.println("The following list is prime within the range [0, " + n + "]: ");

        for(int i = 0; i <= n; i++)
        {
            while (i>0)
            {
                boolean isPrime = true;
                for (int j = 2; j <= i/2; j++)
                {
                    if(i % j == 0)
                    {
                        isPrime = false;
                        break;
                    }
                }
                if (isPrime)
                {
                    System.out.println(i + " is prime.");
                    primeCounter++;
                    break;
                }
                else
                {
                    break;
                }
            }
        }
        System.out.println("There are " + primeCounter + " prime numbers within this set.");
    }
}
  if (isPrime) {
      primeCounter++;
      primeList.add(i);
  } else {
       break;
  }

Remove the else{break;} here. 在这里删除else{break;} Because if you found a number that is not prime, you still need to check the next one. 因为如果你发现一个不是素数的数字,你仍然需要检查下一个。 Currently your program will stop when it founds a number that is not prime. 目前你的程序会在找到一个不是素数的数字时停止。

Also 1 is not a prime number so you should start your first loop at 2. 1也不是素数,所以你应该在2开始你的第一个循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM