简体   繁体   English

Java增量编号

[英]Java incrementation numbering

The problem is that I can't properly display an output. 问题是我无法正确显示输出。
Input: 输入:

6 6
Berik 78 贝里克78
Zeynep 100 Zeynep 100
Saniya 75 三亚75
Daulet 45 Daulet 45
Abay 96 阿贝96
Andrey 75 安德烈75

Expected Output: 预期产量:

1) Zeynep: 100
2) Abay: 96
3) Berik: 78
4) Saniya: 75
5) Andrey: 75
6) Daulet: 45

My Output: 我的输出:

1) Zeynep: 100
2) Abay: 96
3) Berik: 78
4) Saniya: 75
4) Andrey: 75
5) Daulet: 45

As you can see, the numbering is incorrect. 如您所见,编号不正确。 Bug must be somewhere in incrementation logic when two points are equal. 当两个点相等时,错误必须在递增逻辑中的某个位置。 I pass numbering as an argument to the printMax method 我将编号作为参数传递给printMax方法

import java.util.ArrayList;
import java.util.Scanner;

public class a1_t13 {
public static void main(String[] args) {
    Scanner input = new Scanner(System.in);

    int num = input.nextInt();
    ArrayList<Integer> scores_unsorted = new ArrayList<>();
    ArrayList<String> names_unsorted = new ArrayList<>();


    for (int i=0; i<num; i++) {
        String value = input.next();
        int numValue = input.nextInt();
        scores_unsorted.add(numValue);
        names_unsorted.add(value);
    }

    int b = 1;
    for (int z = 0; z<num;z++) {
        int c = getMax(scores_unsorted);
        printMax(c, names_unsorted, scores_unsorted, b);
        b++;     //<================ HERE I'M DOING AN INCREMENTATION

    }

}

public static int getMax(ArrayList list) {
    int max = 0;
    for(int i=0;i<list.size();i++) {
        if (max< (int)list.get(i)) {
            max = (int) list.get(i);
        }
    }
    return max;
}


public static void printMax(int score, ArrayList names_unsorted, ArrayList scores_unsorted, int order) {
    for (int i=0;i<names_unsorted.size();i++) {
        if ((int) scores_unsorted.get(i) == score) {
            int score_index = scores_unsorted.indexOf(Integer.valueOf(score));
            System.out.println(order+")"+names_unsorted.get(score_index).toString()+": "+score);
            scores_unsorted.remove(score_index);
            names_unsorted.remove(score_index);
        }
    }
}

} }

After removing 100, 96 and 78, both lists have 3 elements. 删除100、96和78后,两个列表都包含3个元素。 Now 75 is the max. 现在最大为75。

printMax(c, names_unsorted, scores_unsorted, b) method is called with c=75 and b = 4. 使用c = 75和b = 4调用printMax(c,names_unsorted,scores_unsorted,b)方法。

Now in printMax() this condition will be true for two elements: 现在在printMax()中,此条件对于两个元素将成立:

if ((int) scores_unsorted.get(i) == score)

as score is 75 now and list has also two 75. So you should break the loop if already found one. 因为分数现在是75,列表也有两个75。因此,如果已经找到一个,则应该中断循环。

Your if condition block should be like this: 您的if条件块应如下所示:

if ((int) scores_unsorted.get(i) == score) {
    int score_index = scores_unsorted.indexOf(Integer.valueOf(score));       
    System.out.println(order+")"+names_unsorted.get(score_index).toString()+": "+score);
    scores_unsorted.remove(score_index);
    names_unsorted.remove(score_index);
    break;
}

The problem is in your printMax method, because you iterate over all names and two have the same count and you print out both with the same order number. 问题出在您的printMax方法中,因为您遍历所有名称,并且两个名称具有相同的计数,并且使用相同的订单号打印两个名称。 If you break your loop after the first found it will work. 如果您在第一个发现之后中断循环,它将起作用。

 public static void printMax(int score, ArrayList names_unsorted, ArrayList scores_unsorted, int order) {
    for (int i = 0; i < names_unsorted.size(); i++) {
        if ((int) scores_unsorted.get(i) == score) {
            int score_index = scores_unsorted.indexOf(Integer.valueOf(score));
            System.out.println(order + ")" + names_unsorted.get(score_index).toString() + ": " + score);
            scores_unsorted.remove(score_index);
            names_unsorted.remove(score_index);
            break;
        }
    }
}

The problem is that in printMax function you iterate through the whole array even if you find and print the maximum record you're searching for and you're increasing the value of b after going out of loop. 问题在于,即使您找到并打印了要搜索的最大记录,并且在退出循环后增加了b的值,在printMax函数中也要遍历整个数组。 The simplest way to make it work as you want is to put break at the end of if body: 使它按需工作的最简单方法是在if主体的末尾放置break:

public static void printMax(int score, ArrayList names_unsorted, ArrayList scores_unsorted, int order) {
    for (int i=0;i<names_unsorted.size();i++) {
        if ((int) scores_unsorted.get(i) == score) {
            int score_index = scores_unsorted.indexOf(Integer.valueOf(score));
            System.out.println(order+")"+names_unsorted.get(score_index).toString()+": "+score);
            scores_unsorted.remove(score_index);
            names_unsorted.remove(score_index);
            break;
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM