功能描述:有一个有序整数数组,要求输入一个数字,在数组中查找是否有这个数,如果有,将该数从数组中删除,要求删除后的数组仍然保持有序;如果没有,则显示“数组中没有这个数!”
package com.chy.array;
import java.util.Scanner;
public class Ex5 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int a[]= { 1, 2, 3, 4, 5, 6 };
int b[] = new int [a.length];
System.out.println("请输入一个数:");
int num = sc.nextInt();
int c = 1;
int i = 0;
for (; i < a.length; i++) {
if (a[i] == num) {
c = 1;
break;
} else {
c = 0;
}
}
if (c == 1) {
for (int j = 0; j < i; j++) {
b[j] = a[j];
}
for (int j = i; j < a.length - 1; j++) {
b[j] = a[j + 1];
}
for (int n = 0; n < a.length - 1; n++) {
System.out.print(b[n]);
}
} else {
System.out.println("数组中没有这个数!");
}
}
}