所以这是我的问题: 我有两个数组,我想计算两个数组上完全相同的位置[i]上有多少个值,两个数组上有多少个值相同但不同的[i]
我尝试了使用for和数组大小的常规循环,但是它显示的奇怪值与预期值相差
package stackOverflow;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class mainStack extends JFrame implements ActionListener {
JButton jbr,jbv,jbb,jbo,jbn,jbj;
JTextField l11b;
String a;
int tabRef[]= {0,1,2,3};
int correctAndSameCase=0;
int correctButDiffCase=0;
mainStack(){
this.setLayout(null);
jbr = new JButton("Rouge");
jbr.setBounds(0,80,85,30);
add(jbr);
jbv = new JButton("Vert");
jbv.setBounds(125, 80, 85, 30);
add(jbv);
jbb = new JButton("Bleu");
jbb.setBounds(0, 120, 85, 30);
add(jbb);
jbj = new JButton("Jaune");
jbj.setBounds(125, 120, 85, 30);
add(jbj);
jbo = new JButton("Orange");
jbo.setBounds(0, 160, 85,30);
add(jbo);
jbn = new JButton("Noir");
jbn.setBounds(125,160, 85,30);
add(jbn);
jbr.addActionListener(this);
jbv.addActionListener(this);
jbb.addActionListener(this);
jbj.addActionListener(this);
jbo.addActionListener(this);
jbn.addActionListener(this);
setLayout(null);
setSize(800,800);
setVisible(true);
}
private int index = 0;
private int p=0;
private int tabAnswer[][] = new int[3][4];
private int i;
public void actionPerformed(ActionEvent e) {
if (e.getSource().equals(jbr)) {
tabAnswer[p][index] = 0;
} else if (e.getSource().equals(jbv)) {
tabAnswer[p][index] = 1;
} else if (e.getSource().equals(jbj)) {
tabAnswer[p][index] = 2;
} else if (e.getSource().equals(jbb)) {
tabAnswer[p][index] = 3;
} else if (e.getSource().equals(jbo)) {
tabAnswer[p][index] = 4;
} else if (e.getSource().equals(jbn)) {
tabAnswer[p][index] = 5;
}
index++;
if (index >= tabAnswer[p].length) {
System.out.println(Arrays.toString(tabAnswer[p]));
for(i=0 ; i<tabRef.length;i++) {
if(tabAnswer[p][i]==tabRef[i]) {
correctAndSameCase++;
}else if(tabAnswer[p][i]==tabRef[0] & tabAnswer[p][i]!=tabRef[i] || tabAnswer[p][i]==tabRef[1] & tabAnswer[p][i]!=tabRef[i] || tabAnswer[p][i]==tabRef[2]& tabAnswer[p][i]!=tabRef[i] ||tabAnswer[p][i]==tabRef[3] & tabAnswer[p][i]!=tabRef[i]) {
correctButDiffCase++;
}
}
index = 0;
p++;
System.out.println(correctAndSameCase+" number are on the same case on both arrays");
System.out.println(correctButDiffCase+" number are on different case on both arrays");
if(p>=3) {
p=0;
}
correctAndSameCase=0;
correctButDiffCase=0;
}
}
public static void main(String[] args) {
mainStack t= new mainStack();
}
这里tabAnswer {5,4,3,2}
correctAndSameCase
应该为0,而correctButDiffCase
应该为2,但实际上它给了我0和1作为答案。
编辑:我可以看到问题在于将值设置为correctButDiffCase
,但我不知道如何解决它
答案 0 :(得分:0)
此处是计算这两个值的简短代码段。 但是我不知道,是否要计算一些特殊情况(请参阅TODO)。
public static void main(String[] args) throws Exception {
List<Integer> tabRef = Arrays.asList(10, 5, 3, 5, 2, 6);
List<Integer> tabRef2 = Arrays.asList(12, 8, 3, 5, 6, 2);
int matchesOnSameIndex = 0;
int matchesButDifferentIndex = 0;
for (int i = 0; i < tabRef.size(); i++) {
//compare on same index, if other list has element on this index
if (tabRef2.size() > i) {
if (tabRef.get(i) == tabRef2.get(i)) {
//same element on same index
matchesOnSameIndex++;
}
}
//check if value exists in other list
if (tabRef2.contains(tabRef.get(i))) {
//if yes, check if it is not at same position
if (tabRef2.size() > i) {
if (tabRef2.get(i) != tabRef.get(i)) {
//not same index
//TODO must count multiple times, if element exists multiple times?
matchesButDifferentIndex++;
}else {
//TODO must count, if also exists on other index?
}
}else {
//same position not existing, so must be on other position
matchesButDifferentIndex++;
}
}
}
System.out.println("matchesOnSameIndex: "+matchesOnSameIndex);
System.out.println("matchesButDifferentIndex: "+matchesButDifferentIndex);
}
答案 1 :(得分:0)
您的用于计数发生次数的代码看起来不错(尽管有些混乱)。 我使用您的代码编写了一个junit测试,没有问题:
@Test
public void testArrayCheck() {
int[] tabRef = {0,1,2,3};
int tabAnswer[][] = new int[3][4];
int[] tabAnswerPos0 = {0,0,0,0};
tabAnswer[0] = tabAnswerPos0;
int p = 0;
int correctAndSameCase = 0;
int correctButDiffCase = 0;
Set<Integer> setArrayValues = new HashSet<Integer>();
for(int i=0 ; i<tabRef.length;i++) {
if(tabAnswer[p][i]==tabRef[i]) {
correctAndSameCase++;
}else if(tabAnswer[p][i]==tabRef[0] && tabAnswer[p][i]!=tabRef[i]
|| tabAnswer[p][i]==tabRef[1] && tabAnswer[p][i]!=tabRef[i]
|| tabAnswer[p][i]==tabRef[2] && tabAnswer[p][i]!=tabRef[i]
||tabAnswer[p][i]==tabRef[3] && tabAnswer[p][i]!=tabRef[i]) {
if (!setArrayValues.contains(tabAnswer[p][i])) {
correctButDiffCase++;
setArrayValues.add(tabAnswer[p][i]);
}
}
}
System.out.println(correctAndSameCase+" number are on the same case on both arrays");
System.out.println(correctButDiffCase+" number are on different case on both arrays");
}
1 number are on the same case on both arrays
1 number are on different case on both arrays
您确定tabAnswers的值为{5,4,3,2}吗?