java代码以相反的顺序打印备用素数

时间:2015-04-23 03:28:10

标签: java reverse

以下是我要打印的Java代码' n'素数no.s但我想修改它,以便我可以以相反的顺序打印替代素数。

.table-responsive
  table#shift-list-table.table.table-vcenter.table-condensed.table-hover
    thead
      tr
        th Shift Code
        th Shift Type
        th Shift Start Date
    tbody
      each shifts
        tr
          td {{code}}
          td {{type}}
          td {{formatDate createdAt}}

3 个答案:

答案 0 :(得分:1)

您可以在ArrayList<Integer>

中添加每个号码

设置完所有值后,只需反转列表。

Collections.reverse(list); 

答案 1 :(得分:1)

根据您的代码我做了一些修改,下面是我如何以相反的顺序实现备用素数

Hi Ram,How are you?Thank you.

}

结果输出

public static void main(String args[])
   {
      int n;
      int status = 1;
      int num = 3;
      List<Integer> primeNumberList=new ArrayList<Integer>();
      //For capturing the value of n
      Scanner scanner = new Scanner(System.in);
      System.out.println("Enter the value of n:");
      //The entered value is stored in the var n
      n = scanner.nextInt();
      if (n >= 1)
      {
         System.out.println("First "+n+" prime numbers are:");
         //2 is a known prime number
         System.out.println(2);
         primeNumberList.add(2);
      }

  for ( int i = 2 ; i <=n ;  )
  {
     for ( int j = 2 ; j <= Math.sqrt(num) ; j++ )
     {
        if ( num%j == 0 )
        {
           status = 0;
           break;
        }
     }
     if ( status != 0 )
     {
        System.out.println(num);
        primeNumberList.add(num);
        i++;
     }
     status = 1;
     num++;
  }

  //reversing collection 
  Collections.reverse(primeNumberList);
   System.out.println("\n");
  System.out.println("Alternate Prime numbers in reverse order \n");
  for(int k=0;k<primeNumberList.size();k++){
     if((k%2)==0)
          System.out.println(""+primeNumberList.get(k));
  }

答案 2 :(得分:0)

我会这样。


import java.util.Scanner;
class AlternatePrimeNumbers
{
    static Scanner input = new Scanner(System.in);
    public static void main(String[] args)
    {
        Main obj = new Main();
        System.out.print("Enter the value of n :- ");
        int num = input.nextInt();
        System.out.println("Alternate Prime numbers in reverse order upto "+num+" :- ");
        obj.alternatePrimeNumbers(num);
    }
}

class Main
{
    public void alternatePrimeNumbers(int n)
    {
        int i;
        boolean prime,flag=true;
        for(i=n; i>=1; i--)
        {
            prime = checkPrime(i);
            if(prime)
            {
                if(flag)
                {
                    System.out.print(i+" ");
                    flag = false;
                }
                else if(flag==false)
                    flag = true;
            }
        }
    }

    public boolean checkPrime(int n)
    {
        int i;
        boolean prime=true;
        for(i=2; i<=n/2; i++)
        {
            if(n%i==0) //condition for non-prime numbers
            {
                prime = false;
                break;
            }
        }
        return prime;
    }
}