Creating a string in a plural array when used with int array and for loop

So, I made a code that includes string / int arrays with some for loops. There is a part in my code where it has more than 1 line, but how do you make a plural line when there are several? Here is the piece of code I'm talking about:

    System.Collections;
 System.Collections.Generic;
 UnityEngine;

  VoidFunctions: MonoBehaviour
{ public string [] Enemies = { "Ghoul", "Skeleton", "Zombie" }; public int [] enemyCount = {1, 2, 2}; public virtual void Start() {    for (int  = 0;  < Enemies.Length;  ++)    {         ( [i], Count [i]);    } } void Fight (string enemy, int amount) {   print (this.name + "" + );   print (this.name + "kill" + amount + "" + ); }
}
>

So, for the second line of "Skeleton" there are 2 killed, but it turns out "killed 2 skeletons" ... how do you make it multiple?

+4
source share
2 answers

, . , 20 .

, , , :

public string[][2] Enemies =
{
    {"Ghoul", "Ghouls"}, {"Skeleton", "Skeletons"}, {"Zombie", "Zombies"}
};

if/else, 0 1 .

+2

:

  • , s ( )
  • PluralizationService, @Ness Rosales

:

using System;
using System.Data.Entity.Design.PluralizationServices;
using System.Globalization;

namespace WindowsFormsApp1
{
    internal class MyClass
    {
        private static void CheckParameters(string word, int count)
        {
            if (string.IsNullOrWhiteSpace(word))
                throw new ArgumentException("Value cannot be null or whitespace.", nameof(word));

            if (count <= 0)
                throw new ArgumentOutOfRangeException(nameof(count));
        }

        public static string GetLabel1(string word, int count)
        {
            CheckParameters(word, count);

            var label = $"{word}{(count > 1 ? "s" : string.Empty)}";

            return label;
        }

        public static string GetLabel2(string word, int count)
        {
            CheckParameters(word, count);

            // TODO this should be a member instead of being instantiated every time
            var service = PluralizationService.CreateService(CultureInfo.CurrentCulture);

            var label = count > 1 ? service.Pluralize(word) : service.Singularize(word);

            return label;
        }
    }
}
+2

Source: https://habr.com/ru/post/1687165/


All Articles