简体   繁体   中英

Constructing a java program that reads numbers and says it's prime or lists its factors

The goal of my specific project is to write a program that will prompt the user to input two integers. The program will read the two integers and decide whether they are prime or not. if they are not the program will list the factors, otherwise it will simply print "prime" and ask the user repeatedly for two integers. Also, the program should print factors of all the numbers between the two given integers as well as the integers themselves. It will also give the average value of the prime numbers.

Goal is to make the final result look like this (assuming the two integers are 6 and 11):

Please enter two integers: 6 11
6: 2 3
7: Prime
8: 2 4
9: 3
10: 2 5
11: Prime
There are three prime numbers
The average value of the prime numbers is 9.00

Please enter two integers:

So here is my code:

import java.util.Scanner;

public class Prime {

public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int r1, r2, i, c = 0;

System.out.println("Please enter two integers : ");
int num1 = input.nextInt();
int num2 = input.nextInt();
while (num1 > 0 && num2 > 0)
{

for (i = 2; i < num1; i++) {
r1 = num1 % i;
r2 = num2 % i;
if (r1 == 0 && r2 == 0)
    System.out.println("Prime");
{


System.out.println(i+ "\t");
c++;

}

}
if (c == 0)
System.out.println("Prime");
System.out.print("Please enter two integers : ");
num1 = input.nextInt();
num2 = input.nextInt();



}
}}

And this is my output when inputting 6 and 11:

Please enter two integers : 
6 11
2   
3   
4   
5   
Please enter two integers : 

Now i have no idea where i went wrong but i feel i must be heading somewhat in the right direction. If both inputs are prime it will print prime. If one is prime and one is not it will do what i posted above.

Any and all help is appreciated. Thank you.

Well, I'm just glancing over briefly, but your problem lies within the for loop. You start i at 2, which is sensible for checking for factors. You then check, simultaneously, if both num1 and num2 are evenly divisible by i (at this point, 2). If they are, you print "Prime". Then you iterate and do it again. Think about that carefully: how closely does it match with what you think/thought you were doing?

If I had to guess, you are missing an else line after

if (r1 == 0 && r2 == 0)
    System.out.println("Prime");
{

And also the condition for the "if" should probably be negated: if (!(r1 == 0 || r2 == 0)) . That should at the very least be enough to get you going in the right direction.

Good luck!

A much shorter way to do.

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {
    public static List<Integer> primeFactors(int numbers) {
        int n = numbers; 
        List<Integer> factors = new ArrayList<Integer>();
        for (int i = 2; i <= n / i; i++) {
            while (n % i == 0) {
                factors.add(i);
                n /= i;
            }
        }
        if (n > 1) {
            factors.add(n);
        }
        return factors;
    }

    public static void main(String[] args) 
    {
        System.out.println("Please enter two integers : ");
        Scanner input = new Scanner(System.in);
        int num1 = input.nextInt();
        int num2 = input.nextInt();
        List<Integer> primenos = new ArrayList<Integer>();
        List<Integer> result;
        for(int i=num1; i<=num2; i++)
        {
            result = primeFactors(i);
            System.out.print(i +":");
            if(result.size()==1 && result.get(0)==i)
            {
                System.out.println(" prime");
                primenos.add(i);
            }
            else
            {
                for (Integer j : result) {
                    System.out.print(" "+j);
                }
            }
            System.out.println();
        }
        System.out.println("There are "+primenos.size()+" prime numbers");
        int total = 0;
        for(Integer j : primenos)
        {
            total+=j;
        }
        System.out.println("The average value of the prime numbers is "+total/primenos.size());
    }
}

This is just one way of doing it. You could find hundreds of algorithms if you google. Find one and modify to your needs.

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