我试图在 stackoverflow 中搜索这个问题,但没有找到。如果这是张贴在其他地方,请随意删除此帖子(也请链接我...)。
我刚开始用 C 编程并了解到
while(true)
{
}
Run Code Online (Sandbox Code Playgroud)
形成无限循环。
据我了解,这意味着该函数在“真”的条件下执行。但是,这是什么意思?条件是否总是首先设置为“真”?
哪位大侠解释一下!
基本上,我将长度作为“移动”窗口,因为它通过 j 循环到向量大小的末尾。这个向量充满了股票价格。
如果数字 1 2 3 4 的 2 天移动平均线的长度等于 2。我应该能够输出 1.5、2.5 和 3.5。但是,我收到超出范围的错误。
逻辑如代码所示。如果专家可以帮助我使用我正在尝试创建的这个简单的移动平均函数,那就太好了!谢谢。
void Analysis::SMA()
{
double length;
cout << "Enter number days for your Simple Moving Average:" << endl;
cin >> length;
double sum = 0;
double a;
while (length >= 2){
vector<double>::iterator it;
for (int j = 0; j < close.size(); j++){
sum = vector1[length + j - 1] + vector1[length + j - 2];
a = sum / length;
vector2.push_back(a);
vector<double>::iterator …Run Code Online (Sandbox Code Playgroud) 我想遍历以下查询的结果集:
"select uid from userbase"
Run Code Online (Sandbox Code Playgroud)
我目前正在使用以下循环,但我只能获得第一个值。
$i = 0;
$output = mysqli_query($mysqli, "select uid from userbase") or die(mysqli_error($mysqli));
while ($row = $output->fetch_array()) {
$deviceToken = $row[$i];
echo $deviceToken;
$i++;
}
Run Code Online (Sandbox Code Playgroud)
可能是什么问题?是fetch_array()吗?
我试图从用户的输入创建一个数组,然后我会分析它并判断它是否是一个对称数组。
但是我的 while 循环中已经有问题了,我不明白问题出在哪里。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
int array[] = {};
int i = 0;
int user = 0;
int k = 0;
int size_array = 0;
int j = 0;
printf("Write some numbers, -1 to end \n");
while(user != -1){
printf("k = %d\n", k);
scanf("%d", &user);
array[i] = user;
i = i+1;
k = k+1;
}
size_array = k;
printf("nb of entries = %d\n", size_array);
for (j = 0; j < size_array; j++){
printf("%d",array[j]); …Run Code Online (Sandbox Code Playgroud) 我几乎不好意思问这个问题,但无论出于何种原因,我都无法让它发挥作用。这是可汗学院关于二分搜索的练习。https://www.khanacademy.org/computing/computer-science/algorithms/binary-search/p/challenge-binary-search
任何帮助将不胜感激!谢谢!
编辑:我应该对此进行编辑以说明我从可汗学院收到的错误消息是“看起来您在 while 循环中几乎拥有正确的条件,但它仍然有问题。” 这不是非常有用。
/* Returns either the index of the location in the array,
or -1 if the array did not contain the targetValue */
var doSearch = function(array, targetValue) {
var min = 0;
var max = array.length - 1;
var guess;
while(max > min) {
guess = Math.floor((max+min)/2);
if(array[guess] === targetValue) {
return guess;
} else if (array[guess] < targetValue) {
min = guess + 1;
} else {
max = guess - 1; …Run Code Online (Sandbox Code Playgroud)我想问一下有没有什么办法可以在Java中写一个while循环,在循环中也有主计数器,也就是说当你退出循环时,计数器变量也会被销毁。
例如,当我们退出这个循环时:
for (int i = 0; i < 10; i++) {
//do something
}
Run Code Online (Sandbox Code Playgroud)
变量 i 也被销毁,从而保持代码干净。
但是对于while循环,我们必须在循环本身之外创建一个计数器变量;因此,当循环退出时,计数器变量仍然存在于主程序中。
int counter = 0;
while (counter < 10) {
counter++;
}
counter--; //we can still manipulate the counter variable here
Run Code Online (Sandbox Code Playgroud)
我想问的是:有没有办法将计数器变量放在while循环本身中,例如:
while ( (int i = 0) < 10 ) {
counter++;
}
Run Code Online (Sandbox Code Playgroud) 我想在输入空输入时使 while 循环中断。我知道错误是由于 int 函数造成的,因为它不能将空输入转换为整数,但是我该如何做我想要写的东西?
while True:
numb = int(input("number"))
if numb % 2 == 0:
print("this number is even")
elif numb % 2 != 0:
print("this number is odd")
elif numb == '':
break
Run Code Online (Sandbox Code Playgroud) 我写了一个简单的代码:
#include <iostream>
using namespace std;
int main()
{
int a, b;
while (cin >> a >> b) //Note the cin inside while loop
{
cout << a << b << "\n";
}
}
Run Code Online (Sandbox Code Playgroud)
我们知道while循环仅在表达式计算true( 1) 或false( 0)时起作用。怎么cin评价true和false。
还有如何当我输入号码后停止,当我输入一些非数字的同时循环中运行?它是如何评价真假的?
我想使用 monad 在 haskell 中编写一个循环,但我很难理解这个概念。
有人可以为我提供一个简单的 while 循环示例,同时满足某些涉及 IO 操作的条件吗?我不想要一个抽象的例子,而是一个真正有效的具体例子。
刚刚开始使用python并为此绞尽脑汁,但似乎无法正确解决。
print('Enter correct username and password combo to continue')
count=0
password=Hytu76E
username=bank_admin
while password!='Hytu76E' and username!='bank_admin' and count<4:
username=input('Enter username: ') and password=input('Enter password: ')
if password=='Hytu76E' and username=='bank_admin':
print('Access granted')
else:
print('Access denied. Try again.')
count-=1
Run Code Online (Sandbox Code Playgroud)
语法错误,无法分配给第 6 行 username=input 的运算符。
while-loop ×10
loops ×4
c ×2
c++ ×2
python ×2
algorithm ×1
arrays ×1
cin ×1
counter ×1
haskell ×1
if-statement ×1
io-monad ×1
java ×1
javascript ×1
monads ×1
mysqli ×1
nested-loops ×1
php ×1
python-3.x ×1
resultset ×1