据我所知,虽然声明需要条件才能工作,但在这里它没有任何作用; 怎么可能?while q:工作怎么样?
代码如下:......
q = set([])
for i in range(N):
q.add((i, 0))
q.add((i, M - 1))
w[i][0] = h[i][0]
w[i][M - 1] = h[i][M - 1]
for i in range(M):
q.add((0, i))
q.add((N - 1, i))
w[0][i] = h[0][i]
w[N - 1][i] = h[N - 1][i]
while q:
ci, cj = q.pop()
for ii, jj in ((0, 1), (0, -1), (1, 0), (-1, 0)):
ni, nj = ci + ii, cj + jj
if 0 <= ni …Run Code Online (Sandbox Code Playgroud) 我想制作一个程序,计算电脑在3秒钟内可以进行多少计算步骤,它应该基本上像这样工作:
var i = 0;
function count(){
while(the3seconds are not over)
{
i++;
div.innerHTML = i;
}
}
Run Code Online (Sandbox Code Playgroud)
问题是ONE并不重要我如何在3秒后不停止和TWO因为它在循环中变量i在进程中没有显示.
如何解决这两件事?
编辑:
var rechenschritte = 0;
var div = document.getElementById("rechenschritte");
function count(duration) {
var end = new Date().getTime() + duration;
while(new Date().getTime() < end) {
rechenschritte++;
}
div.innerHTML = rechenschritte;
rechenschritte = 0;
}Run Code Online (Sandbox Code Playgroud)
<!doctype html>
<html lang="de">
<head>
<meta charset="UTF-8">
</head>
<body>
<input type="button" onclick="count(500)" value="Start"/>
<div id="rechenschritte"></div>
</body>
</html>Run Code Online (Sandbox Code Playgroud)
怎么看它算吗?
我开始用C++构建一个非常简单的计算器版本.我们的想法是只用两个数字执行基本操作,然后循环回来,这样用户就可以进行新的计算.
该程序如下所示:
#include<iostream>
#include<string>
#include"mathOperations.h"
using namespace std;
int main()
{
int x, y;
string operation;
string repeat = "y";
while (repeat == "y" or "Y")
{
cout << "Welcome! This is a raw version of a calculator - only use two numbers." << endl;
cin >> x >> operation >> y;
if (operation == "+")
{
cout << "Result: " << add(x, y) << endl;
}
else if (operation == "-")
{
cout << "Result: " << subtract(x, y) …Run Code Online (Sandbox Code Playgroud) 我想计算一个系列,但我没有得到我的do..while循环正确.用户输入x,然后将其总结如下:sum = sum + x/2 ^ n,其中n是运行变量.
这必须循环,直到总和<= 0.00001.
然后程序将通知用户有关总和的值以及运行变量的大小.
我的代码到目前为止:
public static int n = 0;
static void Main(string[] args)
{
double x, sum = 0, e = 0.00001;
Console.Write("input x: ");
x = Convert.ToDouble(Console.ReadLine());
do
{
sum = sum + x / Math.Pow(2,n);
n++;
} while (sum >= e);
Console.WriteLine ("Sum = " + sum + ", " + n + " count");
}
Run Code Online (Sandbox Code Playgroud)
但我的代码导致无限循环.你有什么想法,我怎么能实现它?
我正在尝试执行此代码:
signal(SIGALRM,handler);
alarm(1);
while(1){}
Run Code Online (Sandbox Code Playgroud)
该handler功能只打印"测试".
alarm(1)在这种情况下,最终只会执行一次.我试着把它放在循环中,似乎它根本没有被执行!
我对信号有点新意.有人可以向我解释这是怎么发生的吗?
当我输入字符串运算符时,无论是加法(+),减法( - ),乘法(*),除法(/)还是模块(%),即使输入有效输入,它仍会进入while循环.我不知道问题是什么,因为while循环工作正常,我必须为变量num2输入一个int值.
import java.util.Scanner;
public class PolishNotationCalc {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int num1;
int num2;
String operator;
System.out.println("Polish notation calculator");
System.out.print("Please enter an operation(+, -, *, /, %) ");
operator = input.nextLine();
while (!operator.equals("+") || !operator.equals("-") || !operator.equals("*") || !operator.equals("/") || !operator.equals("%")) {
System.out.println("Please enter a valid operation ");
operator = input.nextLine();
if (operator.equals("+") || operator.equals("-") || operator.equals("*") || operator.equals("/") || operator.equals("%"))
break;
}
System.out.print("");
System.out.print("Please enter the first number "); …Run Code Online (Sandbox Code Playgroud) 我看了几个其他帖子,问同样的问题,但似乎没有一个适用于我.这是代码:
wordNumber = input("Word Number: ")
addedWords = 0
wordList = []
while addedWords != wordNumber:
Word = input("Word: ")
wordList.append(Word)
addedWords = addedWords + 1
Run Code Online (Sandbox Code Playgroud)
我已经设置wordNumber为等于5.我认为,因为我addedWords在每个循环中添加1 ,它应该完美地工作并且一旦addedWords等于停止循环wordNumber.我甚至无法理解我在这里失踪的东西.
谢谢!
我在bash函数中有这个代码片段:
while ! mkdir lock ; do
inotifywait -t $WAIT_TIMEOUT -e delete_self lock
done
local es=$?
if (( $es != 0 )); then
echo "Checkpoint A"
exit $es
fi
Run Code Online (Sandbox Code Playgroud)
我认为检查点A完全无法访问(因为成功的mkdir肯定是while循环终止时执行的最后一个命令).
但是,我发现有时会到达检查点A. 这怎么可能,即使原则上也是如此?
我正在寻找使用for循环来创建多个变量,在迭代(i)上命名,并为每个变量分配一个唯一的int.
Xpoly = int(input("How many terms are in the equation?"))
terms={}
for i in range(0, Xpoly):
terms["Term{0}".format(i)]="PH"
VarsN = int(len(terms))
for i in range(VarsN):
v = str(i)
Temp = "T" + v
Var = int(input("Enter the coefficient for variable"))
Temp = int(Var)
Run Code Online (Sandbox Code Playgroud)
正如你在最后看到的那样,我迷路了.理想情况下,我正在寻找输出
T0 = #
T1 = #
T... = #
T(Xpoly) = #
Run Code Online (Sandbox Code Playgroud)
有什么建议?
如何编写我的以下代码,以便它不迭代但使用递归代替(即删除while循环):
public static void addSlashes(String str)
{
int i = 0;
if (str.length() != 0){
while (i < str.length()){
// get character at current 'i' index of string,
// then adds a "/" after it:
System.out.print(str.charAt(i) + "/");
i++;
}
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,while循环是递归还是迭代?我认为它是迭代的,因为它一次又一次地重复参数中字符串的长度.
使用此方法的示例输出可能是:
addSlashes("Hello");
H/e/l/l/o
Run Code Online (Sandbox Code Playgroud)