Java经典练习题30

/*
题目:有一个已经排好序的数组。现输入一个数,要求按原来的规律将它插入数组中。
*/

import java.util.*;

public class Class30 {

public static void main(String[] args) {
// TODO Auto-generated method stub
int[] a = new int[]{1,3,5,7,9};
int[] b = new int[a.length + 1];
System.out.println("任意输入一个数字!");
Scanner sc = new Scanner(System.in);
int c = sc.nextInt();
if(c <= a[0]){
for(int i = 0; i < b.length; i++){
if(i == 0){
b[i] = c;
}else if(i > 0){
b[i] = a[i-1];
}
}
}
if(c >= a[a.length - 1]){
for(int i = 0; i < b.length; i++){
b[i] = a[i];
if( i == b.length - 1){
b[i] = c;
}
}
}
if((a[0] < c) && (c < a[a.length - 1])){
for(int i = 0; i < b.length; i++){
if(a[i] < c){
b[i] = a[i];
b[i+1] = c;
for(int t = i + 2; t < b.length; t++){
b[t] = a[t - 1];
}
}else{
break;
}

}
}
System.out.println("输出结果:");
for(int j = 0; j < b.length; j++){
System.out.print(b[j]);
}

}

}

posted on 2020-02-24 20:51  桌子哥  阅读(187)  评论(0)    收藏  举报