简体   繁体   中英

What is Wrong with my code to find prime numbers?

For this assignment I need to take in a serious of inputs until 0 is entered which will terminate the code. After the input of each number it needs to output whether that number was prime or not. Then after inputting zero the code should stop and give some description of the numbers entered such as max, min, sum, etc.

My issue is that regardless of what number I put in, it says that every number is prime and I cannot seem to understand why. Any help would be greatly appreciated!

import java.util.Scanner;

public class Assignment4 
{
public static void main (String[] args) 
{
    int input;
    int max = 0;
    int min = 100000000;
    double sum = 0;
    int count= 0;
    double average = 0;
    Scanner scan = new Scanner(System.in);
    boolean isPrime;

    do 
    {
        System.out.println("Enter a postive integer. Enter 0 to quit");
        input = scan.nextInt();
        for (int i =2; i<= input/2; i++) //Determine if prime
        {
            if (input % i == 0)
                isPrime = false;
            else
                isPrime = true;

            if (isPrime = true)
                System.out.println("The number " + input + " is a prime number");
            else
                System.out.println("The number " + input + " is not a prime number");

            if (input > max)
            {
                max = input;
            }
            if (input < min)
            {
                min = input;
            }
            sum += input;
            count = count + 1;
            average = sum/count;
            break;
        }

    }
    while (input != 0);
    System.out.println("The Max is " + max);
    System.out.println("The Min is " + min);
    System.out.println("The sum is " + sum);
    System.out.println("The total numbers are " + count);
    System.out.println("The average is " + average);


}   

}

You are setting isPrime to true if i is not a divisor. If the last i you check is not a divisor, isPrime will be false even though there might have been a divisor in a previous iteration.

Solution: Remove the assignment to false and set isPrime to true before starting the for loop:

import java.util.Scanner;

public class Assignment4  {
    public static void main (String[] args)  {
        int input;
        int max = 0;
        int min = 100000000;
        double sum = 0;
        int count= 0;
        double average = 0;
        Scanner scan = new Scanner(System.in);
        boolean isPrime;

        do 
        {
            System.out.println("Enter a postive integer. Enter 0 to quit");
            input = scan.nextInt();
            isPrime = true; // I added this line
            for (int i =2; i<= input/2; i++) //Determine if prime
            {
                if (input % i == 0) {
                    isPrime = false;
                    break;
                } 
                //else
                //    isPrime = true;
            } 
            if (isPrime)
                System.out.println("The number " + input + " is a prime number");
            else
                System.out.println("The number " + input + " is not a prime number");

            if (input > max)
            {
                max = input;
            }
            if (input < min)
            {
                min = input;
            }
            sum += input;
            count = count + 1;
            average = sum/count;

        }
        while (input != 0);
        System.out.println("The Max is " + max);
        System.out.println("The Min is " + min);
        System.out.println("The sum is " + sum);
        System.out.println("The total numbers are " + count);
        System.out.println("The average is " + average);


    }   
}

Code untested, I am on my phone currently

Fun fact:
You just need to loop to Math.sqrt(input) (square root). To illustrate it: If input/2 is a divisor, 2 is a divisor too ;)

I can see a few problems:

1) Change:

if (input % i == 0)
            isPrime = false;
        else
            isPrime = true;

To:

if (input % i == 0)
    isPrime = false;

And insert

isPrime=true;

before the for loop.

(Because just because the current value of i is not a divisor does not make it prime).

2) Change:

if (isPrime = true)

To:

if (isPrime==true)

Or:

if (isPrime)

3) Delete break;

4) Check if isPrime is true outside the loop, after all the divisors have been checked, not in it.

5) Check values of i up to and including the square root of input, not half of it. It's quicker.

David Sherret is correct, you have an error in that if statement. It needs to change to if( isPrime ) .

Also, you are printing the results of every input % i test. That output should happen after the for loop.

Right now, when testing 15, your code will print out: The number 15 is a prime number The number 15 is not a prime number The number 15 is a prime number The number 15 is not a prime number The number 15 is a prime number The number 15 is a prime number

This is because:

(15 % 2) equals 1
(15 % 3) equals 0
(15 % 4) equals 3
(15 % 5) equals 0
(15 % 6) equals 3
(15 % 7) equals 1

and you are printing the results for each of these tests.

Before the for loop, initialize isPrime with true and then only set it to false inside the loop if the number is not prime. Then, test isPrime after the loop and print the statement. Like this:

isPrime = true;
for (int i =2; i<= input/2; i++) //Determine if prime
{
    if (input % i == 0)
        isPrime = false;

    if (input > max)
    {
        max = input;
    }
    if (input < min)
    {
        min = input;
    }
    sum += input;
    count = count + 1;
    average = sum/count;
    break;
}

if (isPrime)
    System.out.println("The number " + input + " is a prime number");
else
    System.out.println("The number " + input + " is not a prime number");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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