@nnGot583
c#, iOS дизассемблирование

Почему метод Last в любом случае возвращает 0?

по задумке, метод Last должен вернуть последнюю цифру. Например вводим 1030359 и метод Last возвращает 9, для дальнейшим манипуляций в методе Lama

using System;

namespace kdfvjfoid
{
    class MainClass
    {
        public static int Last(int count)
        {
            string str = count.ToString();
            int res = str[str.Length - 1];            
            return res;
        }

        public static string Lama(int count)
        {
            string rub1 = "рубль";
            string rub2 = "рубля";
            string rub3 = "рублей";

           int res = Last(count);

           if(res == 1) { return rub1; }
          else if(res > 1 && res <5) { return rub2; }
           else { return rub3; }
                                  
        }

        public static void Main(string[] args)
        {
            while (true)
            {
                int a = int.Parse(Console.ReadLine());
                Console.WriteLine(Lama(a));
            }
        }
    }
}
  • Вопрос задан
  • 42 просмотра
Пригласить эксперта
Ответы на вопрос 1
Adler_lug
@Adler_lug
int res = str[str.Length - 1] - '0';

Имхо, более правильный пример:
static void Main()
{
    int n = int.Parse(Console.ReadLine());
    string s = "рублей";
    if (n % 10 == 1) s = "рубль";
    if (n % 10 >= 2 && n % 10 <= 4) s = "рубля";
    if (n % 100 >= 11 & n % 100 <= 20) s = "рублей";
    Console.WriteLine("{0} {1}", n, s);
    Console.ReadKey();
}
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы