簡體   English   中英

將int與數組java進行比較

[英]comparing an int with an array java

我正在開發一個項目,讓我掃描數字1-9然后將它們存儲在一個數組中,同時檢查重復。 我可以得到除了我必須檢查重復的部分以外的所有內容。 有一種簡單的方法可以將1個數字與整個數組進行比較嗎?

ex. int[] array = new int[9];
        array[0] = 0;
        array[1] = 1;
        array[2] = 2;
        array[3] = 3;
        array[4] = 4;



then user inputs 3, how can I check the entire array for 3?

如果您只是想要一種方法來查看數組是否包含問題中所述的數字3,您可以添加一個簡單的for循環。

 int[] array = new int[9];
    array[0] = 0;
    array[1] = 1;
    array[2] = 2;
    array[3] = 3;
    array[4] = 4;

    int num = 3;


    for(int i = 0; i < array.length; i++){

        if(num==array[i]){
            System.out.println("found at index: " + i);
        }

您可以輕松地對其進行調整以讀取用戶的值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM