zoukankan      html  css  js  c++  java
  • Java数组5作业(2015-8-27)

    1、从键盘输入7个数存入一维数组中, 先输出下标为奇数的元素,再输出下标为偶数的元素。

    import java.util.Scanner; 

    public class SecondTest{

          public static void main(String[] args){  

                 //--定义一维数组  

                 int[] arr = new int[7];   

                 Scanner scan = new Scanner(System.in);

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

                         System.out.print("请输入第"+(i+1)+"个数据:");   

                         int number = scan.nextInt();   

                         arr[i] = number;

                 }

                  //--输出:下标为奇数的数据   

                System.out.print("奇数下标的数据为:");   

                for(int i=0;i<arr.length;i++){    

                       if(i%2!=0){    

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

                       }  

                }

                System.out.println();   

                //--输出:下标为偶数的数据   

                System.out.print("偶数下标的数据为:");  

                for(int i=0;i<arr.length;i++){   

                           if(i%2==0){     

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

                           }  

      }  } }

    2、从键盘输入7个数存入一维数组中, 将下标为奇数的元素值取倒数后重新存入该数组中, 并输出所有元素。

    import java.util.Scanner;

    public class ThreeTest{

          public static void main(String[] args){

                         //--定义一维数组  

                        double[] arr = new double[7];

                        Scanner scan = new Scanner(System.in);

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

                                   System.out.print("请输入第"+(i+1)+"个数据:");  

                                   double number = scan.nextDouble();

                                   arr[i] = number;  

                        }   

                                   //--奇数下标取倒数

                        for(int i=0;i<arr.length;i++){

                                       if(i%2!=0){   

                                       //--加入奇数位置上的数据为0的话!

                                                       if(arr[i]!=0){    

                                                               double number = 1/arr[i];     

                                                               arr[i] = number;    

                                                        }       

                                       }

                          }  

                          for(int i=0;i<arr.length;i++){   

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

                          }

     } }

    3、从键盘输入6个数存入一维数组a中, 再将其中的值按相反的顺序存入数组b中并输出

    import java.util.Scanner;

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

                    //a--1,2,3   //b--3,2,1

                    int[] a = new int[6];

                    int[] b = new int[6];   

                    int j = 0;  

                    Scanner scan = new Scanner(System.in);  

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

                              System.out.print("请输入第"+(i+1)+"个数据:");  

                              int number = scan.nextInt();   

                              a[i] = number;      

                    }  

                   for(int i=(a.length-1);i>=0;i--){

                             b[j] = a[i];  

                             j++;   

                   }  

                  System.out.print("a当中的数据为:");  

                   for(int i=0;i<a.length;i++){   

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

                   }   

                   System.out.println();

                   System.out.print("b当中的数据为:");   

                   for(int i=0;i<b.length;i++){

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

     } }

    4、选择排序讲解 与 从键盘输入5个数,用选择法按降序排序并输出

    import java.util.Scanner; 

    public class FiveTest{

     public static void main(String[] args){  

                 //--定义数组   int[] arr = new int[5];  

                 //--从键盘中输入5个数据:   

                 Scanner scan = new Scanner(System.in);  

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

                           System.out.print("请输入第"+(i+1)+"个数据:");  

                           int number = scan.nextInt();

                           arr[i] = number;  

                 }  

                 System.out.println("数据全部输入完毕!!!");

                  //-----选择排序---------

                  //--定义最小数据的下标   

                    int min_index = 0;   

                   for(int i=0;i<arr.length-1;i++){   

                               min_index = i;

                               for(int j=i+1;j<arr.length;j++){    

                                         if(arr[min_index]>arr[j]){   

                                                   min_index = j;    

                                          }  

                               }

                              if(i!=min_index){    

                                          int temp = arr[i];   

                                          arr[i] = arr[min_index];    

                                          arr[min_index] = temp;   

                             }

                  }  

                  for(int i=0;i<arr.length;i++){  

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

                   }

     } }

  • 相关阅读:
    Python3.7.1学习(六)RabbitMQ在Windows环境下的安装
    使用jmeter进行简单的压测
    python中mock的使用
    python-shutil模块
    python-常用模块之os、sys
    python-冒泡排序
    python-正则基础
    python-二维数组实现90度旋转
    python-使用递归实现二分法
    python-递归的实现
  • 原文地址:https://www.cnblogs.com/Ly426/p/4763739.html
Copyright © 2011-2022 走看看