检查数组中的值 [英] Check for value in array

查看:64
本文介绍了检查数组中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要检查数组以查看用户输入是否已经存在,并显示一条有关是否存在用户输入的消息。第一部分工作正常,但是我尝试创建一种用于单词检查的方法,我不确定自己是否走对了,干杯。

I need to check the array to see if the user input is already present, and display a message as to whether it is or isn't there. The first part is working, but I tried to create a method for the word check, and I'm not sure if I'm on the right path or not, cheers.

 import java.util.Scanner;

public class InputLoop {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        String array[] = new String[10];
        int num = array.length, i = 0;
        System.out.println("Enter a word");
        for (i = 0; i < num; i++) {
            while (scan.hasNextInt()) // while non-integers are present...
            {

                scan.next(); // ...read and discard input, then prompt again
                System.out.println("Bad input. Enter a word");

            }

            array[i] = scan.next();
            WordCheck();
        }
    }

    public void WordCheck(String[] i) {
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter another word");

        if (scan.next().equals(array[i])) {
            System.out.println("The word has been found");
        } else {
            System.out.println("The word has not been found");
        }

    }

}


推荐答案

正确。显然,您已经经历了一个糟糕的思考过程,所以让我们清理一下状态,重新思考一下。

Right. You've clearly gone down a bad thought process, so let's just clear the slate and have a re-think.


  • 第一步:您想接受一些用户输入

  • 第二步:将其与以前的所有用户输入进行比较,以查看是否存在。


    • 如果存在,则返回一条消息,指示已输入值。

    • 否则忽略输入并继续执行

    • Step one: You want to take some user input
    • Step two: Compare it with all previous user inputs to see if it's present.
      • If it is present, return a message indicating that value has been inputted.
      • otherwise ignore the input and continue execution

      因此,让我们回顾一下您所拥有的内容以及如何更改它。

      So, let's review what you've got, and how you need to change it.

      公共静态无效main(String [] args)

      如果我是你,我会避免直接从此处调用方法。如果这样做,则每个方法都必须是静态的,这对于类功能的作用域是无意义的调整。在main方法内创建您的类的新实例,然后将此代码移到类的构造函数中。

      If I were you, I would avoid calling methods directly from here. If you do, every method will need to be static, which is a pointless adjustment in scope for the functionality of your class. Create a new instance of your class, inside the main method, and move this code to the class' constructor. This will remove the need to make every single method static.

      Scanner scan = new Scanner(System.in);
      String array[] = new String[10];
      

      好的,所以您已经创建了一个扫描器对象,该对象从中获取输入System.in 流。从键盘上输入时,这是合理的做法。您还创建了一个包含每个项目的数组。如果只希望用户能够输入10个值,则可以。就我个人而言,我会使用 ArrayList ,因为

      Okay, so you've created a scanner object that takes input from the System.in stream. That's a reasonable thing to do when taking input from the keyboard. You've also created an array to contain each item. If you only want the user to be able to type in 10 values, then this is fine. Personally, I would use an ArrayList, because it means you can take in as many user inputs as the user desires.

      其次,您需要一个函数将输入与所有其他输入进行比较。您目前所拥有的显然无法正常工作,所以让我们再做一次。

      Secondly, you want a function to compare the input, with all other inputs. What you have at the moment clearly isn't working, so let's have another go at it.

      您将需要一些输入, userInput ,以及将其与 allInputs 进行比较的集合。

      You will need some input, userInput, and a collection to compare it against, allInputs.

      allInputs 需要从程序的任何位置均可访问,因此将其放入字段而不是局部变量可能是明智的。

      allInputs needs to be accessible from any point in the program, so it's probably wise to make it into a field, rather than a local variable.

      然后,因为您要将userInput与所有值进行比较,因此需要一个foreach循环:

      Then, because you're comparing userInput against all values, you're going to need a foreach loop:

      for(String s : allInputs)
      {
          if(s.equals(userInput))
          {
             // Output message code.
          }
      }
      






      现在,诀窍是将其适合与此程序一起使用的循环内。这取决于您,因为我们不是代码编写服务。希望我的回答能使您走上正确的路:)


      Now the trick is fitting this inside a loop that works with this program. That is up to you, because we are not a code writing service. Hopefully my answer will put you on the right track :)

      这篇关于检查数组中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆