6

私は、指定された売上高と等しいすべての売上高を見つけることになっている searchSales() メソッドを持っています。アプリケーションは、キーボードを使用して指定された売上高を入力するようにユーザーに要求し、それを検索します。キーボードから入力された売上高が見つかった場合、アプリケーションは売上高を表示します。それ以外の場合は、適切なメッセージを表示します。さて、私は等しい売上高の最初のインデックスのみを表示するコードを持っています.これを行う?

public static void searchSales(int search[]){

        Scanner input = new Scanner(System.in);

        System.out.print("Enter sales figure you want to find: ");
        int target = input.nextInt();
        int index = -1;
        for (int i=0; i<search.length; i++){
            if (search[i] == target){
                index=i;
                break;
            }
        }
        if (index == -1){
            System.out.println("Sales figure not found");
        }
        else {
            System.out.printf("Sales figure found at branch %d",index+1);
        }
    }
4

5 に答える 5

1
 public static void searchSales(int search[]){

    Scanner input = new Scanner(System.in);
    System.out.print("Enter sales figure you want to find: ");
    int target = input.nextInt();
    int index = -1;
    for (int i=0; i<search.length; i++){
        if (search[i] == target){
            index=i;
            System.out.printf("Sales figure found at branch %d\n",index+1);

        }
    }
    if (index == -1){
        System.out.println("Sales figure not found");
    }

}
于 2012-09-11T01:51:36.137 に答える
0

(この行によりコードがループを終了します)という行を削除し、break;各値を配列に格納して最後に出力するか、index変数に格納する代わりに出力します。

于 2012-09-11T01:47:55.710 に答える
0

a を使用しList<Integer>て、一致するインデックスを収集します。何かのようなもの:

    int target = input.nextInt();
    List<Integer> indices = new ArrayList<Integer>();
    for (int i=0; i<search.length; i++){
        if (search[i] == target){
            indices.add(i);
        }
    }
    if (indices.isEmpty()){
        System.out.println("Sales figure not found");
    }
    else {
        System.out.println("Sales figures found:  " + indices);
    }
于 2012-09-11T01:47:56.393 に答える
0

Arrays.asList を使用してから、List で indexOf/lastIndexOf を使用します。何かのようなもの:

ArrayList<Integer> sales = Arrays.asList(search)
int start = sales.indexOf(target);
int end = sales.lastIndexOf(target);
if(start==end){
    System.out.printf("Sales figure found at branch %d",start+1);
}
else{
    for(int i=start;i<=end-start;i++)
        if(sales.get(i)==target)
            System.out.printf("Sales figure found at branch %d",i+1);
}
于 2012-09-11T02:20:38.230 に答える
0

あなたの場合、どこにも何も保存する必要はありません。次のようなもので十分です。

public static void searchSales(int search[]){
    Scanner input = new Scanner(System.in);
    System.out.print("Enter sales figure you want to find: ");
    int target = input.nextInt();

    boolean found = false
    for (int i = 0 ; i < search.length ; i++)
        if (search[i] == target) {
            found = true;
            System.out.printf("Sales figure found at branch %d", i + 1);
        }
    if (! found)
        System.out.println("Sales figure not found");
}


highestSalesは別の話です。そこでは、次のものを保存する必要があります。

public static void highestSales(int[] nums) {
    List<Integer> list = new ArrayList<Integer>();
    list.add(0);
    for (int i = 1 ; i < nums.length ; i++) {
        if (nums[i] > nums[list.get(0)]) {
            list.clear();
            list.add(i);
        }
        else if (nums[i] == nums[list.get(0)])
            list.add(i);
    }
    System.out.println("Highest sales figure " + nums[list.get(0)] + " found at these branches");
    for (int i : list) System.out.println(i);
}
于 2012-09-11T02:47:51.510 に答える