标签: while-loop

如何修复Ruby Yes/No-Style循环?

我有这个方法,我写了一个使用while循环内部的n次用户输入.这个想法非常简单和常见,如果条件为真则重复while循环,问题是它不起作用......

def play_again?
  flag = true
  while flag
    print "Would you like to play again? [y/n]: "
    response = gets.chomp
    case response
      when 'y'
        Game.play
      when 'n'
        flag = false
    end
  end
  flag
end

play_again?
Run Code Online (Sandbox Code Playgroud)

目前它只会成功重复一次,然后退出,而不是继续循环,你能告诉我有什么问题吗?(对不起,如果这是一个n00b问题,毕竟我是一个红宝石n00b)

谢谢.

ruby while-loop

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

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
查看次数

在语句中尝试阻止

我刚刚开始使用Python 2.7,我不明白为什么会发生什么:

在下面的代码中,来自python 2.7.2教程的示例的装饰版本,我得到了一个意想不到的结果:

while True:
    try:
        x = int(raw_input("Please enter a number: "))
        break
    except ValueError:
        print "Oops!  That was not a valid number.  Try again..."
    else:
        print 'Thanks,',x,'is indeed an integer'
    finally:
        print 'all done, bye'
Run Code Online (Sandbox Code Playgroud)

当我输入一个整数时,代码会忽略该else:语句并直接切入finally:.显然它与while True:顶部有关但为什么会发生?

python if-statement exception-handling while-loop

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

虽然循环被绕过?

所以我是comp sci的第一年学生,我必须创建一个程序,让一个数组加扰它,然后再对它进行排序.我几乎让它工作,但由于某种原因,我的一个while循环将无法工作,并完全被绕过.我真的很陌生,所以所有的帮助都表示赞赏.我排序,打印工作,争抢只是给我一些麻烦.

import java.io.*;
import java.util.Random;
public class Driver03
    {
     public static void main(String[] args)
      {
        int[] array = {100, 101, 102, 103, 104, 105, 106, 107, 108, 109};
        print(array);
        array = scramble(array);
        print(array);
        array = sort(array);
        print(array);
      }
      public static void print(int[] apple)
        {
            for(int x = 0; x < apple.length; x++){
                System.out.println(apple[x]+"");
                }
        }
      public static int max;
      public static int maxIndex;
      public static int temp;
      public static int[] sort(int[] grape)
        {
            for(int y = grape.length; y > …
Run Code Online (Sandbox Code Playgroud)

java while-loop

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

如何修复具有多个条件的while循环返回错误

我编写了代码,要求用户键入"2012"或"2013"​​.然后我进行while循环检查以查看用户是否键入2012或2013.

输入有效年份的请求应该继续,直到用户输入有效数字.但是,我不断收到意外"{"发生的错误.我无法找到为什么这会在我的代码中产生这样的错误.

这是代码:

yr_function <-function(){
   year_of_data <- as.numeric(readline("Is the year of concern 2012 or 2013? >>> "))
}

year_of_data = 0
While((year_of_data != 2012) | (year_of_data != 2013)){
    yr_function
if(year_of_data != (2012 | 2013),print("You must type either 2012 or 2013")
}
Run Code Online (Sandbox Code Playgroud)

生成错误读取:

Error: unexpected '{' in "While((year_of_data != 2012) | (year_of_data != 2013)){"
Run Code Online (Sandbox Code Playgroud)

任何帮助将非常感激.

r while-loop multiple-conditions

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

没有退出"while"循环,但为什么呢?

所以我在这里有一个简单模仿跳棋的程序(但不是跳跃,一件只是"吃"另一件).我有一个实现"Checkers"类对象的简单类,并创建一个名为"chips"的int数组变量,有一个"WHILE"语句创建一个循环,而chip [0]和chips [1]整数大于0.它看起来像这样:

public static void main(String [] args){

        Checkers c = new Checkers();
        c.init();
        c.printBoard();
        int[] chips = c.count();
        Scanner kbd = new Scanner(System.in);
        while(chips[0]>0 && chips[1]>0){
            System.out.println("\nYour move? 4 ints: src row, src col, dest row, dest col separated by [SPACE]");
            int srcR = kbd.nextInt();
            int srcC = kbd.nextInt();
            int destR = kbd.nextInt();
            int destC = kbd.nextInt();
            kbd.nextLine();
            c.move(srcR,srcC,destR,destC);
            c.printBoard();
            System.out.println(c.getMessage());
            c.count();
        }
    }
Run Code Online (Sandbox Code Playgroud)

我在Checkers类中的count方法如下所示:

public int[] count() {  

    int wht=0;
    int blk=0;

    System.out.println();

    for(int i=0; i<board.length;i++)    { …
Run Code Online (Sandbox Code Playgroud)

java arrays while-loop

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

什么 while(($#)); 做 ...; 转移; done 在 bash 中是什么意思,为什么有人会使用它?

我在网上的 bash 脚本教程中遇到了以下 while 循环:

while(($#)) ; do
   #...
   shift
done
Run Code Online (Sandbox Code Playgroud)

我不明白在 while 循环中使用位置参数基数。我知道该shift命令的作用,但是 while 语句与shift?

parameters bash while-loop

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

循环条件中逻辑运算符的使用

在下面给出的代码中,为什么||逻辑不起作用,而是循环在&&使用时专门终止?

int main() {
    char select {};
    do {
        cout<<"Continue the loop or else quit ? (Y/Q): ";
        cin>>select;
    } while (select != 'q' && select != 'Q'); // <--- why || (or) doesn't work here ??
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ loops while-loop logical-operators do-while

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

while(sline &gt;&gt; n &gt;&gt; c &amp;&amp; c== ',') 背后的逻辑是什么?

vector<int> ParseLine (string line){
  istringstream sline(line);

  char c;
  int n;
  vector<int> row;
  while(sline >> n >> c && c == ",")
        row.push_back(n);

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

这是从文件中读取二维向量的函数之一。我想知道 while 循环背后的逻辑。

c++ loops vector while-loop

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

如何进行while循环但有更多条件C++

cout << "Options:\n1: Change Name\n2: Change Password\n3: Change Address\n4: Withraw\n5: Deposit\nL: Log out\n>";
while (user_input2 != '1' && user_input2 != '2' && user_input2 != '3' && user_input2 != '4' && user_input2 != '5' && user_input2 != 'L')
{
    cout << "Invalid input";
}
Run Code Online (Sandbox Code Playgroud)

那么我该如何缩短 while 条件呢?

我试着做:

cout << "Options:\n1: Change Name\n2: Change Password\n3: Change Address\n4: Withraw\n5: Deposit\nL: Log out\n>";
while (user_input2 != '1','2','3','4','5','L')
{
    cout << "Invalid input";
}
Run Code Online (Sandbox Code Playgroud)

但它不起作用。

编辑1:“我为我想做的事情添加了更多提示”

c++ while-loop

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