标签: while-loop

而真正的循环会冻结浏览器 javascript

所以我试图改变图像并将它们变成一个循环,我想出了这个脚本,我遇到的问题是,当它改变到第二张图片时,javascript 会冻结在我身上,我知道它while(true) 部分,但我不知道如何修复它,请帮忙。
感谢您

var images = new Array();
images[0] = "image1.jpg";
images[1] = "image2.jpg";
images[2] = "image3.jpg";
setTimeout("changeImage()", 3000);
var x=0;

function changeImage()
{
    while(true){
        document.getElementById("img").src=images[x]
        x++;
    }

    if (x=2){
        x=0;
    }
}
Run Code Online (Sandbox Code Playgroud)

javascript loops image while-loop

-2
推荐指数
1
解决办法
3636
查看次数

如何使用Python中的for和while循环按字母顺序排序?

我还在努力学习Python。我创建了一个水果列表,如下

fruitList = ["pear","apple","strawberry","banana","orange"]
Run Code Online (Sandbox Code Playgroud)

我想使用 for 循环和 while 循环按字母顺序打印它们。

   fruitList.sort()
Run Code Online (Sandbox Code Playgroud)

因为 python 有一个名为 sort() 的函数,因此我可以排序。但是如果我不想使用排序功能怎么办?

但是,现在我有一个问题,如何使用 while 循环进行排序?有人可以告诉我我该怎么做吗?这是我尝试过的,使用 len()

for fruit in fruitList:
    while (len(fruitList[0]) > len(fruit[0])):
            fruit += 1
            print fruit
            continue
Run Code Online (Sandbox Code Playgroud)

当我运行这个程序时没有打印任何内容。在 while 循环中,我是否应该与索引[0] 进行比较,或者我也可以使用排序函数吗?

python for-loop while-loop

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

JavaScript while循环条件变量未更新

为什么以下逻辑不起作用?

警告:当您单击"运行代码段"时,以下内容将启动无限循环.

var i = 0;
var currentLocation = 1;

while(currentLocation !== 9){
    console.log(currentLocation);
    currentLocation += i;
    i++;
}
Run Code Online (Sandbox Code Playgroud)

这进入了一个无限循环.但是如果我们替换currentLocation += i;它currentLocation++;,它按预期工作.只是好奇为什么会这样.

javascript increment while-loop

-2
推荐指数
1
解决办法
43
查看次数

为什么for循环的条件是可选的,但是while循环不是?

为什么被for(;;)视为无限循环,同时while()给出错误,因为它需要表达式?为什么for循环也不期望表达式呢?

c c++ loops for-loop while-loop

-2
推荐指数
1
解决办法
205
查看次数

为什么在这种情况下 return 0 不起作用?

最近,我从开始学习C C程序设计语言由布莱恩·柯林汉和丹尼斯·里奇。在那本书中,有一个完整的小节(1.5.1,第 2 版),作者在其中创建了一个文件复制程序(但我将其用作文本(由用户输入)复制程序)。他们的代码基本上是这样的

#include <stdio.h>

int main()

{
    int c;
    c = getchar();

    while (c != EOF) {
        putchar(c);
        c = getchar();
    }
}
Run Code Online (Sandbox Code Playgroud)

这段代码在运行时工作正常,但程序永远不会停止。该程序一直在等待输入并继续复制它们,无休止地,永不终止。现在我想创建一个程序,使这种无休止的复制终止。为此,我稍微调整了代码。这是我的代码

#include <stdio.h>

int main()

{
    int c;
    c = getchar();

    while (c != EOF) {
        putchar(c);
        c = getchar();
    }

    return 0;

}
Run Code Online (Sandbox Code Playgroud)

我认为return 0;在最后明确写入可能会完成这项工作,但似乎输出根本没有改变。我还尝试使用 if-else 条件并按如下方式循环它们

#include <stdio.h>

int main()

{
    int c;
    c = getchar();

    while (1) {

        if (c != EOF){
            putchar(c);
            c = …
Run Code Online (Sandbox Code Playgroud)

c loops return infinite-loop while-loop

-2
推荐指数
1
解决办法
128
查看次数

我用这个字符串得到了一个无限的 while 循环

所以我在这里得到了这部分代码:

string stringparam;

string stringparam1;

double d;

unsigned x;


cout << "Welcome" << endl;

cout << "Choose one (ASUS, MSI, Gigabyte, ASrock)" << endl;
cin >> stringparam;
while (stringparam != "ASUS" || "MSI" || "Gigabyte" || "ASrock")
{
    cout << "you gave bad input \n" << endl;
    cout << "Choose one (ASUS, MSI, Gigabyte, ASrock)" << endl;
    cin >> stringparam;
}
Run Code Online (Sandbox Code Playgroud)

问题是即使我给他一个正确的答案,比如“华硕”,它仍然给我错误信息:“你的输入错误”我该怎么办?

c++ while-loop

-2
推荐指数
1
解决办法
88
查看次数

如何在Python中增加字符串值

如何在Python中增加字符串的值?

我有以下字符串:str = 'tt0000002' 并且我想增加该字符串以'tt0000003', 'tt0000004','tt0000005' (...) to 'tt0010000'使用循环。

我怎样才能做到这一点?

python string for-loop while-loop

-2
推荐指数
1
解决办法
1258
查看次数

尝试做一个计算器

所以基本上每当我尝试更改 while 循环内的变量“restart”时,它都会给我一个错误,提示“无法在此范围内声明名为“restart”的本地或参数,因为该名称在封闭的本地范围中使用来定义本地或参数”。如果有人知道如何解决这个问题请告诉我


public class Program
{
    public static void Main()
    {
        bool restart = true;
        while (restart == true)
        {
            Console.WriteLine("please enter Multiply, Divide, Add Or Subtract");
            string Method = Console.ReadLine();
            if (Method == "Add")
            {
                Console.WriteLine("Please Enter First Number");
                double numberOne = Convert.ToDouble(Console.ReadLine());
                Console.WriteLine("Please Enter The Second Number");
                double numberTwo = Convert.ToDouble(Console.ReadLine());
                Console.WriteLine(numberOne + numberTwo);
                Console.WriteLine("Would You Like To Use The Calculator Again? (true = yes, false = no)");
                bool restart = Convert.ToBoolean(Console.ReadLine());
            }
            else if (Method == …
Run Code Online (Sandbox Code Playgroud)

c# calculator while-loop

-2
推荐指数
1
解决办法
67
查看次数

与基本的python代码混淆

我不明白这段代码.我在想,虽然iterLeft不等于0,但它将ans添加到x.答案是0 x是3等于3. Iterleft - 1 = 2

print str(x) 应该打印2因为x是2,然后再打印2,然后是3,这是3

我的解释有什么问题

x = 3
ans = 0
itersLeft = x
while (itersLeft != 0):
     ans = ans + x
     itersLeft = itersLeft - 1
print str(x) + '*' + str(x) + ' = ' + str(ans)
Run Code Online (Sandbox Code Playgroud)

python while-loop

-3
推荐指数
1
解决办法
219
查看次数

这是在c ++中运行循环的有效方法吗?

如果我想要一个while循环:虽然变量不等于几个不同的值,这是否有效?:

while (uInput != ('a' || 'b' || 'c')){
    //do something to make it equal one of those
}
Run Code Online (Sandbox Code Playgroud)

或者我是否必须单独将uInput与a,uInput与b进行比较,依此类推?

c++ syntax loops while-loop

-3
推荐指数
1
解决办法
106
查看次数