【问题标题】:Java, how to compare Strings with String ArraysJava,如何比较字符串和字符串数组
【发布时间】:2012-02-14 16:58:47
【问题描述】:

我已经在这里搜索了一段时间,但一直没有找到答案。

我基本上需要使用一个数组来完成这项大学作业。然后我应该检查输入(也是一个字符串)是否与存储在字符串数组中的任何内容匹配。

我知道使用 .equals() 方法可以轻松比较字符串。但是,同样的方法不适用于 String 数组。

我为 StackOverflow 创建了以下代码示例,如果您愿意,可以使用它向我解释。

我做错了什么?

import java.util.Scanner;

class IdiocyCentral {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        /*Prints out the welcome message at the top of the screen*/
        System.out.printf("%55s", "**WELCOME TO IDIOCY CENTRAL**\n");
        System.out.printf("%55s", "=================================\n");

        String [] codes = {"G22", "K13", "I30", "S20"};

        System.out.printf("%5s%5s%5s%5s\n", codes[0], codes[1], codes[2], codes[3]);
        System.out.printf("Enter one of the above!\n");

        String usercode = in.nextLine();

        if (codes.equals(usercode)) {
            System.out.printf("What's the matter with you?\n");
        }
        else {
            System.out.printf("Youda man!");
        }

    }
}

如果之前有人问过这个问题,我只是错过了,我很抱歉,如果它是一个双重问题,我会删除它。

【问题讨论】:

  • 您需要遍历数组并单独检查每个字符串。

标签: java arrays string equals


【解决方案1】:

我想你想检查数组是否包含某个值,是吗?如果是这样,请使用contains 方法。

if(Arrays.asList(codes).contains(userCode))

【讨论】:

  • @PeterOlsen 啊终于可以接受你的回答了。谢谢一百万!
  • @PeterOlsen - 这可能是最干净的解决方案,但我希望 OP 的讲师期望他/她使用循环来编写代码......并且在这个过程中,练习使用数组和循环。
  • @StephenC 这只是代码的一部分,它是彼得奥尔森建议的完美方式。我必须使用循环,但它与这部分代码是分开的:)
【解决方案2】:

现在你似乎在说“这个字符串数组是否等于这个字符串”,当然它永远不会。

也许您应该考虑使用循环遍历字符串数组,并检查每个字符串是否与输入的字符串相等?

...还是我误解了你的问题?

【讨论】:

  • @PeterOlsen 建议的工作。我只是在等待能够接受他的回答。对不起,如果我的问题不够清楚。
  • 不用担心。因为这是一个大学问题,尽管我认为您的讲师正在寻找您学习循环等控制流。也就是说,无论如何,您的大学课程可能已经过去了:)
【解决方案3】:

使用循环遍历codes 数组,询问每个元素是否为equals()usercode。如果一个元素相等,您可以停止并处理这种情况。如果没有一个元素等于usercode,则采取适当的措施来处理这种情况。在伪代码中:

found = false
foreach element in array:
  if element.equals(usercode):
    found = true
    break

if found:
  print "I found it!"
else:
  print "I didn't find it"

【讨论】:

  • 当我看到伪代码时,我几乎立刻就认为它是 Python。
【解决方案4】:

如果我正确理解您的问题,您似乎想了解以下内容:

如何检查我的String 数组是否包含usercode,即刚刚输入的String

请参阅here 了解类似问题。它引用了先前答案指出的解决方案。我希望这会有所帮助。

【讨论】:

    【解决方案5】:

    您可以直接使用ArrayList 而不是使用数组,并且可以使用contains 方法来检查您通过ArrayList 传递的值。

    【讨论】:

      【解决方案6】:
      import java.util.Scanner;
      import java.util.*;
      public class Main
      {
        public static void main (String[]args) throws Exception
        {
          Scanner in = new Scanner (System.in);
          /*Prints out the welcome message at the top of the screen */
            System.out.printf ("%55s", "**WELCOME TO IDIOCY CENTRAL**\n");
            System.out.printf ("%55s", "=================================\n");
      
            String[] codes =
          {
          "G22", "K13", "I30", "S20"};
      
            System.out.printf ("%5s%5s%5s%5s\n", codes[0], codes[1], codes[2],
                   codes[3]);
            System.out.printf ("Enter one of the above!\n");
      
          String usercode = in.nextLine ();
          for (int i = 0; i < codes.length; i++)
            {
          if (codes[i].equals (usercode))
            {
              System.out.printf ("What's the matter with you?\n");
            }
          else
            {
              System.out.printf ("Youda man!");
            }
            }
      
        }
      }
      

      【讨论】:

        猜你喜欢
        • 2015-05-16
        • 1970-01-01
        • 2012-08-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-06-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多