简体   繁体   English

比较具有不同值Java的2个String数组的索引

[英]Compare the index of 2 String arrays with different values Java

I have 2 parallel arrays: the first contains State Names, the second Capitals of the states. 我有2个并行数组:第一个包含State Names,第二个包含状态的Capitals。

I'm making a quiz that randomly generates a State then asks the user to enter the Capital of the state. 我正在做一个随机生成State的测验,然后要求用户输入州的首都。 Once the input is received I want to call a method to check if the index of the capital entered is the same as the index of the state it goes with. 一旦收到输入,我想调用一个方法来检查输入的资本的索引是否与它所处的状态的索引相同。

ie: stateArray[0] = "New York" and capitalArray[0] = "Albany" . 即: stateArray[0] = "New York"capitalArray[0] = "Albany"

Check Answer Method 检查答案方法

 public static void checkAnswer(String[]stateArray, String capitalArray, String answer)
 {
    int index;
    for (int i = 0; i < capitalArray.length; i++){

        if(capitalArray[i].equalsIgnoreCase(answer)){
            index = i;
        }
    }

    if(capitalArray[index] == stateArray[index]) 
        {
            System.out.println("correct");
        }
        else
        {
            System.out.println("incorrect");
        }
    }

I know the second if statement is wrong. 我知道第二个if语句是错误的。 How can I compare the two arrays using the index where the users answer was found in the capitalArray ? 如何使用在capitalArray中找到用户回答的索引来比较两个数组?

boolean checkAnswer(String[] stateArray, String[] capitalArray, String displayedState, String answer) {
    for (int i = 0; i < stateArray.length; i++) {
        if (stateArray[i].equals(displayedState) && capitalArray[i].equals(answer)) {
            return true;
        }
    }
    return false;
}

Or something. 或者其他的东西。 The key is you need to pass in something to represent the state you displayed. 关键是你需要传递一些东西来代表你所展示的状态。

Since you know what state you asked about you should know its array index as well. 既然你知道你问的状态,你也应该知道它的数组索引。 As you see below both arrays are declared as class variables. 如下所示,两个数组都被声明为类变量。

... class Quiz {

private String[] states = new String[50];
private String[] capitals = new String[50];

... method to fill both arrays with the correct data

 public static void checkAnswer(int question, String answer)
 {


    if(capitalArray[question].equalsIgnoreCase(answer)){
        {
            System.out.println("correct");
        }
        else
        {
            System.out.println("incorrect");
        }
   }
}

It's better to have checkAnswer method's return type as Boolean , but I left it your way. checkAnswer方法的返回类型设置为Boolean更好,但我保留了它的方式。

You need to keep track of the index that holds the State displayed to the user. 您需要跟踪保存显示给用户的状态的索引。 For example, the way your code is written now gives the user the ability to get a right answer by giving a wrong answer. 例如,现在编写代码的方式使用户能够通过给出错误的答案来获得正确的答案。 Take this example as explanation: 以此为例进行说明:

string[] stateArray = {"New York", "California"};
string[] capitalArray = {"Albany", "Sacramento"};

If you were to show "New York" as the question and the user happens to answer "Sacramento" your code would display correct. 如果您要显示“纽约”作为问题并且用户碰巧回答“萨克拉门托”,那么您的代码将显示正确。

You also need a case in which the answer does not match any of the capitals in the array. 您还需要一个答案与数组中的任何大写都不匹配的情况。 One way of doing this to implement in your code is to initiate the index to -1. 在代码中实现此操作的一种方法是将索引启动为-1。

int index = -1;

Once you finish the for loop check if index is -1 and display "Your answers is not a valid State" or something along those lines. 完成for循环后,检查index是否为-1并显示“您的答案不是有效状态”或沿着这些行显示的内容。

Maybe use a HashMap, I am not completely familiar with Java it appears to be the similar to a Dictionary in Python. 也许使用HashMap,我并不完全熟悉Java,它似乎与Python中的Dictionary类似。 Dictionary object has great performance. Dictionary对象具有很好的性能。

An alternate implementation in Java would be to use a Map instead of two arrays. Java中的另一种实现方式是使用Map而不是两个数组。

Map<String,String> stateCapitals = new HashMap<String,String>();
stateCaptitals.put("New York", "Albany");

then you can check the map with 然后你可以查看地图

public voic checkAnswer(String chosenState, String chosenCapital) {
  if (stateCapitals.get(chosenState).equals(chosenCapital) {
     System.out.println("you are correct!");
  }
}

This does not do it with 2 parallel arrays, but it is a better implementation if your real concern is the type of data you mentioned, and not the arrays themselves. 对于2个并行数组,这不能做到这一点,但如果你真正关心的是你提到的数据类型而不是数组本身,那么这是一个更好的实现。

Try this function it return array:- 试试这个函数它返回数组: -

public static String[] numSame (String[] list1, String[] list2) 
     {  
          int same = 0;  
          for (int i = 0; i <= list1.length-1; i++) 
          {  
             for(int j = 0; j <= list2.length-1; j++) 
             {  
                if (list1[i].equals(list2[j])) 
                {  
                    same++;  
                    break;  
                }  
             }  
          }  

          String [] array=new String[same];
          int p=0;
          for (int i = 0; i <= list1.length-1; i++) 
          {  
             for(int j = 0; j <= list2.length-1; j++) 
             {  
                if (list1[i].equals(list2[j])) 
                {  
                    array[p]=  list1[i]+"";
                    System.out.println("array[p] => "+array[p]);
                    p++;
                    break;  
                }  
             }  
          } 
          return array;
       }  

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

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