gpt4 book ai didi

java - 如果这两个数组在同一位置具有相同的数字,如何显示?

转载 作者:行者123 更新时间:2023-12-02 05:25:31 25 4
gpt4 key购买 nike

所以这是我的问题:我有两个数组,我想计算两个数组上完全相同的位置 [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 但我不知道如何修复它

最佳答案

这里有一个简短的代码片段,用于计算这两个值。但我不知道,是否要计算一些特殊情况(请参阅 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);

}

关于java - 如果这两个数组在同一位置具有相同的数字,如何显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56235250/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com