因此,前提是我有7个描述符或质量,我必须问用户,每一个质量与另一个质量相比有多重要。所以,如果我有7个品质,为了方便,我叫它们1-7,我需要这样做.
1-2
1-3
1-4
1-5
1-6
1-7
然后,在我们继续前进的过程中,第一个数字应该从列表中删除,现在只有5个数字比较2和其余的数字。然后,这将持续到最后一个要比较的剩余数字为6-7。这使比较总数达到21。
我已经做了一个行之有效的方法,但很可能会成为任何面试的笑柄,我试着向大家展示这一点。我是一个第一年的程序员,试图加快自己的就业与这些附带项目,所以请理解,我知道这可能是可怕的,但我不确定最好的方式来做这件事。
这是我的密码,你会改什么?问题变量是一个ArrayList和结果变量。谢谢。
public void makeQuestions(){
for (int i = 0; i < 6; i++){
questions.add("How do you rate " + results.get(0) + " in importance compared to "+ results.get(i+1));
}
for (int i = 1; i < 6; i++){
questions.add("How do you rate " + results.get(1) + " in importance compared to "+ results.get(i+1));
}
for (int i = 2; i < 6; i++){
questions.add("How do you rate " + results.get(2) + " in importance compared to "+ results.get(i+1));
}
for (int i = 3; i < 6; i++){
questions.add("How do you rate " + results.get(3) + " in importance compared to "+ results.get(i+1));
}
for (int i = 4; i < 6; i++){
questions.add("How do you rate " + results.get(4) + " in importance compared to "+ results.get(i+1));
}
questions.add("How do you rate " + results.get(5) + " in importance compared to "+ results.get(6));
}发布于 2021-06-20 22:41:07
对于双for循环来说,这是一个很好的地方:
for (int i = 0; i < questions.size(); i++) {
for (int j = i + 1; j < questions.size(); j++) {
// Compare question i against question j
}
}https://stackoverflow.com/questions/68058528
复制相似问题