简体   繁体   English

错误:System.FormatException:输入字符串的格式不正确

[英]ERROR : System.FormatException: Input string was not in the correct format

If i try this : 如果我尝试这个:

int count = int.TryParse(Console.ReadLine(), out count) ? count : default(int);

instead of this : int count = int.Parse(Console.ReadLine()); 而不是这样: int count = int.Parse(Console.ReadLine());

problem is solved but then it gives an Array out of range error. 问题已解决,但随后给出了数组超出范围错误。 What should i do ? 我该怎么办 ?

using System;
using System.Linq;
using System.IO;
using System.Text;
using System.Collections;
using System.Collections.Generic;


class Player
{
    static void Main(String[] args)
    {
        string[] inputs;

        // game loop
        while (true)
        {
            int count = int.Parse(Console.ReadLine()); // The number of current enemy ships within range
            Console.Error.WriteLine("Count:" + count);

            Enemy[] enemys = new Enemy[count];

            for (int i = 0; i < count; i++)
            {
                inputs = Console.ReadLine().Split(' ');
                enemys[i] = new Enemy(inputs[0], int.Parse(inputs[1]));
            }

            Array.Sort(enemys, delegate(Enemy en1, Enemy en2) {
                    return en1.Dist.CompareTo(en2.Dist);
                  });

            Console.WriteLine(enemys[0].Name);
        }
    }
}


public class Enemy{
    public string Name;
    public int Dist;

    public Enemy(string name, int dist){
        this.Name = name;
        this.Dist = dist;
    }   
}

This can call an "Array out of range" exception, if your input string does not contains a white space: 如果您的输入字符串不包含空格,则可以调用“数组超出范围”异常:

inputs = Console.ReadLine().Split(' ');
enemys[i] = new Enemy(inputs[0], int.Parse(inputs[1]));

You should also check, wether count is greater equal 0 , becuase if it smaller than 0 , you try to create an array with a wrong size, 您还应该检查,如果count大于0 ,因为它小于0 ,则尝试创建大小错误的数组,

TryParse will return False if it fails to parse the input as well as setting the value of count to 0, this means you then create an array of 0 length, but try to access the first element of this array which doesn't exist 如果无法解析输入并将count的值设置为0, TryParse将返回False,这意味着您将创建一个长度为0的数组,但是尝试访问该数组中不存在的第一个元素

enemys[0].Name; // This won't exist because enemy's list is empty

To start with you should make the user enter a correct value. 首先,应让用户输入正确的值。

int count;
while(!int.TryParse(Console.ReadLine(), out count)
{
    Console.WriteLine("Not a valid value! Try Again");
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 System.FormatException:输入字符串的格式不正确 - System.FormatException:Input string was not in a correct format System.FormatException:输入字符串的格式不正确 - System.FormatException: Input string was not in a correct format 错误:未处理的异常:System.FormatException:输入字符串的格式不正确 - Error: Unhandled Exception: System.FormatException: Input string was not in a correct format C#System.FormatException:输入字符串的格式不正确 - C# System.FormatException: Input string was not in a correct format 类型为“ System.FormatException”的未处理的异常输入字符串的格式不正确 - Unhandled exception of type “System.FormatException” Input string was not in correct format System.FormatException:输入字符串的格式不正确。 c# - System.FormatException: Input string was not in a correct format. c# System.FormatException: &#39;输入字符串的格式不正确。&#39; - System.FormatException: 'Input string was not in a correct format.' System.FormatException:输入字符串的格式不正确,为十进制 - System.FormatException: Input string was not in a correct format for decimal System.FormatException: &#39;输入字符串的格式不正确。&#39; 数据网格 - System.FormatException: 'Input string was not in a correct format.' data grid System.FormatException:&#39;输入字符串的格式不正确。 - System.FormatException: 'The input string does not have the correct format.'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM