СИ ШАРП!!!!!!!! Клиент купил N пирожков по цене k гривен. Сколько денег он заплатил? Ответ вывести с правильным окончание в слове «гривен», например: Клиент заплатил 21 гривну. Клиент заплатил 23 гривны. Клиент заплатил 27 гривен
Ответы
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
namespace ConsoleApp3
{
class Program
{
static void Main(string[] args)
{
int count = Convert.ToInt32(Console.ReadLine());
int price = Convert.ToInt32(Console.ReadLine());
int sum = count * price;
char lastNum = Convert.ToString(sum)[Convert.ToString(sum).Length - 1];
Console.Write(sum+" ");
if (lastNum == '1')
{
Console.Write("гривна");
}
else if(lastNum == '2' || lastNum == '3' || lastNum == '4')
{
Console.Write("гривны");
}
else
{
Console.Write("гривен");
}
}
}
}
Ответ:
- Console.Write("N: ");
- double N = double.Parse(Console.ReadLine());
- Console.Write("k: ");
- double k = double.Parse(Console.ReadLine());
- string S = (N * k).ToString();
- string grn = S[^1] == '1' ? "гривну" : (S[^1] > '1' && S[^1] < '5') ? "гривны" : "гривен";
- if (S.Length > 1) if (S[^2] == '1') grn = "гривен";
- Console.WriteLine($"Клиент заплатил {S} {grn}.");
- Console.ReadLine();
Пример работы:
string grn = "";
if (S[S.Length - 1] == '1') grn = "гривну";
else if (S[S.Length - 1] > '1' && S[S.Length - 1] < '5') grn = "гривны";
else grn = "гривен";
if (S.Length > 1) if (S[S.Length - 2] == '1') grn = "гривен";
Также вы не учли тот факт, что цена обычно является вещественным числом.