有没有更加优雅的解决方案来处理这个简单的循环?

huangapple go评论38阅读模式
英文:

Is there any more elegant solution for this simple loop?

问题

Hello I'm new to programming, I'm trying to solve a problem in which I'm given an array of 10 random numbers and I have to say whether the number is in the array.

If it is - then the program has to show the message "I found it!", and if it isn't - "That number is not in the array!"

I wrote this solution and I would like to know if there is a simpler and faster way to solve it (language Java)

input: one number and an array of numbers

int cont = 0;

for (i = 0; i < 10; i++) {
  if (numbers[i] == number) {
    System.out.println("I found it");
    cont++;
    break;
  }
}
if (cont == 0)
  System.out.println("That number is not in the array!");

Thank you for helping, sorry if it's really simple, but I'm a beginner.

英文:

Hello I'm new to programming, I'm trying to solve a problem in which I'm given an array of 10 random numbers and I have to say whether the number is in the array.

If it is - then the program has to show the message "I found it!", and if it isn't - "That number is not in the array!"

I wrote this solution and I would like to know if there is a simpler and faster way to solve it (language Java)

input: one number and an array of numbers

int cont=0;

for(i=0; i&lt;10;i++){
  if(numbers[i]==number){
    System.out.println(&quot;I found it&quot;);
    cont++;
    break;
  }
}
if(cont==0)
  System.out.println(&quot;That number is not in the array!&quot;);

Thank you for helping, sorry if it's really simple, but I'm a beginner.

答案1

得分: 1

尝试像这样(Java 8及更高版本):

boolean contains = IntStream.of(numbers).anyMatch(x -> x == number);
if (contains)
    System.out.println("我找到了它");
else
    System.out.println("数组中没有那个数字!");
英文:

try like this (Java 8 and later versions):

boolean contains = IntStream.of(numbers).anyMatch(x -&gt; x == number); 
if(contains)
   System.out.println(&quot;I found it&quot;);
else
   System.out.println(&quot;That number is not in the array!&quot;);

答案2

得分: 0

Using Streams everywhere, just because of they are streams, isn't a good idea and doesn't make code cleaner, nor does it always make it more readable or even more faster.

Your loop seems good and I don't think you can improve the performance here. The only thing I'd have changed is, that I'd have introduced the found flag, it's a bit less code.

boolean found = false;

for (i = 0; i < 10; i++) {
if (numbers[i] == number) {
System.out.println("I found it");
found = true;
break;
}
}
if (!found)
System.out.println("That number is not in the array!");

英文:

Using Streams everywhere, just because of they are streams, isn't a good idea and doesn't make code cleaner, nor does it always make it more readable or even more faster.

Your loop seems good and I don't think you can improve the performance here. The only thing I'd have changed is, that I'd have introduced the found flag, it's a bit less code.

boolean found=false;

for(i=0; i&lt;10; i++) {
    if(numbers[i]==number) {
        System.out.println(&quot;I found it&quot;);
        found=true;
        break;
    }
}
if(!found)
    System.out.println(&quot;That number is not in the array!&quot;);

huangapple
  • 本文由 发表于 2020年7月28日 05:24:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/63123782.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定