标签: while-loop

循环中的回声/打印问题

我需要修复此问题.
这只是我代码的一部分

<?php
$number = 30;
while($number > 0) {
 $number--;
 sleep(30);
 print "$number . Posted<br>";

}
?> 
Run Code Online (Sandbox Code Playgroud)

循环中的循环过程实际上要大得多,我只是把重要的东西.

无论如何,你可以看到它应该打印
30张贴
(等待30秒)
29张贴
(等待30秒)
28张贴
(等待30秒)

但是它等待直到循环结束,然后立即打印所有.有没有解决这个问题?我在想一个ajax方法,但我不知道.

php ajax loops echo while-loop

6
推荐指数
1
解决办法
4382
查看次数

C# - while循环中的foreach循环 - 打破foreach并立即继续while循环?

while (foo() == true)
{
   foreach (var x in xs)
   {
       if (bar(x) == true)
       {
           //"break;" out of this foreach
           //AND "continue;" on the while loop.
       }
   }

   //If I didn't continue, do other stuff.
}
Run Code Online (Sandbox Code Playgroud)

我有点坚持如何做到这一点.


更新:我修正了问题.我遗漏了一个事实,即如果我不在continue;while循环上调用,我需要处理其他内容.

对不起,我没有意识到我曾两次使用"某事"这个词.

c# for-loop continue break while-loop

6
推荐指数
3
解决办法
8726
查看次数

6
推荐指数
1
解决办法
343
查看次数

while循环使用if语句比while循环更快

我正在做一些关于循环中if语句的速度及其对速度的影响的测试.我发现的东西始终如一,if语句提高了性能.我的代码:

import time
t = time.time

start = t()
x = 0
while x < 10000000:
    x += 1
time1 = t()
x = 0
while x < 10000000:
    x += 1
    if True:
        pass
time2 = t()

print(start)
print(time1 - start) # Time for simple while-loop
print(time2 - time1) # Time for while+if
Run Code Online (Sandbox Code Playgroud)

示例输出将是:

1355517837.993
1.7850000858306885
1.7209999561309814
Run Code Online (Sandbox Code Playgroud)

这完全是违反直觉的.while-if-loop的工作速度比标准的while循环要快得多.几乎每次我运行时都会发生这种情况; 也许每20次中有1次需要更长时间.有谁知道为什么?

python performance if-statement while-loop python-3.x

6
推荐指数
2
解决办法
1602
查看次数

在Python中循环退出

在下面的代码中,我希望while循环在a+ b+ c=时立即退出1000.但是,使用print语句进行测试表明它只会持续到for循环完成.我已经尝试过while True然后在if语句集中False但是会导致无限循环.我认为使用x = 0然后设置x = 1可能会工作,但这也只是运行,直到for循环完成.什么是最优雅,最快速的退出方式?谢谢.

a = 3
b = 4
c = 5
x = 0
while x != 1:
    for a in range(3,500):
        for b in range(a+1,500):
            c = (a**2 + b**2)**0.5
            if a + b + c == 1000:
                print a, b, c
                print a*b*c
                x = 1
Run Code Online (Sandbox Code Playgroud)

python loops while-loop

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

循环 - 进程替换与此处字符串与命令替换

有人可以解释这两个while循环之间的区别:

while read test; do
echo $test
done <<< "$(seq 5)"
Run Code Online (Sandbox Code Playgroud)

-

while read test; do
echo $test
done < <(seq 5)
Run Code Online (Sandbox Code Playgroud)

bash while-loop

6
推荐指数
1
解决办法
2070
查看次数

使用按钮停止并启动循环

我试图控制我的程序中的while循环停止和启动基于用户输入.我用一个按钮尝试了这个,它的"开始"部分工作,但然后代码进入无限循环,我不能手动终止它.以下是我的所有代码: Header Class

package test;

    import javax.swing.JFrame;

    public class headerClass {
        public static void main (String[] args){
            frameClass frame = new frameClass();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(150,75);
            frame.setVisible(true);
        }
    }
Run Code Online (Sandbox Code Playgroud)

框架类

package test;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class frameClass extends JFrame {

    private JButton click;

    public frameClass(){
        setLayout(new FlowLayout());
        click = new JButton("Stop Loop");
        add(click);

        thehandler handler = new thehandler();
        click.addActionListener(handler);
    }

    private class thehandler implements ActionListener{
        public void actionPerformed(ActionEvent e){
            if(e.getSource()==click){
                looper loop = new looper();
                looper.buttonSet = !looper.buttonSet; …
Run Code Online (Sandbox Code Playgroud)

java swing button while-loop

6
推荐指数
1
解决办法
2623
查看次数

使用花括号来隔离我想在C++中多次使用的变量

在下面的代码中,我有一个while语句用于确保输入字符串少于10个字符.我已经声明了一个bool被叫cont,我用它来告诉while循环在我的条件满足后停止.

#include "stdafx.h"
#include <iostream>
#include <string>

int main()
{
    using namespace std;

    cout << "Enter a string less than 10 characters long: ";

    string teststring;

    {
        bool cont(false);

        //if input is 10 or more characters, ask for input again until it is less
        while (!cont)
        {
            getline(cin, teststring);

            if (teststring.length() >= 10)
            {
                cout << "Too long, try again: ";
            }
            else
            {
                cout << "Thank you.\n\n";
                cont = true;
            }

        }
    }

    return 0; …
Run Code Online (Sandbox Code Playgroud)

c++ loops curly-braces while-loop

6
推荐指数
1
解决办法
132
查看次数

Halide - while循环等效

我正在尝试在Halide中实现Meijster距离变换算法.我已经将此代码重写为C++(使用openCV)并且它工作正常.关于这个算法的论文就在这里.现在我的卤化物代码已完成50% - 第一阶段工作正常,现在我遇到了阶段2(链接代码中的扫描3)的问题,其中(简化)看起来像这样:

//g is 2 dimensional cv::Mat (something like array) - result of previous stage
// m is g.width and n is g.height
int(*functionF)(int x, int i, int g_i) = EDT_f;
int(*functionSep)(int i, int u, int g_i, int g_u, int max_value) = EDT_Sep;
cv::Mat dt = cv::Mat(n, m, CV_32SC1);
int* s = new int[m];
int* t = new int[m];
int q = 0, w;

for (int y = 0; y<n; y++)
{
    q = 0; …
Run Code Online (Sandbox Code Playgroud)

c++ opencv image-processing while-loop halide

6
推荐指数
1
解决办法
914
查看次数

while循环中的setTimeout()方法

我在这里阅读了w3schools和其他类似问题的相关页面,但似乎无法理解下面的内容有什么不对:

var myfunc03 = function (i) {
  document.getElementById('d01').innerHTML += 100-i+"<br>";
};

var myFunc01 = function() {
  i=0;
  while (i<100) {
    setTimeout(myfunc03(i), 1000)
    i++;
  }
};
Run Code Online (Sandbox Code Playgroud)

何时myFunc01();运行.

没有任何暂停,i的所有可能值都会立即列出.

这里有逻辑错误吗?

javascript while-loop settimeout

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