简体   繁体   English

java无法找到打印素数的错误

[英]java unable to find the error to print a prime number

This program gets 4 input from the user and prints out all the pairs except for the duplicate pairs. 该程序从用户获得4个输入并打印出除重复对之外的所有对。 I wanted it print only the pairs that sum equals a prime number. 我希望它只打印总和等于素数的对。 I did the entire program there is no compilation error but in the output it prints to all pairs except the duplicate pair(i want it to print only the pair whose sum= a prime number) can anyone tell me what i am doing wrong 我做了整个程序没有编译错误,但在输出中打印到除了重复对之外的所有对(我希望它只打印其总和=素数的对)任何人都可以告诉我我做错了什么

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Prime {
    public static List<Integer> numbers = new ArrayList<>(); // a list of integers that was accepted, can be acced in the order they were added
    public static Scanner input = new Scanner(System.in);
    public static void main(String args[])
    {
        int inputs = 4; 
        input(inputs);
        for(int i = 0; i < inputs; i++){ 
            for(int j = 0+i; j < inputs; j++){
                if(i != j) 
                    System.out.println(numbers.get(i)+ " + " + numbers.get(j) + " = "+ isPrime(numbers.get(i) + numbers.get(j)));                  
            }
        }   }   

    public static int isPrime (int sumPair)
    {
        boolean primeVal =true ;
        if(sumPair < 2)
            primeVal= false;            
        else if(sumPair ==2)
            primeVal=true;         
        else if (sumPair % 2 == 0)
            primeVal = false;
        else
        {
            int stop = (int)Math.sqrt(sumPair);
            for (int d = 3; d <= stop && primeVal; d = d + 2)
            {
                if (sumPair % d == 0)
                    primeVal = false;
            }
        }
        return(sumPair);
    }

    public static void input(int inputNumbers) 
    {
        while(numbers.size() < inputNumbers){ // while there is more inputs needed
            System.out.print("Enter a positive integer: ");
            int num = input.nextInt();
            if(num > 0) // if the input is valid
                numbers.add(num);
            else // if the input is not valid
                while (num <= 0) { // while the input is not valid
                    System.out.print("Enter a positive integer: ");
                    num = input.nextInt(); // get the next int if it wasn't valid
                    if(num > 0) { // if the input gets valid
                        numbers.add(num);
                    }
                }
            System.out.println("Thank you.");
        }
    }

    public static int sumPair(int num1, int num2)
    {    
        return num1 + num2;
    }

}

You go to a lot of trouble to compute a boolean primeVal which tells whether the input be true or false , but you never actually return this value. 你很难计算一个boolean primeVal来判断输入是true还是false ,但你实际上并没有真正返回这个值。 Try this instead: 试试这个:

public static boolean isPrime (int sumPair) {
    boolean primeVal = true;
    if (sumPair < 2 || sumPair % 2 == 0) {
        primeVal = false;            
    }      
    else {
        int stop = (int)Math.sqrt(sumPair);
        for (int d=3; d <= stop; d += 2) {
            if (sumPair % d == 0) {
                primeVal = false;
                break;
            }
        }
    }
    return primeVal;
}

Use this main() method: 使用此main()方法:

public static void main (String args[]) {
    int inputs = 4; 
    input(inputs);
    for (int i=0; i < inputs-1; i++) { 
        for (int j=i+1; j < inputs; j++) {
            boolean prime = isPrime(numbers.get(i) + numbers.get(j));
            if (prime) {
                System.out.println(numbers.get(i) + " + " + numbers.get(j));
            }                
        }
    }
}  

Your condition to determine if you print the current pair or not is if(i != j) , so it will only print if i is different from j (duplicate). 您确定是否打印当前对的条件是if(i != j) ,因此只有i与j(重复)不同时才会打印。

You made a nice isPrime function, you should call it. 你做了一个很好的isPrime函数,你应该调用它。

You made your isPrime function return an int, and I'm pretty sure it should return a boolean instead. 你让你的isPrime函数返回一个int,我很确定它应该返回一个布尔值。 Also, it returns the argument you gave it, so it does virtually nothing except spend time in a potentially expensive operation. 此外,它返回您给出的参数,因此除了花费在可能昂贵的操作上的时间之外,它几乎没有任何作用。 Perhaps should it return primeVal instead. 也许它应该返回primeVal。

Your code should look like: 您的代码应如下所示:

for(int i = 0; i < inputs; i++){ 
  for(int j = 0+i; j < inputs; j++){
    ni = numbers.get(i);
    nj = numbers.get(j);
    if(isPrime(ni+nj)){
      System.out.println(ni + " + " + nj + " = "+  (ni + nj));                  
    }
  }
}

What this does is get the numbers for indexes i and j and store them in ni and nj respectively. 这样做的是获取索引i和j的数字并分别将它们存储在ni和nj中。 Then, it checks it their sum is prime. 然后,它检查它们的总和是否为素数。 If it is, then it prints the sum. 如果是,则打印总和。

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

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