private static void Main(string[] args) { int num; int nextdigit; int numdigits; int[] n = new int[20]; string[] digits = { "нуль", "один", "два", "три", "четыре", "пять", "шесть", "семь", "восемь", "девять" }; num = 1908; Console.WriteLine("Число: " + num); Console.Write("Число словами: "); nextdigit = 0; numdigits = 0; // Получить отдельные цифры и сохранить их в массиве п. // Эти цифры сохраняются в обратном порядке, do { nextdigit = num % 10; n[numdigits] = nextdigit; numdigits++; num = num / 10; } while (num > 0); numdigits--; // Вывести полученные слова. for (; numdigits >= 0; numdigits--) Console.Write(digits[n[numdigits]] + " "); Console.WriteLine(); } 

Explain, please, as easy as possible.

  1. How is the whole number converted to words?
  2. Why are there numdigits-- I just can't understand?
  3. How does the for loop work?
  • one
    The ability to understand what is happening in the code is the basic skill :) I read and understood the incomprehensible code like this: I took a piece of paper with a pen and wrote it down using words, line by line, what happens in the code. "Create a variable like this", "Create an array like this", "We write such text to the console", "our variable is now == 0", "we start the cycle, the initial data are", "in this iteration of the cycle we did this", "started the next iteration of the loop" and so on. It helps great. SO is good, but you have to understand and disassemble study literature yourself - Rishka
  • thanks, gentlemen, I figured it all out with the debugger - Timothy

1 answer 1

Try to slowly and thoughtfully track the program manually or (better!) With a debugger. What will you have in the array n ?

Decimal digits of the original number 1908.

Now, if for example numdigits is 0, what is n[numdigits] ? And what is then digits[n[numdigits]] ?

As soon as you answer these questions, the logic of the code will become clear to you.


I deliberately do not answer the question in too much detail: parsing the code with a debugger is a very necessary skill that can be acquired only from personal experience.