日期:2014-05-20  浏览次数:20661 次

java 从屏幕输入数据问题
import java.util.*;

public class Client {
public static void main (String[] args) {
Scanner in = new Scanner(System.in);
int arr[] = new int[5];
for(int i = 0;i < 5; i++){
arr[i] = in.nextInt();
}
/*arr[0] = in.nextInt();
arr[1] = in.nextInt();
arr[2] = in.nextInt();
arr[3] = in.nextInt();
arr[4] = in.nextInt();*/
DataOperation dataOperation = (DataOperation)XMLUtil.getBean();
System.out.println ("经过排序后的数组为:");
dataOperation.sort(arr);
dataOperation.search(arr,23);
System.out.println ("");
System.out.print ("您要查找的是第"+dataOperation.search(arr,23)+"个数");
}
}

输入完后,执行dataOperation.sort(arr);但是不执行dataOperation.search(arr,23);要继续输入
用arr[0] = in.nextInt();
arr[1] = in.nextInt();
arr[2] = in.nextInt();
arr[3] = in.nextInt();
arr[4] = in.nextInt();
可以执行dataOperation.search(arr,23)

------解决方案--------------------
有点不太理解你的意思是想像下面这样么?
Java code

public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int arr[] = new int[5];
        for (int i = 0; i < 5; i++) {
            arr[i] = in.nextInt();
        }
        Arrays.sort(arr);
        System.out.println("经过排序后的数组为:" + Arrays.toString(arr));
        
        for (int i = 0; i < 5; i++) {
            arr[i] = in.nextInt();
        }
        Arrays.binarySearch(arr, 23);
        System.out.print("您要查找的是第" + Arrays.binarySearch(arr, 23) + "个数");
    }