标签: while-loop

Makefile while 循环

即使将 SHELL 设置为 bash 而不是 sh,以下操作也不起作用:

doc:
    while read line; do \
      eval echo "$$line" > $(DOC) \
    done < $(DOC).templ

/bin/bash: -c: line 3: syntax error: unexpected end of file
Run Code Online (Sandbox Code Playgroud)

我想要做的是有一个模板文件,其中包含 bash 参数理解等 ( $(), ${}) 并在编译时“构建”它。有一个更好的方法吗?

bash templates makefile while-loop

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

如何突然停止当前在后台执行的线程?

事实:

我有一个在后台线程上执行的方法:

[currentGame performSelectorInBackground:@selector(playOn:) withObject:self];
Run Code Online (Sandbox Code Playgroud)

这个方法基本上包含一个while循环,一直持续执行,直到用户点击Quit按钮:

-(void) playOn: (UIViewController*) thisViewController
{
    while(!quitButtonPressed)
    {
       // this is my method's main loop
    }
}
Run Code Online (Sandbox Code Playgroud)

问题:

如果用户在上述循环中间的某处单击Quit,则循环 的其余部分必须在再次检查BOOL 之前执行并最终停止.为了防止这种情况发生,并具有while循环停止,只要用户点击退出,我想我还可以添加很多 if(quitButtonPressed) break; 在这里和那里,我while循环,以半经常检查,"立即"割舍如果需要的话.然而,考虑到上面的主要while循环的大小以及它内部包含许多较小的while循环这一事实,从设计角度来看,这似乎并不是非常聪明或实用(if.. break;我必须添加的数量将是弄得很大,可能会让事情变得很复杂......

可能的解决方案(但它是正确的解决方案吗?):

所以我认为最好的方法是停止/取消上面方法的while循环执行的后台线程,而不是while循环本身,在方法内,用户点击Quit的那一刻

是这样的,或类似的(即更好的建议),可能,我怎么能这样做?

上述解决方案的可能实施:

我可以创建这个新方法:

-(void)checkQuitButton
{
   while(!quitButtonPressed)
   {
      //wait
   }
   if(quitButtonPressed)
   {
     // stop this thread-->[currentGame performSelectorInBackground:@selector(playOn:) withObject:self];
     // this is the method …
Run Code Online (Sandbox Code Playgroud)

multithreading objective-c while-loop ios

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

Python 3.3,在while循环中返回最小值

我试图使用牛顿方法返回k的平方根的最小值.

k=float(input("Number? "))
x = k/2

def newton(x):
    while abs(x**(1/2)- k) >= 10**(-10):
        if k >= 0:
            x = (x+k/x)/(2)
            return x
        elif k < 0:
            raise ValueError ("Cannot take the square root of a negative number")


print ("The approximate square root of", k, "is", newton(k))
print ("The error is", abs(x**(1/2)- k))
Run Code Online (Sandbox Code Playgroud)

但是,上面的代码只返回第一次迭代.例如,如果k是2,牛顿方法的准确平方根应该是1.41422,这是第三次迭代.但是,代码当前返回1.5,第一次迭代.如何返回1.41422而不是1.5的更精确的平方根?同样,错误需要反映这种变化.

python loops minimum while-loop

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

如果是Statement和while循环

我是一名自学成才的程序员,我刚开始使用python.当我执行这段代码时,我遇到了一些问题:

x = 0
while x == 0:
  question = raw_input("Would you like a hint? ")
  if question == "y" or "yes":
    print "Ok"
    first.give_hint("Look over there")
    x = 1
  elif question == "n" or "no":
    print "Ok"
    x = 1
  else:
    print "I'm Sorry, I don't understand that"
Run Code Online (Sandbox Code Playgroud)

只是你知道,这first.give_hint("Look over there")是在程序早期的一个类中定义的,我只是为了空间而离开了那一部分.当我运行程序时,无论我输入什么,我都会得到第一个案例"Look over There",我一直试图弄清楚问题是什么,但我只是不明白.如果你们能帮助我,我会非常感激.

python loops if-statement while-loop

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

python导入名称文件并按字母顺序排序

我无法弄清楚为什么我的简单名称脚本不起作用.它似乎在while循环中出错.我可能错误地称它为,但我想我可能会在这里继续研究时尝试得到答案.

#!/usr/bin/python

#open the file
name_file = open('names.txt', 'r')

#read in lines
names = name_file.readlines()

#close file
name_file.close()

#loop to place names in array
index = 0
        while index < len(names):
             names[index] = names[index].rstrip('\n')
             index += 1
#sort
names.sort()

#print sorted names
print names
Run Code Online (Sandbox Code Playgroud)

python file while-loop

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

oracle中的while循环

您好我在Oracle SQL中编写了一个while循环.我认为语法很好,但由于某种原因它不能成功.有人可以帮我检查问题所在吗?非常感谢!

它只使用一个表"test_fruit",colume"price".

BEGIN
  WHILE (select sum(price) from test_fruit) <50 LOOP
    update test_fruit set price = price + 5; 
    EXIT WHEN (select max(price) from test_fruit) >20; 
  END LOOP;
END;
Run Code Online (Sandbox Code Playgroud)

顺便说一句,我要做的是更新表,并增加每行的价格,直到总和为50.此外每行的最高价格不能超过20.谢谢!

sql oracle while-loop

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

我的"最低常见倍数"程序挂起,并没有输出答案

我一直在努力学习编程,所以我一直在努力创建一个简单的程序,它将两个数字作为输入,并计算最低的公共倍数.我在Python中这样做是因为我不知道如何在Java中输入.现在发生的事情是,在我输入数字后程序就会挂起,没有任何反应.这里的任何指针将不胜感激.谢谢.

#LCM Calculator
#Author: Ethan Houston
#Language: Python
#Date: 2013-12-27
#Function: Program takes 2 numbers as input, and finds the lowest number
# that goes into each of them

def lcmCalculator(one, two):
    """ takes two numbers as input, computes a number that evenly 
        divides both numbers """
    counter = 2 #this is the number that the program tried to divide each number by.
                #it increases by 1 if it doesn't divide evenly with both numbers.
    while True:
        if one % …
Run Code Online (Sandbox Code Playgroud)

python if-statement function lcm while-loop

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

while循环字符C++

我正在编写一个编程任务,我无法弄清楚为什么这个while循环无限循环.在此循环发生之前还有其他输入,但我有cin.ignore()在此循环之前清除任何垃圾.我在挠脑,帮忙吗?

//Prompt for readiness
    char ready = 'x';
    while (ready != 'Y' || ready != 'y') {
        cout << "READY TO START YOUR TURN " << playerName << "? ";
        ready = cin.get();
    }
Run Code Online (Sandbox Code Playgroud)

c++ char while-loop

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

很奇怪为什么这个递归代码不起作用,并且在另一个方法中表现出非常不合理的行为

这里是新程序员,对于我认为递归的一些代码存在一些问题.

public static int sum (int a) {
    int input = goodInput(); //get input from below method without having to put its code in this one
    if (input==-1)//so user has the ability to exit at any time
        return a; //when user has finally entered -1, the final sum is sent out
    else; //for scenarios before last input 
    int b = a + input; //adding the newest input to the sum 
    int c = sum(b); //throw the total into the method …
Run Code Online (Sandbox Code Playgroud)

java recursion input while-loop

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

Bash文件描述符3和读取行

我一直在寻找,找不到明确的线索来验证我从给我的脚本中得出的结论。

因此file.txt是一个打开的文件(由文件描述符3表示),并通过记录时间戳的脚本不断添加新行。每次将新行添加到文件时,下一段是否会进入while循环?

exec 3 < /path/file.txt
while read <&3
  command
  command..
done
Run Code Online (Sandbox Code Playgroud)

因此,只要我不关闭文件描述符,添加到我的file.txt中的新行将始终激活while循环,对吗?

请帮我解决这个问题。谢谢

bash file-descriptor while-loop

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