标签: while-loop

遍历数组Javascript

我应该使用while循环来遍历这个数组并将每个项目乘以2.当我尝试运行它时页面会永远保持加载....我错过了什么?谢谢.

var prices = [4, 5, 8, 10];

var i = 0;
while (i < prices.length){
    prices[i]++;
    i*=2;
}

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

javascript traversal while-loop

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

while循环在分配char'\ 0'时不起作用

我已经尝试过这个程序,但它没有用,IMO它应该在数组的每个位置存储q,但为什么不发生?

  `
   int main()
   {
      char *c=new char[10];
      char *p=c+9;
      *p='\0';    //This should've assigned the last value to null terminator
      int i=0;
      p=c;
      while(*c)
        {
            i++;
            *c='q';
            cout<<*c<<endl;
            ++c;
        }
      cout<<i<<endl<<*p<<endl;
      return 0;
  }
Run Code Online (Sandbox Code Playgroud)

这真的超出了我,请帮忙.为什么这个循环没有循环10次?因为'\ 0'位于第10位.

c++ arrays while-loop

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

如何迭代一个字符列表,同时仍能在迭代中跳过?

我有以下代码:

let mut lex_index = 0;
let chars = expression.chars();
while lex_index < chars.count() {
    if(chars[lex_index] == "something") {
        lex_index += 2;
    } else {
        lex_index += 1;
    }
}
Run Code Online (Sandbox Code Playgroud)

我while在这里使用循环,因为我有时需要跳过一个字符chars.但是,这给了我以下错误:

error[E0382]: use of moved value: `chars`
  --> src/main.rs:23:15
   |
23 |     while i < chars.count() {
   |               ^^^^^ value moved here in previous iteration of loop
   |
   = note: move occurs because `chars` has type `std::str::Chars<'_>`, which does not implement the `Copy` trait
Run Code Online (Sandbox Code Playgroud)

iteration while-loop chars rust

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

input.nextLine()在while循环中带有一个字符串

我很难理解我的代码到底出了什么问题.我不是一个好的开发人员,我还在学习,所以所有提示都非常受欢迎!:)

所以我正在做的是有一个while循环来检查用户是否给了Scanner某个字母.如果有,则循环结束.如果没有,它会不断再问这个问题.

我的代码如下:

Scanner userInput = new Scanner(System.in);
String userAnswer = userInput.nextLine();
boolean isTrue = true;

     while (isTrue){
         if (userAnswer.equals("e")){
             System.out.println("Wrong, try again");
             userInput.nextLine();
         } else if (userAnswer.equals("k")) {
             System.out.println("That's right!"); 
             isTrue = false;
             break;
         }      
     } 
Run Code Online (Sandbox Code Playgroud)

它有效,有点.如果用户给出答案"k"作为第一个答案,则打印出"那是对的!" 然后打破循环.如果用户给出答案"e"作为第一个答案,则打印出"错误,再试一次"并给出一个新行再次回答,但如果你写了正确的字母,即"k",它仍会打印出来"错误,再试一次"循环,不会跳转到"else if"语句.

我如何在这方面工作?非常感谢提前.

java string loops user-input while-loop

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

如何在while循环中杀死线程C#

我有一个while循环检查true/false.And我也有阻止.如果块计数到300到0.我想杀死线程时它是0和另外两个地方.我看了论坛但我的方法看起来像有点不同.btw我试过中止但没有工作.如果我的帖子中有错误请edit.thx!

            void Test()
            {

                Thread thread = new Thread(() =>
                            {
                                try
                                {
                                    int countdown = 300;
                                    while (true)
                                    {
                                        Thread.Sleep(1000);
                                        paymentService.CheckPayment(pdId);
                                        if (result.Complete == false)
                                        {
                                            countdown--;
                                            if (countdown == 1)
                                            {
                                                //kill thread
                                            }
                                        }
                                        if (result.Complete == true)
                                        {
                                            //kill thread
                                            NavigationService.Navigate(new Uri("Pages/success.xaml", UriKind.Relative));
                                        }
                                    }
                                }
                                catch (Exception)
                                {
                                    //kill thread
                                    Application.Current.Shutdown();
                                }
                            });
                thread.Start();

            }
Run Code Online (Sandbox Code Playgroud)

c# multithreading while-loop

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

使用ifstream while循环,如何显示输入错误并在下一行继续?

如果我的输入文件以字母开头,它将停止while循环,因为它无法重写int1,我知道但是我怎么能够检测到这一点并显示一条错误消息,说workinfile>>int1不起作用,然后继续循环?

cin>>filename;
ifstream workingfile(filename);

while (workingfile>>int1>>int2>>string1>>string2) {
    cout<<int1<<int2<<string1<<string2<<endl;
    linenumread++;
}
Run Code Online (Sandbox Code Playgroud)

我尝试过,但它不起作用,任何帮助将不胜感激

 while (workingfile>>int1>>int2>>string1>>string2) {
    if(!(workingfile>>int1))
    {
       cout<<"Error first value is not an integer"<<endl;
       continue;
    }
    cout<<int1<<int2<<string1<<string2<<endl;
    linenumread++;
}
Run Code Online (Sandbox Code Playgroud)

还有可能检测它是否也停止读取字符串?

输入文件看起来像这样

10 10 ab bc
11 11 cd ef
a  
12 12 gh hi
Run Code Online (Sandbox Code Playgroud)

我想检测何时遇到无效输入,显示错误消息,并继续文件中的下一行.

c++ ifstream while-loop

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

游标SQL Server很慢

我不知道为什么这么慢

DECLARE kursor CURSOR FOR
    SELECT mediatime, ShortDate, MediaDateID, TimeID 
    FROM DateTimeMCross

DECLARE @mediatime INT, @ShortDate DATE, @MediaDateID INT, @TimeID INT, @tmpDate DATE

SET @mediatime = 0;

OPEN kursor

FETCH NEXT FROM kursor INTO @mediatime, @ShortDate, @MediaDateID, @TimeID

WHILE @@FETCH_STATUS = 0
BEGIN
    /*SET @tmpDate = (select DATEADD(day,-1,@shortDate))*/
    IF @mediatime >= 2400
    BEGIN
        UPDATE DateTimeMCross
        SET DateTimeMCross.newMediaDateID = 2/*(SELECT     YEAR(@tmpDate) * 10000 + MONTH(@tmpDate) * 100 + DAY(@tmpDate))*/
        WHERE DateTimeMCross.MediaDateID = @MediaDateID 
          AND DateTimeMCross.TimeID = @TimeID
    END
    ELSE IF @mediatime …
Run Code Online (Sandbox Code Playgroud)

sql sql-server performance cursor while-loop

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

为什么do数字(数字> 10)会产生无限循环?

根据我的理解,while应该结束循环,导致int数字"in"循环应该用0初始化它.或者我错了吗?但它给了我一个无限循环,打印零.我用gcc编译它并尝试用gdb调试它.对我来说没有意义,因为while不会停止循环.

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

int main()
{
    int number = 20;
    do
    {
        int number = number / 2;
        printf("%d ", number);
    } while (number > 10);
}
Run Code Online (Sandbox Code Playgroud)

c int loops initialization while-loop

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

Python 3:如何使用While循环从列表中删除重复项

这是我目前的代码:

sandwich_orders = ['pbj ', 'pastrami ', 'ham ', 'blt ', 'pastrami ', 'knuckle ', 'pastrami ']
finished_sandwiches = []
print("Ordered sandwiches: " , sandwich_orders)

while sandwich_orders:
    complete_sandwiches = sandwich_orders.pop()
    print("Now making your  " + complete_sandwiches.title() + "sandwich")
    finished_sandwiches.append(complete_sandwiches.title())

for finished_sandwich in finished_sandwiches:
    print("Your " + finished_sandwich.title() + " sandwich is now ready.")
Run Code Online (Sandbox Code Playgroud)

这是我运行时的当前程序输出:

    Ordered sandwiches:  ['pbj ', 'pastrami ', 'ham ', 'blt ', 'pastrami ', 'knuckle ', 'pastrami ']
Now making your  Pastrami sandwich
Now making your  Knuckle sandwich …
Run Code Online (Sandbox Code Playgroud)

python list while-loop python-3.x

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

在C编程中退出(1)循环

我正在为我的C编程课做例子.其中一个例子是这样的:

更新:return 0;和之间的区别break;

#include<stdio.h>
int main()
{

while(1)
{
  printf("Enter number: ");
  scanf("%d", &num);

  if (num==2)
   {
      return 0;
   }
  else
   {
      printf("Num = %d", num);
   }

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

我知道这while(1)是一个无限循环.为了摆脱循环我break;正常使用.

但是,在这个例子中,当num=2它失去循环.

我不明白循环return o;效果如何while(1)?

c return-value infinite-loop while-loop

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