实验五:任意输入10个int类型数据,排序输出,再找出素数

import java.util.Scanner;
public class Pxsushu {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
          Scanner s = new Scanner(System.in);
          int temp;
           //对数组事先声明并创建10个空间
         
          int[] a = new int[10];
                 //把输入的数存储为数组
          for (int i = 0; i < 10; i++) {
                 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]==0 && a[i]==1)
       continue;
      else if(a[i]/2>1 && a[i]%2==0)
       continue;
      else if(a[i]/3>1 && a[i]%3==0)
       continue;
      else if(a[i]/5>1 && a[i]%5==0)
       continue;
      else if(a[i]/7>1 && a[i]%7==0)
       continue;
      else
       System.out.print(" "+a[i]);
      }
      System.out.println();

    }

}

实验结果

心得体会:

  通过本次程序设计,对冒泡排序有了进一步的掌握,将C语言中的学的冒泡排序应用到了JAVA程序语言的设计当中了。

posted @ 2019-04-07 12:05  无尽的心碎  阅读(187)  评论(1编辑  收藏  举报