简体   繁体   English

如何使用一种方法检查要添加到数组中的新元素是否尚未添加?

[英]How can I use a method to check if the new element that I want to add to my array hasn't already been added?

Im working on a school project and I am suppose to create a program that will display a 2 x 2 grid of numbers (randomly generated from 1 - 16). 我正在做一个学校项目,我想创建一个程序,该程序将显示2 x 2的数字网格(从1-16随机生成)。 This program will then sort it out step by step. 然后,该程序将逐步对其进行分类。

I am supposed to have a logic check class. 我应该有一个逻辑检查类。

I'm trying to make a method where it will take the value that the main() program wants to add, check to see if its already in the array or not, and return false if that number is already there or true if not. 我正在尝试创建一个方法,该方法将采用main()程序要添加的值,检查其是否已经在数组中,如果该数字已经存在,则返回false,否则返回true。

class LogicStatementsV2 {
    //Declare constants
    private final int LENGTH_OF_INDEX = 16;
    //Create an array of how the index of the array should look like
    int[] finalIndex = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
    //Creates an array of length 16 which will be filled as the main program uses a random number generator.
    int[] buildIndex = new int[LENGTH_OF_INDEX];

    /**Method that is called to add a random number if that number hasnt been entered    
    * @param n The integer that the main class will generate from a random number generator
    * @return wether that number is already in the array or not
    */
    public boolean addIfValid(int n) {
        for (int i = 0; i < 16; i++) {
            if (n == buildIndex[i]) return false;
            else return true;
        }
    }
}

For some reason the first to last } keeps saying that a return value is required. 由于某种原因,第一个}一直在说需要返回值。

What am I doing wrong? 我究竟做错了什么?

The else return true; else return true; doesn't belong in the loop. 不属于循环。 Only once you've looped through all the elements and found no duplicates can you safely return true: 只有遍历所有元素并且没有发现重复项,您才能安全地返回true:

public boolean addIfValid(int n) {
    for (int i = 0; i < 16; i++) {
        if (n == buildIndex[i]) return false;
    }
    return true;
}

暂无
暂无

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

相关问题 如何添加新整数来替换旧整数到我已经存在的数组中? - How can I add new integers to replace the old integers to my already-existing Array? 为什么不能使用array的元素调用其方法? - Why can't I use an element of array to call its method? 我创建了十进制到二进制方法,我想在2D数组中使用它,但是我不能 - I created decimal to binary method and I want to use it in 2D array but I can't Java:如何检查数组中的元素是否已初始化? - Java: How do I check if an element in an array has been initialized? 如何使用方法返回数组元素的索引? - How can I use a method to return the index of an array element? Java 如何将元素添加到我的数组中? - Java How can i add an element into my array? 如何使用Hamcrest检查双精度数组中的每个元素是否“接近”另一个数组中的每个元素? - How can I use Hamcrest to check if each element in an array of doubles is “close” to each element in another array? 如何在每种方法中使用对象数组? - How can I use my object array at every method? 我想检查是否有新文件添加到 firebase 存储中 - I want to check whether a new file is added to firebase storage or not 如何检查字符串数组的长度? 我不想使用 lenght() 因为我需要知道数组中有多少个字符串 - How to check the lenght of an array of strings? I don't want to use lenght() because I need to know how many strings are in the array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM