在数组java中搜索数字

时间:2016-01-27 21:06:50

标签: java arrays search random

我的代码首先生成包含100个元素的数组,然后在每个元素中放置随机生成的数字。我正在尝试搜索一个数字,如果找到,打印出它的索引。我到目前为止的代码是:

import java.util.Scanner;

public class Lab01 
{

    public static void main(String[] args) 
    {
        int[] nums = new int[100];

        for (int i = 0; i < nums.length; i++)
        {
            nums[i] = (int)((Math.random() * 100) + 1);
            System.out.print(nums[i] + " , ");
        }
     System.out.println();
     Scanner input = new Scanner(System.in);
     int num;
     System.out.println("What number would you like to search for?");
     num = input.nextInt();
     boolean found = false;        
     for (int i = 0; i < nums.length; i++) 
        {
            if (num == nums[i]) 
            {              
               found = true;
               break;
            }

            if (found)
            {
                System.out.println("That number was found at index" + i);
                break;
            }
            else
            {
                System.out.println("That number was not found.");
                break;
            }
        }       
    }
}

我输入了print语句以查看值,因此我可以验证它是否正常工作,但它总是返回&#34; Not found&#34;。我在这里缺少什么?

4 个答案:

答案 0 :(得分:3)

尝试替换此块,请参阅底部的说明:

With xlSheet
    Set range = .Range("A1:L1")
    Sheets("xyz").Select

     range.FormatConditions.Add Type:=xlExpression, Formula1:="=AND($I1>=TODAY(),($I1<(TODAY()+30)))"
    rng.FormatConditions.Add Type:=xlTextString, String:="Focus", TextOperator:=xlContains


        With rng.FormatConditions(1).Interior
            .Color = Any color            
        End With


End With

使用:

     for (int i = 0; i < nums.length; i++) 
    {
        if (num == nums[i]) 
        {              
           found = true;
           break;
        }

        if (found)
        {
            System.out.println("That number was found at index" + i);
            break;
        }
        else
        {
            System.out.println("That number was not found.");
            break;
        }

<强>解释
放出 int i; // create this for ( i = 0; i < nums.length; i++) // and remove int from for loop { if (num == nums[i]) { found = true; break; } } if (found) { System.out.println("That number was found at index " + i); } else { System.out.println("That number was not found."); } 两个for loop并从中删除if condtion语句,然后在break之前创建一个int i = 0

答案 1 :(得分:1)

检查完第一个号码后,您正在退出循环,因此如果第一个号码不匹配,则打印&#34;未找到该号码&#34;。如果第一个数字匹配,则在不打印任何内容的情况下中断。你应该只打印&#34;找不到那个号码&#34;检查完所有数组后的数字。

您的if语句应该在for循环之后,而不是在其中。

int i = 0;
for (; i < nums.length; i++) {
    if (num == nums[i]) {              
        found = true;
        break;
    }
}
if (found) {
    System.out.println("That number was found at index" + i);
} else {
    System.out.println("That number was not found.");
}

答案 2 :(得分:0)

尝试一下:)

table_1.column_1 = 2

答案 3 :(得分:0)

public class Contains {

    public static void main(String[] args) {
        int[] num = {1, 2, 3, 4, 5};
        int toFind = 3;
        boolean found = false;

        for (int n : num) {
            if (n == toFind) {
                found = true;
                break;
            }
        }

        if(found)
            System.out.println(toFind + " is found.");
        else
            System.out.println(toFind + " is not found.");
    }
}