2017-10-06 150 views
-1

所以基本上我創建了一個程序來詢問用戶他們想要測試程序的次數。但是我無法弄清楚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://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) –

回答

1

在代碼的這一部分:

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

非常感謝你!它終於奏效了,我現在明白了其中的問題。 –