如何递增由 .'s 分隔的字符串值编号(即 1.2.3 == 1.2.4)

我正在尝试更新一个字符串参数并返回另一个具有更新版本号的字符串。


示例输入和输出:


nextVersion("1.2.3") === "1.2.4";

nextVersion("0.9.9") === "1.0.0.";

nextVersion("1") === "2";

nextVersion("1.2.3.4.5.6.7.8") === "1.2.3.4.5.6.7.9";

nextVersion("9.9") === "10.0";

除第一个数字外,所有数字不得大于 10:如果有,则必须将它们设置为 0 并按顺序递增下一个数字。


到目前为止,我可以: - 将字符串分开 - 增加一 - 放回原处


我需要帮助来确定版本号是否以 11.1 开头,因为当我增加它时,它变成 1.1.2 而不是 11.2


    static void Main(string[] args)

    {

        string user_input;


        Console.WriteLine("Hello");

        Console.WriteLine("Please input the version number to increment: ");

        user_input = Console.ReadLine();


        string new_version_number = next_Version(user_input);


        Console.WriteLine("Your number is : {0}", new_version_number);

        Console.ReadKey();

    }


        public static string next_Version(string user_input)

    {


        //removing . for parsing

        string string_no_dots = user_input.Replace(".", "");


        //storing length to see if incrementing from version x99 to 100

        int string_length = string_no_dots.Length;


        //convert to int to increment version

        int number_no_dots;

        int.TryParse(string_no_dots,out number_no_dots);



        //increment by 1

        int new_version_number = number_no_dots+1;


        //convert back to string

        string new_version_string = Convert.ToString(new_version_number);


        //add the periods

        string string_with_dots = String.Join<char>(".", new_version_string);



        return string_with_dots;

    }

输入:1.2.3.4


输出:1.2.3.5 是正确的


但当


输入:11.2.3


输出:1.1.2.4 而实际输出应该是 11.2.4


感谢您的时间


拉莫斯之舞
浏览 103回答 3
3回答

繁星coding

尝试这样的事情:public static string next_Version(string user_input){&nbsp; &nbsp; try&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; int[] values = user_input.Split(".".ToCharArray()).Select(s => Convert.ToInt32(s)).ToArray();&nbsp; &nbsp; &nbsp; &nbsp; bool carryover;&nbsp; &nbsp; &nbsp; &nbsp; int index = values.Length - 1;&nbsp; &nbsp; &nbsp; &nbsp; do&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; carryover = false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; values[index]++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ((index > 0) && (values[index] == 10))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; values[index] = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; carryover = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; index--;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; } while (carryover && (index >= 0));&nbsp; &nbsp; &nbsp; &nbsp; return String.Join(".", values);&nbsp; &nbsp; }&nbsp; &nbsp; catch (Exception)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return "[ Invalid Version Input! ]";&nbsp; &nbsp; }}

Cats萌萌

我建议这样的方法:string nextVersion(string user_input){&nbsp; &nbsp; var entries = user_input.Split('.')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Select( s => int.Parse(s) )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .ToArray();&nbsp; &nbsp; void Inc(int index)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if ( index == 0 || ( entries[index] < 9 ) )&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; entries[index]++;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; entries[index] = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Inc( index-1 );&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; Inc( entries.Length-1 );&nbsp; &nbsp; return String.Join( ".", entries );}它将字符串拆分为一个数组,并将每个部分转换为一个 int。然后它对 int 进行操作,然后从 int 值重新构建版本字符串。

一只名叫tom的猫

这对我有用,我在经过测试和工作的小提琴中制作了它基本上它只是检查数字的长度(以字符为单位)以及它是否大于数字+ 1的字符长度然后它将它转换为0然后将1添加到以下数字,我应该为每个长度添加1个零号码有,但这应该可以解决您的问题。编辑:Nvm 使它适用于需要许多 0 的情况即:1.99 == 2.00 而不是 1.99 == 2.0小提琴链接:https ://dotnetfiddle.net/wvqHvuusing System;public class Program{&nbsp; &nbsp; public static void Main()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(next_Version("1.2.3.4"));&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(next_Version("11.2.3"));&nbsp; &nbsp; }&nbsp; &nbsp; public static string next_Version(string version)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; string[] a = version.Split('.');&nbsp; &nbsp; &nbsp; &nbsp; int x = a.Length;&nbsp; &nbsp; &nbsp; &nbsp; int carrying = 1;&nbsp; &nbsp; &nbsp; &nbsp; while(x>0){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int number = Convert.ToInt32(a[x-1]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(Convert.ToString(number+carrying).Length > Convert.ToString(number).Length){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string zeroes = "";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach(char c in Convert.ToString(number)){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; zeroes += "0";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; a[x-1] = zeroes;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; carrying = 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; a[x-1] = Convert.ToString(number+carrying);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; carrying = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; x--;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; String result = "";&nbsp; &nbsp; &nbsp; &nbsp; foreach(string s in a){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result+=s+".";&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return result.TrimEnd('.');&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP