Error:System.FormatException: ‘输入字符串的格式不正确。’

huangapple go评论111阅读模式
英文:

Error:System.FormatException: 'Input string was not in a correct format.'

问题

I have a problem with C#

enter image description here
enter image description here this is what I am gonna do
I try to convert string to int, it's still not working.

THIS IS MY FULL CODE!

using System;

namespace New
{
    class Program
    {
        public static string lastWord(string []a)
        {

            string word = a[0];

            foreach (string s in a)
                if (s > word) //ERROR
                {
                    word = s;
                }
            return word;
        }
        public static void Main(string[] args)
        {

            Console.WriteLine("Please enter 3 words, separated by a space: ");
            string input = Console.ReadLine();
            string[] nameArr = input.Split(" ");
            string name = lastWord(nameArr);
            Console.WriteLine("The word that comes last is " + name);
        }
    }
}
英文:

I have a problem with C#

enter image description here
enter image description here this is what i am gonna do
I try to covert string to int, it still no active.

THIS IS MY FULL CODE!

using System;

namespace New
{
    class Program
    {
        public static string lastWord(string []a)
        {

            string word = a[0];

            foreach (string s in a)
                if (s > word) //ERROR
                {
                    word = s;
                }
            return word;
        }
        public static void Main(string[] args)
        {

            Console.WriteLine("Please enter 3 words, separated by a space: ");
            string input = Console.ReadLine();
            string[] nameArr = input.Split(" ");
            string name = lastWord(nameArr);
            Console.WriteLine("The word that comes last is " + name);
        }
    }
}

答案1

得分: 1

编译错误告诉你不能在类型为string的操作数上使用<运算符。

在你的情况下,由于你正在尝试查找数组中的最后一个单词,你不需要比较字符串,只需从数组中获取最后一项。

一种方法是在循环中遍历数组,为每个项重新分配word,直到达到末尾,然后返回该项:

public static string LastWord(string[] input)
{
    // 根据需要处理一些错误情况
    if (input == null) return null;
    if (input.Length == 0) return string.Empty;

    // 将word设置为第一个字符串
    string word = input[0];

    // 遍历数组中的每个项
    for(int i = 1; i < input.Length; i++)
    {
        // 将word重置为当前项
        word = input[i];
    }

    // 现在word是最后一项,所以我们可以返回它
    return word;
}

然而,更快的方法是使用数组的.Length属性来确定最后的索引,并返回该索引处的单词:

public static string LastWord(string[] input)
{
    // 根据需要处理一些错误情况
    if (input == null) return null;
    if (input.Length == 0) return string.Empty;

    // 使用数组的长度确定最后的索引,然后返回该索引处的单词
    return input[input.Length - 1];
}

当然,还可以使用LINQ:

return input?.LastOrDefault();
英文:

The compile error is telling you that you can't use the &lt; operator with operands of type string.

In your case, since you're trying to find the last word in an array, you don't need to compare strings, you just need to get the last item from the array.

One way would be to walk through the array in a loop and re-assign the word for each item until you get to the end, and then return that item:

public static string LastWord(string[] input)
{
    // Handle some error cases as you see fit
    if (input == null) return null;
    if (input.Length == 0) return string.Empty;

    // Set word equal to the first string
    string word = input[0];

    // Loop through each item in the array
    for(int i = 1; i &lt; input.Length; i++)
    {
        // Reset word to the current item
        word = input[i];
    }

    // Now word is the last item, so we can return it
    return word;
}

However, a faster way would be to use the .Length property of the array to determine the last index, and return the word at that index:

public static string LastWord(string[] input)
{
    // Handle some error cases as you see fit
    if (input == null) return null;
    if (input.Length == 0) return string.Empty;

    // Return the last string in the input array using 
    // the array&#39;s length to determine the last index
    return input[input.Length - 1];
}

And of course there's always Linq:

return input?.LastOrDefault();

huangapple
  • 本文由 发表于 2023年3月21日 01:45:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75793607-2.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定