Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将主生成器存储到阵列中_Java_Arrays_Loops - Fatal编程技术网

Java 将主生成器存储到阵列中

Java 将主生成器存储到阵列中,java,arrays,loops,Java,Arrays,Loops,我现在正在学习阵列 我做了一个简短的小节目。这是一个测试然后打印素数的循环。 特别是1到100之间的数字 我想知道我是否可以进行循环并将每个素数存储到一个数组中。这是我的密码: public class QC3PrimeNumbers { public static void main (String[] args) { System.out.println ("Here are the prime numbers: "); for (int inde

我现在正在学习阵列

我做了一个简短的小节目。这是一个测试然后打印素数的循环。 特别是1到100之间的数字

我想知道我是否可以进行循环并将每个素数存储到一个数组中。这是我的密码:

public class QC3PrimeNumbers
{

    public static void main (String[] args)
   {
      System.out.println ("Here are the prime numbers: ");

       for (int index = 2; index < 100; index++)
      {
         if (index%2 != 0 && index%3 !=0)
         System.out.print (index + " ");         
      }

   }
}

如果使用此条件:如果索引%2!=0&索引%3=0,那么它永远不会考虑2个或3个素数。如果您将其更改为:

 if (index%2 != 0 && index%3 !=0 && index%5 !=0 && index%7 !=0)
它不会发现2、3、5或7是素数

我修正了你的算法,并决定使用一个存储素数列表,因为它的大小是动态的。否则,我们首先要找到你所在区域的素数来确定数组的大小,然后再做另一个同样的循环,除了这次将这些数添加到数组中

一旦你找到一个素数。使用ArrayList.addprimeNumberFound;的名称

_________________________________________________________________________________

使用ArrayList执行此操作 _________________________________________________________________________________

使用常规数组执行此操作 以下是如何使用不推荐使用的常规阵列执行此操作,但是:

public class QC3PrimeNumbers
{

    public static void main (String[] args)
    {
        System.out.println ("Here are the prime numbers: ");

        int numOfPrimes = 0; // counts the # of prime nums in your region

        // this loop will count up the number of prime numbers
        for (int index = 2; index < 100; index++)
        {
            boolean isPrime = true; // initially true, reset this every loop

            // for every number that can factor into the index number
            for (int i = 2; i < index; i++)
                // if a number is found that factors into it, it's not prime
                if (index % i == 0) isPrime = false;

            if (isPrime) // if this number is prime
                numOfPrimes++; // add it to the count
        }

        // array to hold the prime nums w/ size=how many prime nums we counted
        int[] primeNums = new int[numOfPrimes];

        int count = 0; // keep track of the position in the primeNums array

        for (int index = 2; index < 100; index++)
        {
            boolean isPrime = true; // initially true, reset this every loop

            // for every number that can factor into the index number
            for (int i = 2; i < index; i++)
                // if a number is found that factors into it, it's not prime
                if (index % i == 0) isPrime = false;

            if (isPrime) // if this number is prime
            {
                primeNums[count] = index; // add this prime num to the array
                count++; // change the array position tracker for next number
            }
        }

        for (int n : primeNums) // for every integer in primeNums array
           System.out.print(n + " "); // display that integer to the console
    }
}

这不会生成素数。修复您的算法并回答您的问题,您可以使用ArrayList存储找到的素数。注意:数组的大小是固定的。您确定对素数的测试是正确的吗?素数是一个大于1的自然数,除1和它本身外,没有其他正因子。大于1的非素数的自然数称为复合数。索引%5、索引%7以及2和3的所有非倍数如何?herpite derp。你们说得对。所以我必须加上5和7?还有其他可除数吗?数组是固定的,但您不需要填充它们,不是吗?编译器给了我两个无法找到指向列表和数组列表的符号错误。我正在使用第一段代码。@munchshair尝试添加import java.util.ArrayList;并导入java.util.List;到你们班的第一名。哇!现在它跑了!它还活着
public class QC3PrimeNumbers
{

    public static void main (String[] args)
    {
        System.out.println ("Here are the prime numbers: ");

        int numOfPrimes = 0; // counts the # of prime nums in your region

        // this loop will count up the number of prime numbers
        for (int index = 2; index < 100; index++)
        {
            boolean isPrime = true; // initially true, reset this every loop

            // for every number that can factor into the index number
            for (int i = 2; i < index; i++)
                // if a number is found that factors into it, it's not prime
                if (index % i == 0) isPrime = false;

            if (isPrime) // if this number is prime
                numOfPrimes++; // add it to the count
        }

        // array to hold the prime nums w/ size=how many prime nums we counted
        int[] primeNums = new int[numOfPrimes];

        int count = 0; // keep track of the position in the primeNums array

        for (int index = 2; index < 100; index++)
        {
            boolean isPrime = true; // initially true, reset this every loop

            // for every number that can factor into the index number
            for (int i = 2; i < index; i++)
                // if a number is found that factors into it, it's not prime
                if (index % i == 0) isPrime = false;

            if (isPrime) // if this number is prime
            {
                primeNums[count] = index; // add this prime num to the array
                count++; // change the array position tracker for next number
            }
        }

        for (int n : primeNums) // for every integer in primeNums array
           System.out.print(n + " "); // display that integer to the console
    }
}
import java.io.*;
import java.util.*;              
class numbers
{
public static void main(String args[])
{
    int[] prime=new int[20];
    int[] nonprime=new int[20];
    int count=0;
    int count1=0;`enter code here`
    try
        {
        InputStreamReader r=new InputStreamReader(System.in);  
        BufferedReader br=new BufferedReader(r);  
        System.out.println("enter n value");
        int n=Integer.parseInt(br.readLine());
        for(int i=1;i<n;i++)
        {   
            if(i%2!=0)
            {
                if(i%3!=0)
                    {
                    prime[count]=i;
                    count++;
                    }
            }
            else    
            {
            nonprime[count1]=i;
            count1++;
            }
        }
        System.out.println("prime");
        for(int m=0;m<count;m++)
        {
            System.out.println(prime[m]);
        }
        System.out.println("Non-prime");
        for(int k=0;k<count1;k++)
        {
            System.out.println(nonprime[k]);
        }
    }
    catch(Exception e)
    {
        System.out.println(e);
    }
}
}