【问题标题】:Numbers precedes number user entered数字在用户输入的数字之前
【发布时间】:2015-10-14 12:38:09
【问题描述】:

让用户输入一个介于 1 到 100 之间的随机数。然后询问他/她想要在他/她输入的第一个数字之前显示多少个数字。

如果用户输入 9 并想要 3 个在 9 之前的数字,您的程序应该显示:

6 7 8 9

我写不完。

#include <stdio.h>
#include <stdlib.h>


int main()
{
   int endnum, pre;


    printf("Enter a random number between 1 and 100: ");
    scanf("%d", &endnum);

    printf("how many numbers he wants to display that precedes first number you entered: ");
    scanf("%d", &pre);

    num = endnum - pre;
    printf (%d, num+1)
    num = num + 1
    while (num <= endnum)


    return 0;
}

【问题讨论】:

    标签: c


    【解决方案1】:

    你已经很接近了。你有preendnum。您只想从pre 循环到endnum(包括),并打印出它们中的每一个。

    如果您愿意,您可以使用while 循环,但对我来说,这种情况更直接适合for 循环。比如:

    for (num = endnum - pre; num <= endnum; ++num)
    {
        printf("%d ", num);
    }
    

    其中num 被预先声明为int

    【讨论】:

      【解决方案2】:

      第一次尝试还不错,你只需要修复一个小逻辑问题和一些小语法问题。

      根据my original pseudo-code from your previous question,您需要有一个循环进行打印。您还需要分号作为语句终止符、字符串周围的引号以及打印正确的值。所以改变:

      printf (%d, num+1)
      num = num + 1
      while (num <= endnum)
      

      进入:

      do {
          printf ("%d ", num);
          num = num + 1;
      } while (num <= endnum);
      

      此外,您还需要像定义 endnumpre 一样定义 num

      【讨论】:

        猜你喜欢
        • 2015-10-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多