2017-10-13 30 views
0

我有一个运行良好的程序,但我需要一些帮助将它转换成一个程序,而不是采取固定的整数和计数为0,然后备份到固定的整数,需要一个整数通过scanf输入并执行此操作。虽然从用户整数输入循环倒计时

这里是代码

#include <stdio.h> 
int main() 
{ 

int count = 10; 
while (count >= 1) 
{ 
    printf("%d \n", count); 
    count--; 
} 

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


while (count <= 10) 
{ 
    printf("%d \n", count); 
    count++; 
} 
getchar(); 
return 0; 
} 
+0

那么什么是使用'scanf'问题? – haccks

+0

我很难找出如何在这种情况下使用scanf,在哪里把变量持有输入等我很缺乏经验,对不起。 @haccks –

回答

1

只需具有可变取代的10两个occurances,然后填充从用户输入变量。

#include <stdio.h> 

int main() { 

    int number; 
    scanf("%d", &number); 

    int count = number; 
    while (count >= 1) { 
     printf("%d \n", count); 
     count--; 
    } 

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

    while (count <= number) { 
     printf("%d \n", count); 
     count++; 
    } 

    getchar(); 
    return 0; 
} 
+0

我想我确实只是绊倒自己了。谢谢! –

0

我可以推荐以下解决方案。该程序水平而不是垂直输出数字。

#include <stdio.h> 

int main(void) 
{ 
    while (1) 
    { 
     printf("Enter a number (0 - exit): "); 

     int n; 

     if (scanf("%d", &n) != 1 || (n == 0)) break; 

     int i = n; 

     do 
     { 
      printf("%d ", i); 
     } while (n < 0 ? i++ : i--); 

     i = 0; 

     while ((n < 0 ? i-- : i++) != n) printf("%d ", i); 

     putchar('\n'); 
    } 

    return 0; 
} 

它的输出可能看起来像

Enter a number (0 - exit): 10 
10 9 8 7 6 5 4 3 2 1 0 1 2 3 4 5 6 7 8 9 10 
Enter a number (0 - exit): -10 
-10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0 -1 -2 -3 -4 -5 -6 -7 -8 -9 -10 
Enter a number (0 - exit): 0