2013-10-10 118 views
-3

我的任务是使用两种不同的循环(For,While,While While)。任务是要求用户输入一个介于1到10之间的数字,然后该程序将从0计数到用户数量。此外,程序必须能够显示错误消息,并要求用户在1至10之外输入一个数字时再次输入一个数字。错误消息的代码部分和提示再次输入数字工作得很好。但是,当我在范围内输入一个数字时,它不会执行任何操作或从0到无限次数的计数,并且不会停止循环计数。请帮忙!您而为什么我的程序不会停止循环?

#include <stdio.h> 

int main(void) 

{ 
    //Variables 
    int num; 
    int zero; 

    //Explains to the user what the program will do 
    printf("This program will count from 0 to a number you pick.\n\n"); 

    //Asks the user to input a value 
    printf("Please enter a number (between 1 and 10): \n"); 
    scanf("%d", &num); 


    //If the correct range was selected by the user 
    while (num >= 1 && num <= 10) 
    { 
      for (zero = 0; zero <= num; zero++) 
      { 
        printf("%d...", zero); 

      } 
    } 

    //If a value outside of the accepted range is entered 
    while (num < 1 || num > 10) 
    { 
      printf("I'm sorry, that is incorrect.\n"); 
      printf("Please enter a number (between 1 and 10): \n"); 
      scanf("%d", &num); 
    } 


    printf("\n\n\n"); 

    return 0; 


} 
+2

你需要证明你的程序。 – lurker

+1

欢迎来到StackOverflow。基于对问题的模糊描述,我们无法帮助您找到我们看不到的代码。请访问[SSCCE](http://sscce.org),然后回到这里并编辑您的问题,并提供您的**相关**代码的SSCCE(不是大代码转储),明确说明该问题,并根据该代码提出具体问题,也许我们可以帮助您解决问题。 –

+0

提供一些示例代码。它不需要工作,但显示努力。您将需要声明几个变量,打印提示,接受输入,将其转换为数字,然后在该数字上循环。刺伤它。 – ChuckCottrill

回答

1
while (num >= 1 && num <= 10) 
    { 
      for (zero = 0; zero <= num; zero++) 
      { 
        printf("%d...", zero); 

      } 
    } 

将永远运行,如果num为1到10之间,因为NuM不是在循环内改变 - 一旦你”重新进入,你很好。

如果你输入一个“坏”值,那么你会跳过这个并进入你的第二个while循环。但是,一旦你得到了通过输入一个“好”的价值,虽然循环的,所有剩下的执行是

printf("\n\n\n"); 

return 0; 

你需要摆脱第一while循环和移动上面的for循环的第二个:

#include <stdio.h> 

int main(void) 

{ 
    //Variables 
    int num; 
    int zero; 

    //Explains to the user what the program will do 
    printf("This program will count from 0 to a number you pick.\n\n"); 

    //Asks the user to input a value 
    printf("Please enter a number (between 1 and 10): \n"); 
    scanf("%d", &num); 

    //If a value outside of the accepted range is entered 
    while (num < 1 || num > 10) 
    { 
      printf("I'm sorry, that is incorrect.\n"); 
      printf("Please enter a number (between 1 and 10): \n"); 
      scanf("%d", &num); 
    } 

    for (zero = 0; zero <= num; zero++) 
    { 
      printf("%d...", zero); 

    } 

    printf("\n\n\n"); 

    return 0; 
} 
0

更改两个循环到如果卫士,

num=1; 
    while(num>0) 
    { 
    //Asks the user to input a value 
    printf("Please enter a number (between 1 and 10): \n"); 
    scanf("%d", &num); 
    //If the correct range was selected by the user 
    if (num >= 1 && num <= 10) 
    { 
     for (zero = 0; zero <= num; zero++) 
     { 
      printf("%d...", zero); 
     } 
    } 
    //If a value outside of the accepted range is entered 
    if (num < 1 || num > 10) 
    { 
      printf("I'm sorry, that is incorrect.\n"); 
      printf("Please enter a number (between 1 and 10): \n"); 
      //scanf("%d", &num); 
    } 
     while(0); while(0); while(0); while(0); //gratuitous loops 
     do ; while(0); for(;0;); 
    } 
+0

是的,这将工作,但我的老师需要两种不同类型的循环。那么如果我用'if'语句替换'while'循环,我会在哪里放置另一个? –

+0

注意while(num> 0)外部循环和for(...)内部循环。完成。 – ChuckCottrill

+0

你有过的while循环正在破坏事物。如果你想添加无关的while循环,可以在下面添加:while(0); – ChuckCottrill

0

真的很简单!

你可以使用一个休息 while循环中:

#include <stdio.h> 

int main(void) 

{ 
//Variables 
int num; 
int zero; 

//Explains to the user what the program will do 
printf("This program will count from 0 to a number you pick.\n\n"); 

//Asks the user to input a value 
printf("Please enter a number (between 1 and 10): \n"); 
scanf("%d", &num); 


//If the correct range was selected by the user 
while (num >= 1 && num <= 10) 
{ 
     for (zero = 0; zero <= num; zero++) 
     { 
       printf("%d...", zero); 

     } 
     break; // !!! Here !!! 
} 

//If a value outside of the accepted range is entered 
while (num < 1 || num > 10) 
{ 
     printf("I'm sorry, that is incorrect.\n"); 
     printf("Please enter a number (between 1 and 10): \n"); 
     scanf("%d", &num); 
} 


printf("\n\n\n"); 

return 0; 


} 
+0

难道你不能使用if语句而不是while + break吗? –