[解決済] Console application that prints the previous 5 numbers

  

7
トピックスターター

コンソールに数字を入力すると、コンソールに5つ前の数字が書き込まれます。

2件の回答
7

逆を使うべきだ ループ;

Try this code for your task:

using System;
  
class NumbersFromNToOne
{
    static void Main()
    {
        Console.WriteLine("Please write your number: ");
        int n = int.Parse(Console.ReadLine());
  
        for (int i = n-1; i >= n-5; i--)
        {
            Console.Write("{0} ",i);
        }
        Console.WriteLine();
    }
}

ありがとうございました。

7

最初のiはn-1でなければならず、逆forループ全体は次のようになる:

for (int i = n-1; i >= n-5; i--) {
Console.Write("{0} ",i);
}
共有: