简体   繁体   English

我不能列出我所有的质数。 怎么了? :S

[英]I can;t get all my prime numbers listed out. What's wrong? :S

What im trying to do: Write a java program that accepts some integers as a command line argument and;我想做什么: 编写一个接受一些整数作为命令行参数的 Java 程序; - display the number of integers passed - display smallest and largest number - display all the prime no in the list* - 显示传递的整数数 - 显示最小和最大数 - 显示列表中的所有质数 *

    import java.util.*;

    class commandLine
    {

        public static void main(String [] args)
        {
            int a[] =  new int[args.length];
            int count = 0;

            for (int i = 0; i<a.length;i++)
            {  
                 a[i] = Integer.parseInt(args[i]);
                count++;
            }
            System.out.println("Number of digits: " +count );

            int large = a[0];
            int small  = a[0];
            int num = 0;
            for (int j = 0; j<a.length; j++)
            {
                 num = a[j] ;
                 if(num>large)
                 {
                     large=num;
                 }
                 if(num<small)
                 {
                     small=num;
                 }
            }
            System.out.println("the largest is:" + large);
            System.out.println("Smallest no is : "  + small);   

            boolean isPrime = true; 
            for (int i = 2; i<a.length;i++){        
                 for (int j=2; j<i; j++)          
                    {
                            if((i % j)== 0)
                            {                                             
                                isPrime=false;     
                                break;                   
                            }  
                    }             

            if(isPrime)         
            {
                System.out.println(i + " is Prime");
            }
                isPrime = true;     
            }
        }
    }               

Your prime number check checks the numbers from 2 to a.length, whereas it should check the elements of the array a.您的质数检查检查从 2 到 a.length 的数字,而它应该检查数组 a 的元素。

 boolean isPrime; 
 for (int i : a) {     
    isPrime = true;   
    for (int j=2; j<i; j++) {
       if ((i % j)== 0) {                                             
          isPrime=false;     
          break;                   
       }  
     }             

     if(isPrime) {
         System.out.println(Integer.toString(i) + " is Prime");
     }
 }

This should help - disclaimer: I did not run this code.这应该会有所帮助 - 免责声明:我没有运行此代码。

Use a [i] instead of i in the primality check.在素性检查中使用[i]而不是i Easy mistake to make.容易犯错。

for (int i =0; i<a.length;i++){
    int tempInt = a[i];
         for (int j=2; j<tempInt; j++)          
         {
                    if((tempInt % j)== 0)
                    {                                             
                    isPrime=false;     
                    break;                   
                    }  
         }
         if(isPrime == true) System.out.println("Prime Number : " + tempInt);
         else isPrime = true;
}             

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

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