简体   繁体   English

构造一个读取数字并表示其为质数或列出其因子的Java程序

[英]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): 目标是使最终结果看起来像这样(假设两个整数分别为6和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: 这是我输入6和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. 好吧,我只是简要浏览一下,但是您的问题出在for循环之内。 You start i at 2, which is sensible for checking for factors. 您从2开始我,这对于检查因素很明智。 You then check, simultaneously, if both num1 and num2 are evenly divisible by i (at this point, 2). 然后,您同时检查num1和num2是否均被i整除(此时为2)。 If they are, you print "Prime". 如果是这样,则打印“ 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 如果我不得不猜测,您在之后错过了else一行

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)) . 而且“ if”的条件也应该被否定: 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. 如果您使用google,可以找到数百种算法。 Find one and modify to your needs. 找到一个并修改您的需求。

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

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