简体   繁体   English

如何检查输入(整数)是否与数组中存在的另一个值(整数)匹配?

[英]How do I check if a input (integer) matches another value (integer) that exits within my array?

I just learned about array and wanted to build a simple program where I use do-while loops to only accept numbers entered that exist in the array but I'm having some trouble see the problem 我刚刚学习了数组,并想构建一个简单的程序,在其中使用do-while循环仅接受输入的存在于数组中的数字,但是我遇到了一些麻烦,无法解决问题

Scanner input = new Scanner(System.in);
int[] values; 
values = new int[5];
values[0] = 16;
values[1] = 10;
values[2] = 11;
values[3] = 14;
values[4] = 17;
    int value;
    do {
    System.out.println("Enter a number:");
        value = input.nextInt();    
    }
    while(value != values[]);

the rest was going to be "if" statements that contained some out going text based on the which numbers from the array are entered. 其余的将是“ if”语句,其中包含一些基于输入数组中数字的输出文本。 The "while" condition comes up in an error in the way I entered it as 我输入时,“ while”条件出现错误

EDIT: Since this isn't possible in java could I just write !=16,10,11,14,17 without getting an error? 编辑:由于这不可能在Java中,我可以只写!= 16,10,11,14,17而不会出错? The array won't work in the condition so I'm still stuck in figuring a way to including multiple numbers in the condition 数组在这种情况下不起作用,因此我仍然想办法在条件中包括多个数字

The termination condition of your do loop: do循环的终止条件:

while(value != values[])

is not legal Java. 不是合法的Java。 You cannot compare an int value to an array and you cannot reference an array using values[] . 您不能将int值与数组进行比较,也不能使用values[]引用数组。 I don't know what you're trying to accomplish, but you need to put in a specific subscript in the termination condition or modify it in some other way. 我不知道您要完成什么,但是您需要在终止条件中放入特定的下标或以其他方式对其进行修改。

As you see it's not possible to just write while(value != values[]) but you can search a value in an array or check if the value is contained in a List: 如您所见, 不可能只编写while(value!= values []),但可以在数组中搜索值或检查该值是否包含在List中:

--- First way -第一种方式

Using a List of integers: 使用整数列表:

import java.util.Scanner;
import java.util.List;
import java.util.ArrayList;

public class InArrayWithAsListContains {

    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        int value=0;
        List<Integer> values = new ArrayList<Integer>();

        values.add(16);
        values.add(10);
        values.add(11);
        values.add(14);
        values.add(17);    

        do {
            System.out.println("Enter a number:");
            value = input.nextInt();    
        } while(!values.contains(value)); 
    }
}

A variation to work with a List starting with int[]: 与以int []开头的List一起使用的变体

int[] arr = new int[] {16,10,11,14,17};        

// int[] into List<Integer> conversion
for (int i : arr) values.add(i);

--- Second way -第二种方式

Just using arrays and Arrays.binarySearch(values, key) 仅使用数组和Arrays.binarySearch(values,key)

import java.util.Scanner;
import java.util.Arrays;

public class Program {

    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        int value=0;
        int[] values = {16,10,11,14,17};

        Arrays.sort(values);

        do {
            System.out.println("Enter a number:");
            value = input.nextInt();    
        } while(Arrays.binarySearch(values, value) < 0); 
    }
}

一个int与一个int[]

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

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