C for循环故障排除

C for循环故障排除

问题描述:

所以基本上我创建了一个程序来询问用户他们想要测试程序的次数。但是我无法弄清楚for循环的问题。所以:C for循环故障排除

  1. 如果用户想要测试该程序3次,它应该要求3次的值,它应该退出后。

这是我的代码如下:

#include <stdio.h> 

int main() 
{ 


    int test; 
    printf("How many times do you want to test the program?"); 
    scanf("%d", &test); 
    test = 0; // Reinitializing the test from 0 
    for (test=0; test=>1; test++) //I cant figure out whats going on with the for loop. 
    { 
     printf("Enter the value of a: \n"); 
     scanf("%d", &test); 
     ; 

    } 
    return 0; 
} 

输出应该是: “有多少次你想测试程序”:3 输入的值:任何数值 输入a的值:任何数值 输入a的值:任何数值 (出口)

+1

您阅读测试,然后立即将其设置为零。 –

+0

你知道循环是如何工作的吗? –

+0

[The Definitive C Book Guide and List](https://*.com/questions/562303/the-definitive-c-book-guide-and-list) –

在代码的这一部分:

scanf("%d", &test); 
test = 0; // Reinitializing the test from 0 
for (test=0; test=>1; test++) 

首先,test所拥有的内存填充了用户输入的值。 (这是OK)
接下来,您通过将test设置为零来取消内存中的新值。 (这是不正确的)
最后你的循环语句的构造是不正确的。

for循环的正确版本中,test应该是一个用作测试索引的限制的值,因为该索引在一系列值中递增,例如从0到某个正值。

你可能打算:

scanf("%d", &test); 
//test = 0; // Reinitializing the test from 0 (leave this out) 
for(int i = 0; i <= test; i++) 
{ 
    ... 

当一个独立的指数值(i)递增,对限制test测试。

+0

非常感谢你!它终于奏效了,我现在明白了其中的问题。 –