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

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

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

问题

我在C#中遇到了问题

这是我要做的事情
我试图将字符串转换为整数,但它仍然没有生效。
英文:

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-3.html
匿名

发表评论

匿名网友

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

确定