标签: while-loop

虽然循环不会在bool C#上破坏

我不明白为什么我的while循环不会在我分配bool变量(gameEnded)时停止循环true.

Board board = new Board();
bool gameEnded = false;
while (!gameEnded)
{
    gameEnded = board.DrawCheck(board); //one of these three methods returns true
    gameEnded = board.WinCheckO(board);
    gameEnded = board.WinCheckX(board);

    Render(board);
    bool turnO = false;
    Console.WriteLine("Player X's turn.");
    //... here some code that gets executed right
}
Run Code Online (Sandbox Code Playgroud)

因此,gameEnded布尔变量应该赋值为true,因此循环中断.我检查了Check something方法返回true.修改后的版本工作正常,即打破循环.

Board board = new Board();
bool gameEnded = false;
while (!gameEnded)
{

    if (board.WinCheckX(board))
     {
         Console.WriteLine("X player won!");
         break;
     }
     else if (board.WinCheckO(board))
     {
         Console.WriteLine("O player won!");
         break;
     } …
Run Code Online (Sandbox Code Playgroud)

c# while-loop

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

java while循环错误?附加输出代码有什么问题?

class demoWhile{        
public static void main(String args[])
    throws java.io.IOException{
        char ch = (char)System.in.read();
        while(ch!='w'){
            System.out.println("Hi"+ch);
            ch = (char)System.in.read();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

- 首先我给输入"R"和预期产量"的HIR",然后输入选项,而是获得产量的HIR,嗨,嗨然后输入选项.为什么它会显示两个额外的Hi,嗨?

java loops while-loop

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

T-SQL日期范围在循环中使用动态变量

我有一个包含事务,时间戳和用户的表.

CREATE TABLE [dbo].[Transactions]
(
    [transaction_ts] [datetime] NULL,
    [user_id] [bigint] NULL,
    [transaction_id] [bigint] NULL,
    [item] [varchar](50) NULL
)
Run Code Online (Sandbox Code Playgroud)

对于每一个user_id,我需要选择他们在第一次交易和72小时之后进行的所有交易.

--get first and last timestamps for range
DROP TABLE IF EXISTS #first;

SELECT mt.transaction_ts as first_trans,mt.user_id 
INTO #first
FROM Transactions mt 
INNER JOIN
    (SELECT user_id, MIN(transaction_ts) MinDate
     FROM Transactions
     GROUP BY user_id) t ON mt.user_id = t.user_id AND mt.transaction_ts = t.MinDate;

ALTER TABLE #first
ADD first_trans_plus_72 datetime;

UPDATE #first 
SET first_trans_plus_72 = DATEADD(hour, 72, first_trans)

--loop through user_id and …
Run Code Online (Sandbox Code Playgroud)

sql t-sql sql-server while-loop

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

为什么使用多个"if"不起作用,但它确实可以在while循环中使用"if"和"else if"?

这是我的代码,不使用else if:

#include <stdio.h>

main()
{
    long s = 0, t = 0, n = 0;
    int c;
    while ((c = getchar()) != EOF)
        if (c == ' ')
            ++s;
        if (c == '\t')
            ++t;
        if (c == '\n')
            ++n;
    printf("spaces: %d tabulations: %d newlines: %d", s, t, n);
}
Run Code Online (Sandbox Code Playgroud)

这是使用else的代码,如果:

#include <stdio.h>

main()
{
    long s = 0, t = 0, n = 0;
    int c;
    while ((c = getchar()) != EOF)
        if …
Run Code Online (Sandbox Code Playgroud)

c if-statement while-loop getchar

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

为什么带有空字符串的数组上的array_shift会破坏循环?

我只是注意到,当遇到空字符串时,使用while和array_shift迭代数组的行为与简单的for循环不同.这是一个示例:

循环:

$arr=[1, "",1,""];
while ($elm = array_shift($arr))
{
    var_dump($elm);
}
Run Code Online (Sandbox Code Playgroud)

对于循环:

$arr=[1, "",1,""];
for ($i = 0; $i<count($arr); $i++)
{
    var_dump($arr[$i]);
}
die();
Run Code Online (Sandbox Code Playgroud)

为什么while退出循环?

php loops for-loop while-loop

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

Json对象在while循环中断页面时进行字符串检查

我有一个网页,从网址获取一个json对象,然后它将json对象转换为一个字符串.如果字符串为真,我希望div淡出,如果它是假的,我希望它保持不变.

来自url的json对象是:{"description":"Input 1","type":"no","enabled":true,"alarm":false}

启用警报时,alarm = true.但我需要不断检查警报是真还是假,所以我需要一个while循环或其他方法来不断检查url的json对象.

出于某种原因,我在下面的代码中使用的方法使页面中断.

当我取出while循环时,它完美地工作,但我必须刷新浏览器以重新检查警报对象.

<html>
<head>
    <link rel="stylesheet" type="text/css" href="style.css">
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" crossorigin="anonymous"></script>
       <script>
        $(document).ready(function () {
            var data;
            var r = true;
            do {


                $.ajax({
                    dataType: "json",
                    url: 'http://192.168.1.6/inputs/alarm.cgi',
                    data: data,
                    success: function (response) {
                        // begin accessing JSON data here
                        console.log(response.alarm);
                        var j = (JSON.stringify(response.alarm));
                        console.log(j);
                        if (j == 'true') {
                            $('div').fadeOut('slow');
                        }
                    }
                });
            } while (r == true);
        });
    </script>
</head>
<body>

    <div></div>


</body>
</html>
Run Code Online (Sandbox Code Playgroud)

CSS代码

 div {
        height: 100px;
        width: …
Run Code Online (Sandbox Code Playgroud)

javascript jquery json while-loop

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

为什么while循环像这样?

我想,我对编程有所了解......但我不明白下面的例子......

<?php
$i = 0;
$j = 0;
while($i<5){
    while($j<5){
        echo "$i $j; ";
        $j++;
    }
    $i++;
}
Run Code Online (Sandbox Code Playgroud)

我期望的输出:"0 0; 0 1; 0 2; 0 3; 0 4; 1 0; 1 1; 1 2; 1 3; 1 4; 0 0; 2 1; 2 2; 2 3; 2 4 ; 3 0; 3 1; 3 2; 3 3; 3 4; 4 0; 4 1; 4 2; 4 3; 4 4;".

我得到的输出:"0 0; 0 1; 0 2; 0 3; 0 4;".

这是为什么?

php loops cycle while-loop

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

为什么我的整数增加2而不是1?java的

我已经实现了类似于简单时钟的代码,每次在我的课程中它应该增加1.到目前为止,这是我在主程序中的内容:

public static void main(String[] args) {
    BoundedCounter counter = new BoundedCounter(60);
    System.out.println("Value at start: " + counter);
    int i = 0;
    while (i < 10) {
        counter.next();
        System.out.println("Value: " + counter);
        i++;
    }
}
Run Code Online (Sandbox Code Playgroud)

这是"BoundedCounter"类:

class BoundedCounter {

    private int value;
    private int upperBound;

    public BoundedCounter(int upperBound) {
        this.upperBound = upperBound;
        this.value = 0;  
    }

    public void next () {
        if (value++ > upperBound) { 
            value = 0;
        } else {
            value++;
        }
    }

    public String toString() {
        return …
Run Code Online (Sandbox Code Playgroud)

java int increment while-loop

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

用文件名中的空格读取

在这个.ogg文件上

$ tree
.
??? Disc 1 - 01 - Procrastination.ogg
??? Disc 1 - 02 - À carreaux !.ogg
??? Disc 1 - 03 - Météo marine.ogg
??? mp3
Run Code Online (Sandbox Code Playgroud)

我尝试使用while循环将ffmpeg转换为mp3保存文件名中的空格::

$ ls *.ogg | while read line; do ffmpeg -i "$line" mp3/"$line".mp3 ; done
Run Code Online (Sandbox Code Playgroud)

但我得到这个错误::

$ ls *.ogg | while read line; do ffmpeg -i "$line" mp3/"$line".mp3 ; done
...
Parse error, at least 3 arguments were expected, only 0 given
in string ' 1 …
Run Code Online (Sandbox Code Playgroud)

bash ffmpeg while-loop

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

用c ++读取未知数量的输入

我正在使用C++ Primer一书学习C++.

在1.4.3节中,给出了以下关于读取未知数量输入的示例代码.

#include <iostream>
int main()
{
  int sum = 0, value = 0;
  // read until end-of-file, calculating a running total of all values read
  while (std::cin >> value)
  sum += value; // equivalent to sum = sum + value
  std::cout << "Sum is: " << sum << std::endl;
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

根据书中我们给出的输入3 4 5 6,输出将是Sum is: 18

但是当我在我的计算机上尝试这个(带有mingW的Windows 10)时,代码并没有结束.即使我输入换行符,它也会继续询问输入.它仅在我输入非int输入时才有效f.


这是预期的吗?如果是,是否有任何代码在输入换行符后停止?

我对c ++很陌生,而且我已经学过python,所以这么早就陷入困境是非常令人沮丧的.


感谢致敬.

c++ algorithm user-input while-loop c++14

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

标签 统计

while-loop ×10

loops ×3

java ×2

php ×2

algorithm ×1

bash ×1

c ×1

c# ×1

c++ ×1

c++14 ×1

cycle ×1

ffmpeg ×1

for-loop ×1

getchar ×1

if-statement ×1

increment ×1

int ×1

javascript ×1

jquery ×1

json ×1

sql ×1

sql-server ×1

t-sql ×1

user-input ×1