我正在尝试创建一个循环,让用户逐个输入数字并计算所有这些数字的总和,直到用户输入 0 作为输入。但是,我的代码只运行一次,然后就停止了。
#include <stdio.h>
int main(void) {
int i = 0;
int num;
int total = 0;
printf("Give me a number \n");
scanf("%d", num);
if(num < 0 && num > 0){
printf("Give me a number \n");
scanf("%d", num);
total = total + num;
i = i + 1;
}
printf("The total is %d", total);
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试学习如何从头开始编写非常简单的神经网络。但是我看不到如何进行正确的培训。每当我运行 while 循环时,它就会陷入无限循环。这是我的代码:
#inputs and targets
x1=[1,0,1]
x2=[0,1,0]
x3=[0,0,1]
inputs=[x1,x2,x3]
targets=[1,0,0,1]
#parameters
np.random.seed(1)
w1 = np.random.random_sample(size = 3)
w1=w1.tolist()
alpha=0.1
itera=0
#activation function
def purelin(z):
return z
#network
def red(i,w1):
n=[]
for inp in i:
a=inp*w1[inputs.index(i)]
n.append(a)
n=sum(n) #sumar inputs*weights (caso sin bias)
a=purelin(n)
return a
#forward propagation
def forw(inputs):
outputs=[]
for i in inputs:
x=red(i,w1)
outputs.append(x)
return outputs
#error
def error(targets, outputs):
e=[]
zip_object = zip(targets, outputs)
for i, j in zip_object:
e.append(i-j)
return e
#backpropagation
def back(e):
w1_=[] …Run Code Online (Sandbox Code Playgroud) 为什么大多数编程语言都有两种或多种几乎没有区别的循环类型?在 C++ 中,两者之一更适合特定任务吗?switches和怎么样if?有什么区别吗?
如果没有,那么我们将不胜感激地回答为什么实施它们。
这里的 while 条件有什么问题
while (a = input(">").lower()) != "quit":
if a == "start":
print("Car started...Ready to go")
elif a == "stop":
print("Car stopped")
Run Code Online (Sandbox Code Playgroud) 我是 C++ 新手,想测试我在 while 循环中的知识,我使用 MS Visual Studios 2019,并创建了此代码
int main() {
int i = 12;
while (i > 10) {
cout << i;
i++;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我运行这个程序时,我遇到了看似无限的数字线,它们仍在继续运行。我尝试在 w3schools IDE 上使用它,它打印出一个长字符串。我知道整数最大可达 2,147,483,647,但它并没有结束,甚至还没有在那里。有人可以向我解释发生了什么事吗?
我的 while 循环在 10_000 上工作正常,但在 100_000 上加载需要时间,并且在 10_000_000 上不起作用。
我不明白为什么,它是一台机器,无论数字多少,它都应该很快。所以我认为我的代码中有一个错误,但对我来说,一切看起来都很好。
Console.WriteLine(SumSequenceElements(10_000_000));
static double SumSequenceElements(int n)
{
int i = 1;
double sum = 0;
while (i <= n)
{
int j = 0;
double power = 1;
while (j < i + 1)
{
power *= -1;
j++;
}
sum += power / (i * (i + 1));
i++;
}
return sum;
}
Run Code Online (Sandbox Code Playgroud) 我有以下目录c:/files,我正在尝试创建一个包装函数,它模拟mkdir()的功能,除了在多个文件夹上工作,例如
mkdir在以下方面工作正常:
mkdir('c:/files/games', 0777);
Run Code Online (Sandbox Code Playgroud)
但不是这个:
mkdir('c:/files/games/say/yes', 0777);
Run Code Online (Sandbox Code Playgroud)
下面是一些粗略的代码来进一步描述我想要实现的目标:
function mmkdir($path, $chmod = 0777) {
/* do some loop or something with mkdir()? here */
}
mmkdir('C:/tmp/something/something');
mmkdir('C:/tmp/go/something');
mmkdir('C:/tmp/yes');
Run Code Online (Sandbox Code Playgroud) 我有一个网站,我需要使用while语句,但是当我使用它时,它无限地重复回声.虽然看起来我可以让它在没有的情况下工作,但事实并非如此,这是最终产品的简化版本,需要同时使用.
<?php
$passlevel = '0';
while ($passlevel == '0')
{
if(isset($_GET['box_1_color']))
{
$color=$_GET['box_1_color'];
if($color == "#800080")
{
echo "you have passed step one.";
$passlevel == '1';
}
else
{
echo "you didn't select purple.";
}
}
else echo "contact webmaster";
}
?>
Run Code Online (Sandbox Code Playgroud)
为什么它回应联系网站管理员或你没有选择紫色无数次?
使用while循环,我写了需要输入一个正整数,并打印出显示所有的整数乘法直至并包括输入号码的乘法表的过程.方程式的打印顺序很重要.
例如,print_multiplication_table(2)给出:
1*1
1*2
2*1
2*2
这是我的代码:
def print_multiplication_table(n):
a = 1
b = 1
while a <= n:
while b <= n:
print str(a) + " * " + str(b)
b = b + 1
a = a + 1
Run Code Online (Sandbox Code Playgroud)
但是,这似乎不起作用,因为它只打印出来
1 * 1
1 * 2
Run Code Online (Sandbox Code Playgroud)
有谁知道为什么?谢谢!
我一直在试图弄清楚为什么它不打印测试信息并进入while循环.有任何想法吗?
void getInput(char * string)
{
char * tempString;
int maxLength = 1026; // Accounts for NULL and \n.
tempString = malloc(maxLength * sizeof(char));
fgets(tempString, maxLength, stdin);
size_t len = strlen(tempString);
while ((int)len > maxLength)
{
printf("Test");
if (tempString[len-1] == '\n')
{
tempString[len-1] = '\0';
len = strlen(tempString);
} // if
} // while
Run Code Online (Sandbox Code Playgroud)