标签: do-while

Bash Shell Do While Loop Infinite循环?

基本上这是我的代码:

bay=$(prog -some flags)
while [ $bay = "Another instance of this program is running, please exit it first" ]
do
echo "Awaiting Access to program"
do
.....
Run Code Online (Sandbox Code Playgroud)

我有一个程序,它只允许一个实例一次运行,因为它与我的硬件交互的方式,当另一个实例运行时它踢出以下消息"该程序的另一个实例正在运行,请先退出它" .

我需要能够运行多个脚本,这将使用相同的程序,所以我决定使用上面的代码.我的问题是,当我运行我的两个脚本时,一个人将获得对程序的访问并按照需要运行,但另一个将注意到错误,然后卡在一个无限循环中,回显"等待访问程序".

错过了什么?Statement是执行CLI命令还是只是重新执行其原始执行?或者我的问题在哪里?

linux bash shell while-loop do-while

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

在Java中将do-while转换为for循环

目前,我有这个:

 do{
           if (i < y){ //y is a constant

               ObsHandler.obsPartsHandler(); //increment i twice
               ObsHandler.search(); 

           }
           else { 
               break;
           }

       } while (!endline.equals(null)); // endline is changed during each loop cycle
Run Code Online (Sandbox Code Playgroud)

通过这个循环和我的输入,endline不能为null; 这使得while循环破坏条件变得多余.当我尝试将此循环转换为for循环时,我从Eclipse中得到了错误的构造错误.

即:

for (i < y) {
       ObsHandler.obsPartsHandler(); //increment i twice
       ObsHandler.search(); }
Run Code Online (Sandbox Code Playgroud)

虽然,我工作的while循环,对我来说似乎是不好的做法.有什么建议?谢谢.

java for-loop do-while

4
推荐指数
2
解决办法
986
查看次数

while循环使用C++中的多个条件

在满足多个条件之一之前,我将如何创建循环来执行循环.例如:

do
{
    srand (time(0));
    estrength = rand()%100);

    srand (time(0));
    strength = rand()%100);
} while( ) //either strength or estrength is not equal to 100
Run Code Online (Sandbox Code Playgroud)

有点蹩脚的例子,但我想大家都明白.

我知道&&,但我希望它只满足其中一个条件并继续前进,而不是两者兼而有之.

c++ do-while

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

我的do-while循环没有结束

我是c ++的新手,很抱歉,如果这个问题非常简单.我正在用c ++编写一个程序来掷骰子并显示它的数字,直到用户键入单词cancel但我的循环没有结束,即使我输入取消我的代码(我用的是dev c ++):

#include<iostream>
#include<conio.h>
#include<stdlib.h>
#include<stdio.h>
using namespace std;
int dice (int);
int main()
{
    char k[7];
    int x;
    do
    {
          cout<<"your dice number is: "<<dice(x)<<endl;
          cout<<"do you want to cancel or continue?";
          cin>>k;
     }while(k!="cancel");
          cout<<"END";
          getch();
}
int dice (int a)
{   
    srand(time(NULL));
    for(int i=1;i<100;i++)
        {
            a=(rand()% 6)+1;
        }
            return a;        
}
Run Code Online (Sandbox Code Playgroud)

c++ do-while

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

如果switch语句达到默认值,则重复执行while循环

我有一个while循环请求用户输入.在while while循环中我有一个switch语句.如何才能使其达到默认值,重复循环再次询问用户性别?

do
    {
        cout << "What is your weight?" << endl;
        cin >> weight;
        cout << "What is your height?" << endl;
        cin >> height;
        cout << "What is your age?" << endl;
        cin >> age;
        cout << "What is your gender?" << endl;
        cin >> gender;

        switch (gender)
        {
            case 'M':
            case 'm':
                cout << endl << Male(weight, height, age);
                break;
            case 'F':
            case 'f':
                cout << endl << Female(weight, height, age);
                break;
            default:
                cout << "What is …
Run Code Online (Sandbox Code Playgroud)

c++ switch-statement do-while

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

为什么`do {} while(0);`这么快?

我尝试了以下3个for循环:

#define loop 1000000000
NSDate *start;
NSDate *end;


// 1: empty for loop
start = [NSDate date];
for (NSInteger i = 0; i < loop; i++) {
}
end = [NSDate date];
NSLog(@"time interval: %f", [end timeIntervalSince1970] - [start timeIntervalSince1970]);


// 2: do-while for loop
start = [NSDate date];
for (NSInteger i = 0; i < loop; i++) {
    do {
    } while (0);
}
end = [NSDate date];
NSLog(@"time interval: %f", [end timeIntervalSince1970] - [start timeIntervalSince1970]);

// …
Run Code Online (Sandbox Code Playgroud)

objective-c do-while

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

使用多个CPU核心优化目录中的映像

我在目录中有几个PNG图像,我optipng用来优化和减少图像大小.问题是优化所有文件需要很长时间.

我有一个四核处理器,我注意到optipng在优化目录时只使用了一个核心.

这是我正在使用的代码:

ls -1 | while read line
do 
    optipng -o7 "$line"
done
Run Code Online (Sandbox Code Playgroud)

optipng在读取目录时是否可以并行执行四个不同的文件?

linux bash terminal image do-while

4
推荐指数
3
解决办法
1472
查看次数

开关盒在嵌套时做

#include<stdio.h>
int main()
{
    int n=0, i=2;
    switch(1)
    {
    case 0:do
           {
    case 1: n++;
    case 2: n++;
           }while(--i > 0);
    }
    printf("n = %d",n);
}
Run Code Online (Sandbox Code Playgroud)

我期望上面代码的输出为0,因为情况1和情况2都在do内,而在case 0中.Twitch正在测试值1,所以case 0将永远不会被执行,因此情况1或2都没有.

n的值将是4.任何解释?

c nested switch-statement do-while

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

Nodejs - 错误回调时重新调用函数 - 是否存在非阻塞方式?

我有一个函数向API发出请求.有时API会出现一些小问题,并且不时有一两秒钟可用,导致出现错误,我想再次调用该函数.由于此回调后还有另外70~80行代码,我不想用一个分割流if(error) <do the same stuff> else <as here>

在尝试了一段时间后,我最终使用了do-while(错误)循环,它可以阻止.有这样的异步方式吗?

我的代码(简化为泛化):

//This is the request part

function bar(j, callback){      
   j++;

   //simulated error
   if(j<=10){   
       return( callback('dont', j) );
   }
   //simulated success
   else{
       return( callback(null, j) );
   }

}

//This is the main function - part of a bigger piece in my code

function foo(){
   var i = 0;
   var err = 'yes'; //else we'd get an 'err is not defined'

   do{
     bar(i, function(error, j){
        i = j
        err = error;

        if(error){ …
Run Code Online (Sandbox Code Playgroud)

asynchronous node.js do-while

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

'ne'在do-while循环中不起作用,而'!='起作用

我不确定我犯的是什么错误,但我只是改变ne了!=它并且它起作用了.

这是一个简单的程序,让用户猜出一个数字,直到他们达到目标数字.

#!/usr/bin/perl

my $guess = 1;

do {
    $guess = <STDIN>;
} while ( $guess != 12 ) ; # it doesn't work if i replace != with ne

say "you guessed ", $guess;
Run Code Online (Sandbox Code Playgroud)

perl do-while

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