實驗五:任意輸入10個int數據,排序輸出,並找出其中的素數

 

package lin;java

 

import java.util.Scanner;數組

 

public class Paixu {排序

 

    public static void main(String[] args) {class

        Scanner s = new Scanner(System.in);import

     int temp;im

                        //對數組事先聲明並建立10個空間next

     int[] a = new int[10];static

                    //把輸入的數存儲爲數組new

     for (int i = 0; i < 10; i++) {void

            a[i] = s.nextInt();

                     }

                    //排序

     for (int i = 0; i < 10; i++) {

        for (int j = i + 1; j < 10; j++) {

               if (a[i] > a[j]) {

                    temp = a[i];

                    a[i] = a[j];

                    a[j] = temp;

                            }

                        }

                    }

                    //輸出結果

     for (int i = 0; i < 10; i++) {

         System.out.print(a[i] + " ");

            }

     System.out.println(" ");

     System.out.print("素數是:");

     //判斷素數

     for(int i=0;i<10;i++)

        {

         if(a[i]==2||a[i]==3||a[i]==5||a[i]==7)

         System.out.print(a[i]+" ");

         if(a[i]>2)

          {

             if(a[i]%2!=0) {

                 for(int j=3;j<Math.sqrt(a[i]);j+=2)

                 {

                     if(a[i]%j==0) 

                         break;

         else {

           System.out.print(" "+a[i]);

            break;

                    }

                   }

                 }

                }

              }

     

     

     

    }

 

}

輸出結果:

10 9 8 7 6 5 4 3 2 1

1 2 3 4 5 6 7 8 9 10

素數是:2 3 5 7 

實驗心得:

1.理解了數組的定義與輸出格式

2.排序輸出採用的是冒泡排序 對於此排序不是很熟練 因此還有待增強聯繫。

相關文章
相關標籤/搜索