我听说有些“休息”不是坏习惯。这个如何? [英] I've heard that some "break"s aren't bad practice. What about this one?

查看:58
本文介绍了我听说有些“休息”不是坏习惯。这个如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常听到在Java中使用 break s被认为是不好的做法,但是在阅读了Stack Overflow上的一些线程之后,我发现不是这样。许多人说这在某些情况下是可以接受的。

I have often heard that using breaks in Java is considered bad practice, but after reading some threads on Stack Overflow, I've seen otherwise. Many say that it is acceptable in certain cases.

在这种情况下,对什么是/不是不好的做法我有些困惑。

I'm a little confused as to what is/isn't bad practice in this case.

对于欧拉计画:问题7,我构建了以下代码。挑战是找到第10001个素数。

For Project Euler: Problem 7, I've constructed the code below. The challenge was to find the 10001st prime.

int index = 2, count = 1, numPrime = 1;

while (true) {
    index++;

    if (isPrime(index)) {
        count = index;
        numPrime++;
    }

    if (numPrime >= 10001)
        break;
}

System.out.println(count);

这将返回正确的答案(以21ms为单位),但是我是否忽略了严重警告?可以不间断地创建一个while循环,这是100%的可能性,但是我发现这样做比较容易。

This returns the correct answer (in 21ms), but am I overlooking a serious warning? It's 100% possible to create a while loop without a break, but I find that this is a little easier to follow.

这是我使用<$ c $的方式c> break; 不良做法?我知道总有一种方法可以使用它,但这真的很可怕吗?

Is the way I use the break; bad practice? I know that there's always a way around using one, but is it really that terrible here?

非常感谢

Justian

编辑

这是我的isPrime()代码。

Here's my isPrime() code. I might as well optimize this while I'm at it.

public static boolean isPrime(long num) {  
    if (num == 2)
        return true;

    if (num % 2 == 0 || num <= 0)
        return false;

    for (long i = 3; i * i <= num; i += 2)
        if (num % i == 0)
            return false;

    return true;
}


推荐答案

在这种情况下,对我来说,只需更改 while 条件会更容易:

In this case, it looks to me like it would be easier just to change the while condition:

while (numPrime < 10001) {

通常是这种情况while(true)循环结束

if (condition)
{
    break;
}

...尽管您需要检查主体中是否还有其他东西循环执行继续

... although you need to check whether anything else in the body of the loop performs a continue.

或者,您也可以对其进行轻微的重组:

Alternatively, you could restructure it slightly:

int current = 1;
for (int i = 0; i < 10001; i++)
{
    current++;
    while (!isPrime(current))
    {
        current++;
    }
}

然后 current

Then current will be the answer at the end.

相对于while循环,我通常更喜欢 for 循环,当您尝试执行特定次数的操作时。在这种情况下,某物就是找到下一个素数。

I generally prefer a for loop over a while loop, when you're trying to do something a particular number of times. In this case the "something" is "find the next prime".

编程中有很多教条,我觉得太过分了-包括一个出口指向方法和请勿使用中断。尽力编写代码。如果您看了一些代码,但感觉并不十分清楚正在发生的事情,请尝试制定其他结构化方法。有时是改变循环的情况;有时是提取一种方法;有时候,这会颠倒一些逻辑(首先用负分支进行交易,可能提早退出,然后再处理正常情况)。

There are various bits of dogma in programming that I feel are taken too far - including "one exit point to a method" and "don't use break." Write code as readably as you can. If you look at some code and feel it's not blindingly obvious what's going on, try to work out other ways of structuring it. Sometimes that's a case of changing a loop; sometimes it's extracting a method; sometimes it's inverting some logic (deal with a negative branch first, possibly exiting early, and then handle the normal case).

这篇关于我听说有些“休息”不是坏习惯。这个如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆