zoukankan      html  css  js  c++  java
  • java 实例 数组

    package rj435;
    
    import java.util.Scanner;
    /*编程实现:从键盘录入两组数,每组5个元素,分别存储在B和C数组中,
    然后把两个数组反向相加,相加的结果存储在A数组中,
    然后再把A数组的元素按升序排序之后输出。*/
    public class Arrays {
        public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
            int[] B = new int[5];
            int[] C = new int[5];
            int[] A = new int[5];
            System.out.println("请输入B数组包含的5个元素:");
            B[0] = input.nextInt();
            B[1] = input.nextInt();
            B[2] = input.nextInt();
            B[3] = input.nextInt();
            B[4] = input.nextInt();
            System.out.println("请输入C数组包含的5个元素:");
            C[0] = input.nextInt();
            C[1] = input.nextInt();
            C[2] = input.nextInt();
            C[3] = input.nextInt();
            C[4] = input.nextInt();
            System.out.print("A数组的元素为:	");
            for (int i = 0; i < B.length; i++) {
                A[0]=B[0]+C[4];
                A[1]=B[1]+C[3];
                A[2]=B[2]+C[2];
                A[3]=B[3]+C[1];
                A[4]=B[4]+C[0];
                System.out.print(A[i] + "	");
            }
            for (int i = 0; i < A.length - 1; i++) {
                for (int j = 0; j < A.length - i - 1; j++) {
                    if (A[j] > A[j + 1]) {
                        int temp = A[j];
                        A[j] = A[j + 1];
                        A[j + 1] = temp;
                    }
                }
    
            }
            System.out.print("最终排序结果:	");
            for (int a = 0; a < A.length; a++) {
                System.out.print(A[a] + "	");
            }
        }
    
    }
  • 相关阅读:
    redis
    一句话总结面向对象
    HTML鼠标悬停改变样式
    div 在css中透明度怎么调?
    SpringMyBatisDay03
    list与Set、Map区别及适用场景
    Java之构造器和构造方法的使用和意义
    Set keys=Map.keyset()
    SpringMyBatisDay02
    CSS选择器可以用数字开头吗
  • 原文地址:https://www.cnblogs.com/Angella/p/6858745.html
Copyright © 2011-2022 走看看