标签: while-loop

有没有理由用C++中的for(; condition;)替换while(condition)?

好像

while( condition ) {
    //do stuff
}
Run Code Online (Sandbox Code Playgroud)

完全等同于

for( ; condition; ) {
    //do stuff
}
Run Code Online (Sandbox Code Playgroud)

有没有理由使用后者而不是前者?

c++ loops for-loop while-loop

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

中断从循环调用的方法

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

在该方法中有许多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
查看次数

在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万
查看次数

使用console.log进行经典ASP调试

我正在看一个用经典ASP编写的网页,我正处于'while'语句的中间.我想使用Chrome开发者工具调试页面上的值.我试过插入

console.log(value);
Run Code Online (Sandbox Code Playgroud)

它适用于C#应用程序,但它Expected end of statement在console.log我插入的行中抛出了一个.

我只是在寻找关于如何使用经典asp调试器的快速提示?

debugging while-loop asp-classic console.log

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

循环遍历文件并在C中打印文件属性

我是C语言编程的新手.我需要这个程序遍历文件夹中的所有文件并为每个文件打印这些属性.此时它正在打印文件夹的属性.

#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <dirent.h>

int main(int argc, char *argv[])
{
    DIR *dp;
    struct stat file_stats;

    if (argc != 2) {
        fprintf(stderr, "Usage: fstat FILE...\n");
        return EXIT_FAILURE;
    }

    if ((stat(argv[1], &file_stats)) == -1) {
        perror("fstat");
        return EXIT_FAILURE;
    }

    dp = opendir("./");
    if (dp == NULL) {
        perror("couldn't open directory");
        return EXIT_FAILURE;
    }

    while (readdir(dp)) {
        printf("filename: %s\n", argv[1]);
        printf(" device: %lld\n",
                file_stats.st_dev);
        printf(" protection: %o\n",
                file_stats.st_mode);
        printf(" number of hard links: %d\n", …
Run Code Online (Sandbox Code Playgroud)

c linux posix while-loop

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

定时循环不终止

运行此代码,我希望它将测试变量增加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
查看次数