集册 Java实例教程 逐一搜索数组元素

逐一搜索数组元素

欢马劈雪     最近更新时间:2020-01-02 10:19:05

447
逐一搜索数组元素

You could just use a for loop, like this:


import java.util.Arrays;
/**来自 时代Java - N o w  J a v a . c o m**/

public class Main {

  public static void main(String[] args) {


    int[] lotto = new int[6];


    for (int i = 0; i < 6; i++)

      lotto[i] = (int) (Math.random() * 100) + 1;

    

    Arrays.sort(lotto);

    

    int lucky = 13;

    int foundAt = -1;

    

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

      if (lotto[i] == lucky)

        foundAt = i;//来自 时代Java - nowjava.com


    if (foundAt > -1)

      
展开阅读全文