java 实例 数组
2017-05-15 22:09 backyyan 阅读(424) 评论(0) 编辑 收藏 举报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数组的元素为:\t"); 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] + "\t"); } 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("最终排序结果:\t"); for (int a = 0; a < A.length; a++) { System.out.print(A[a] + "\t"); } } }