java: array if all the same

后端 未结 1 873
逝去的感伤
逝去的感伤 2021-02-08 16:31

I have a array:

int tarningar[] = new int[5];

That holds numbers. When all the numbers are the same, system.out.println(\'ok\');

How ca

1条回答
  •  天涯浪人
    2021-02-08 17:01

    boolean flag = true;
    int first = tarningar[0];
    for(int i = 1; i < 5 && flag; i++)
    {
      if (tarningar[i] != first) flag = false;
    }
    if (flag) System.out.println("ok");
    

    0 讨论(0)
提交回复
热议问题