标签: while-loop

如何收集数组中所有真值的键?

这是 PHP 文档中代码的略微修改版本:

http://php.net/manual/en/function.next.php

<?php

$array = array(
    -1 => true,
    0 => false,
    1 => true
);

while ($bet = current($array)) {
    if ($bet == true) {
        echo key($array) . '<br />';
    }
    next($array);
}
?>
Run Code Online (Sandbox Code Playgroud)

这是尽可能接近我想要的。

这与“-1”相呼应,我明白为什么。

我想知道的是如何修改此代码,以便它即使在遇到 false(而不仅仅是 3 个值的数组)后仍会继续搜索“true”值。

我意识到这可能不是最好的方法,我愿意接受建议。

提前致谢!!

特鲁法

php arrays function while-loop

0
推荐指数
1
解决办法
452
查看次数

+=、-=、*= 和 /= 在 Python 中代表什么?

做什么

+=、-=、*= 和 /=

在 Python 中代表什么?以及如何将它用于while循环?

python loops while-loop

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

如何遍历文件并用行号回显每一行

如何在 bash 中循环遍历文件并回显行号和行号?

我有这个,它除了行号之外还有所有内容:

while read p;
do
echo "$p" "$LINE";
done < file.txt
Run Code Online (Sandbox Code Playgroud)

谢谢你的帮助!

编辑这将使用 xargs 多线程运行,所以我不想使用计数器。

unix linux bash while-loop

0
推荐指数
1
解决办法
2637
查看次数

Python:当 raw_input 无效时重复一个函数

我希望能够确定输入是否有效。我有:

servqual = raw_input(">").lower()
while servqual != "great" or "good" or "lacking" or "poor":
    print "I didn't understand that. Please try again."
    servqual = raw_input(">").lower()
Run Code Online (Sandbox Code Playgroud)

但是,每当我在循环中运行它时,它总是假定为 True,即使我输入了有效的答案。我查看了不同的答案,但似乎没有一个在这种情况下有效。

python raw-input while-loop

0
推荐指数
1
解决办法
415
查看次数

While 循环不循环 - Ruby

当 str[0] 是 "a" 并且 str[4] 是 "b" 时,我得到了正确的结果。但是,当“a”在另一个位置并且与“b”隔开三个空格时,我会出错。

任何帮助都会很棒!

def ABCheck(str)

  str.downcase.split()
  x = 0
  while x < str.length - 4
    return true if ((str[x] == "a") && (str[x + 4] =="b"))
      x += 1
    return false

  end     
end

puts ABCheck("azzzb")
#Currently puts "true"
puts ABCheck("bzzabzcb")
#Currently puts "false" - even though it should print true
Run Code Online (Sandbox Code Playgroud)

ruby while-loop

0
推荐指数
1
解决办法
61
查看次数

这个while循环导致我的程序挂起

我有一个导致程序挂起的函数。我已经注释掉了该功能,其他所有功能都运行正常。程序到达循环应结束的位置,并且仅等待输入。isBomb()函数只是一个获取器,它返回一个true / false值。该功能是扫雷游戏的一部分。我正在尝试找出一种方法来找出所选单元格附近有多少枚炸弹。我可以发布整个程序,但是大约250-350行。makeNum方法是一个简单的getter,它将单元格号设置为等于参数的值。拒绝我投票之前,请让我知道是否有问题。我尝试搜索答案,但被卡住了。

void mazeDisplay::countBombAdj(int row, int col) {
    int counter = 0;
/*  for (int x = row - 1; x < row + 1; x++) {
        while ((x > - 1) && (x < 4)) {
            for (int y = col - 1; y < col + 1; y++) {
                while ((-1 < y) && (y < 4)) {
                    if (mazeCells[x][y].isBomb() == true)
                        counter += 1;
                }
            }
        }
    }*/

    mazeCells[row][col].makeNum(counter);
}
Run Code Online (Sandbox Code Playgroud)

c++ loops while-loop data-structures

0
推荐指数
1
解决办法
90
查看次数

为什么循环不断?

我正在做一些作业,试图使它免受字符输入的影响,但是以某种方式在其中写入字符仍然会破坏字符,因为这会使循环无限。谁能建议一种解决方法?这是代码:

#include <iostream>
#include <cstdlib>
#include <ctime>
int main()
{
    srand( time( NULL ) );

    int given = 0;
    int imaginarynum = ( std::rand() % 99 ) + 1;
    int tries = 0;

    std::cout << "Hello! We'regoing to play an easy game! I'm goinng to think of a number between 1-100 and you'll have to guess it! " << std::endl;
    std::cout << given << " "<< imaginarynum;

    while(given < imaginarynum || given > imaginarynum)
    {
        std::cin >> given;
        bool error …
Run Code Online (Sandbox Code Playgroud)

c++ loops inputstream infinite-loop while-loop

0
推荐指数
1
解决办法
100
查看次数

在循环主体中使用逗号运算符

我试图用两种不同的方式编写一个while循环,并注意到在两次条件下使用','不能按我认为的方式工作。我只是对实际发生的事情感到好奇。这是代码:

#include <stdio.h>

int main()
{
    int i = 0, lim = 1000, c;
    char s[lim];
    while (i < lim - 1, (c = getchar()) != '\n')
    {
        if (c != EOF)
        {
            s[i] = c;
        }
        else
        {   
            break;
        }
        ++i;
    }
    printf("%s\n", s);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)
#include <stdio.h>

int main()
{
    int i = 0, lim = 1000, c;
    char s[lim];
    while (i < lim - 1, (c = getchar()) != '\n', c != EOF)
    {
        s[i] = …
Run Code Online (Sandbox Code Playgroud)

c loops while-loop

0
推荐指数
1
解决办法
46
查看次数

输出预测无法理解while语句

我正在寻找这个问题的解释。我看不懂while部分,为什么会打印6。

#include <stdio.h>
#include<stdlib.h>

int main() 
{
    int array[] = {1, 2, 4, 0, 4, 0, 3};
    int *p, sum = 0;

    p = &array[0];

    while (*p++)
        sum += *p;

    printf("%d\n", sum);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c loops while-loop output

0
推荐指数
1
解决办法
54
查看次数

这个程序如何最终获得5分?

谁能解释这个程序?末尾如何打印“ 5”?提前致谢。

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int a = 1;
    while (a++ <= 1)
    while (a++ <= 2);
    printf("%d",a);  //5
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c while-loop

0
推荐指数
1
解决办法
70
查看次数