6

我有方法 searchSales() 应该找到等于给定销售数字的所有销售数字。应用程序要求用户使用键盘输入给定的销售数字并搜索它。如果找到从键盘输入的销售数字,则应用程序显示销售数字/数字,否则显示适当的消息。好吧,我有一个代码,它只显示相等销售数字的第一个索引,例如:数组有元素 1、2、3、3、4、5,我想找到 [array] = 3 的所有索引。我怎么能做这个?

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

使用 aList<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 回答