标签: while-loop

中断从循环调用的方法

我正在重构一个非常大的方法,里面有很多重复.

在该方法中有许多while循环,包括:

    if ( count > maxResults){
        // Send error response
        sendResponse(XMLHelper.buildErrorXMLString("Too many results found, Please refine your search"), out, session);
        break;
Run Code Online (Sandbox Code Playgroud)

我想把它作为一种方法提取,因为它在当前的这个方法中发生了3次,但是当我这样做时,我在中断时得到一个错误,因为它不再在循环中.问题是仍然需要打破while循环,但仅在达到最大结果数时.

有什么建议?

java methods break while-loop

5
推荐指数
1
解决办法
359
查看次数

J:为什么`f ^:proposition ^:_ y`代表一个while循环?

正如标题所说,我不明白为什么f^:proposition^:_ y是一个while循环.我实际上已经使用了几次,但我不明白它是如何工作的.我得到了^:重复函数,但我对它在该语句中的双重用法感到困惑.

我也无法理解为什么f^:proposition^:a: y有效.这与前一个迭代相同,但返回所有迭代中的值,而不是上一个迭代中的最后一个值.

a:是一个空盒子,我得到的具有特殊含义,^:但即使在查看字典后,我无法理解它.

谢谢.

j while-loop

5
推荐指数
2
解决办法
179
查看次数

当我输入"完成"时,我的程序不会停止

我之前问了一个问题,从那以后我编辑了我的代码,但现在我的代码不会停止,当我读完它不会停止.

public class Done {

    public static void main(String[] args){

       Scanner kb = new Scanner(System.in);
       ArrayList<String> sal = new ArrayList<String>();
       int count = 0;
       while (true){
             sal.add(kb.next());
             if (sal.equals("done"))
                 break;
             count++;
      }
      display(sal);
      displayb(sal);
    }

    public static void display(ArrayList<String> sal){

       for (int i=0; i<sal.size(); i++)
            System.out.print(sal.get(i)+ " ");
       System.out.println();
     }

    public static void displayb(ArrayList<String> sal){

       for (int z = sal.size(); z >= 1; z--)
            System.out.print(sal.get(z-1) + " ");
       System.out.println();
    }
 }
Run Code Online (Sandbox Code Playgroud)

当我输入短语"done"时,我的代码不会停止.谁知道我可能做错了什么?

java for-loop arraylist while-loop java.util.scanner

5
推荐指数
1
解决办法
285
查看次数

在Bash的'while'循环中使用'if'

我将差异结果保存到文件中:

bash-3.00$ cat /tmp/voo
18633a18634
> sashabrokerSTP
18634a18636
> sashatraderSTP
21545a21548
> yheemustr
Run Code Online (Sandbox Code Playgroud)

我真的需要登录:

bash-3.00$ cat /tmp/voo | egrep ">|<"
> sashaSTP
> sasha
> yhee
bash-3.00$
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试迭代下摆并打印名称时,我会收到错误.我只是不明白使用"if"和"while循环"的基本原理.最终,我想使用while循环,因为我想对行做一些事情 - 显然一次while只加载一行到内存中,而不是一次整个文件.

bash-3.00$ while read line; do  if [[ $line =~ "<" ]] ; then  echo $line ; fi ;  done <  /tmp/voo
bash-3.00$
bash-3.00$
bash-3.00$ while read line; do  if [[ egrep "<" $line ]] ; then  echo $line ; fi ;  done    <  /tmp/voo
bash: conditional …
Run Code Online (Sandbox Code Playgroud)

bash loops if-statement while-loop

5
推荐指数
2
解决办法
4万
查看次数

定时循环不终止

运行此代码,我希望它将测试变量增加5秒然后完成.

import java.util.Timer;
import java.util.TimerTask;

public class Test {
    private static boolean running;

    public static void main( String[] args ) {
        long time = 5 * 1000;       // converts time to milliseconds
        long test = Long.MIN_VALUE;
        running = true;

        // Uses an anonymous class to set the running variable to false
        Timer timer = new Timer(); 
        timer.schedule( new TimerTask() { 
            @Override
            public void run() { running = false; }
        }, time );

        while( running ) {
            test++;
        }

        timer.cancel();
        System.out.println( …
Run Code Online (Sandbox Code Playgroud)

java timer while-loop timertask

5
推荐指数
1
解决办法
91
查看次数

如何在while循环中生成非重复随机数?(Python 3)

到目前为止,我所拥有的是

import random
def go():
    rounds = 0
    while rounds < 5:
        number = random.randint(1, 5)
        if number == 1:
            print('a')
        elif number == 2:
            print('b')
        elif number == 3:
            print('c')
        elif number == 4:
            print('d')
        elif number == 5:
            print('e')
        rounds = rounds + 1
go()
Run Code Online (Sandbox Code Playgroud)

并且输出结束了生物的某些东西

e
e
c
b
e
Run Code Online (Sandbox Code Playgroud)

如何制作一个数字只使用一次,字母不重复?(例如某事)

a
e
b
c
d
Run Code Online (Sandbox Code Playgroud)

提前致谢

python random while-loop python-3.x

5
推荐指数
1
解决办法
1262
查看次数

为什么我的while循环停止了?

我一直在阅读其他帖子,但无法理解.无论我在重复结束时输入什么,它总是重复循环.我试过while(repeat != "Quit" or repeat != "quit" or repeat != "b" or repeat != "B" or repeat != "no" or repeat != "No"):但它仍然没有奏效.我不知道还有什么可以尝试的.这个while循环有什么问题?

    repeat = "d"
    print "Please answer questions using the choices (A, B, C, etc.)"
    time.sleep(2.1738)
    while repeat != "Quit" or repeat != "quit" or repeat != "b" or repeat != "B" or repeat != "no" or repeat != "No":
        print "A) Round Edges"
        print "B) Straight Edges"
        Edges1 = raw_input("Does the shape have round edges …
Run Code Online (Sandbox Code Playgroud)

python while-loop

5
推荐指数
1
解决办法
102
查看次数

如何在循环时退出此操作?

我在退出以下循环时遇到问题.这是一个简单的程序,hello如果随机值大于5则打印.程序运行良好一次但是当我尝试再次运行它时会进入无限循环.

    from random import *

    seed()
    a = randint(0,10)
    b = randint(0,10)
    c = randint(0,10)
    count = 0

    while True:
        if a > 5 :
           print ("aHello")
           count = count + 1
        else : 
           a = randint(0,10)
        if b > 5 : 
           print ("bHello")
           count = count + 1
        else :
           b = randint(0,10)
       if c > 5 :
           print ("cHello")
           count = count + 1
       else :
           c = randint(0,10)

      if count == 20 :
           count = …
Run Code Online (Sandbox Code Playgroud)

python loops while-loop

5
推荐指数
2
解决办法
119
查看次数

while(true)循环没有中断

我是Java编程的新手.目前我正在研究一个名为Evercam的Android应用程序的源代码.但是,我在理解涉及while(true)循环的代码的一部分时遇到了问题.

这是代码的片段:

while (true)
{
    while (true)
    {
        byte[] responseMessageByteArray = new byte[4000];
        DatagramPacket datagramPacketRecieve = new DatagramPacket(responseMessageByteArray, responseMessageByteArray.length);
        datagramSocket.receive(datagramPacketRecieve);
        String responseMessage = new String(datagramPacketRecieve.getData());
        EvercamDiscover.printLogMessage("\nResponse Message:\n" + responseMessage);
        StringReader stringReader = new StringReader(responseMessage);
        InputNode localInputNode = NodeBuilder.read(stringReader);
        EnvelopeProbeMatches localEnvelopeProbeMatches = (EnvelopeProbeMatches)(new Persister()).read(EnvelopeProbeMatches.class, localInputNode);
        if (localEnvelopeProbeMatches.BodyProbeMatches.ProbeMatches.listProbeMatches.size() > 0)
        {
            ProbeMatch localProbeMatch = (ProbeMatch) localEnvelopeProbeMatches.BodyProbeMatches.ProbeMatches.listProbeMatches.get(0);
            if (uuidArrayList.contains(localProbeMatch.EndpointReference.Address))
            {
                EvercamDiscover.printLogMessage("ONVIFDiscovery: Address " + localProbeMatch.EndpointReference.Address + " already added");
            }
            else
            {
                uuidArrayList.add(localProbeMatch.EndpointReference.Address);
                DiscoveredCamera discoveredCamera = getCameraFromProbeMatch(localProbeMatch);
                if (discoveredCamera.hasValidIpv4Address())
                {
                    this.onActiveOnvifDevice(discoveredCamera);
                    cameraList.add(discoveredCamera);
                } …
Run Code Online (Sandbox Code Playgroud)

java android infinite-loop while-loop

5
推荐指数
1
解决办法
991
查看次数

加速必须遍历整个列表的Python代码

我有一个问题,我需要(至少可以肯定)通过整个列表来解决.问题是找出列表中连续数字的最大数量,该数字加起来列表中的另一个(更大)元素.如果没有,那么我们只需将列表中的最大值作为候选求和,将1作为最大连续元素数.

我的通用代码有效,但对于大型列表(> 500,000个元素)则不太好.我只是在寻找有关如何以不同方式解决问题的提示.我目前的做法:

L = [1,2,3,4,5,6,7,8,9,10]
candidate_sum = L[-1]
largest_count = 1
N = len(L)
i = 0

while i < N - 1:
    s = L[i]
    j = 0
    while s <= (N - L[i + j + 1]):
        j += 1
        s += L[i+j]
        if s in L and (j+1) > largest_count:
             largest_count = j+1
             candidate_sum = s
    i+=1
Run Code Online (Sandbox Code Playgroud)

在这种情况下,答案是[1,2,3,4],因为它们加起来为10,长度为4(显然这个例子L是一个非常简单的例子).

然后我通过将初始while循环条件更改为:

while i < (N-1)/largest_count
Run Code Online (Sandbox Code Playgroud)

这不是一个很好的假设,但基本认为数字的分布有些统一,因此列表后半部分的两个数字平均大于列表中的最终数字,因此被取消资格.

我只是在寻找:

  • 可能的瓶颈
  • 关于尝试不同方法的建议

python list while-loop python-3.x

5
推荐指数
1
解决办法
276
查看次数