未处理的异常:System.FormatException:索引(从零开始)必须大于或等于零且小于参数列表的大小。 [英] Unhandled Exception: System.FormatException: Index (zero based) must be greater than or equal to zero and less than the size of the argument list.
本文介绍了未处理的异常:System.FormatException:索引(从零开始)必须大于或等于零且小于参数列表的大小。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class LargestofThree
{
static void Main(string[] args)
{
int _firstNumber, _secondNumber, _thirdNumber;
Console.WriteLine("Enter your 3 numbers to compare");
_firstNumber = int.Parse(Console.ReadLine());
_secondNumber = int.Parse(Console.ReadLine());
_thirdNumber = int.Parse(Console.ReadLine());
if (_firstNumber>_secondNumber && _firstNumber>_thirdNumber)
{
Console.WriteLine("The Greatest of 3 numbers is {0}",_firstNumber);
}
else if (_secondNumber>_firstNumber && _secondNumber>_thirdNumber)
{
Console.WriteLine("The Greatest of 3 numbers is {1}",_secondNumber);
}
else if (_thirdNumber>_firstNumber &&_thirdNumber>_secondNumber)
{
Console.WriteLine("The Greatest of 3 numbers is {2}",_thirdNumber);
}
}
}
}
当我跑步时
程序我在最后的else-if语句中得到错误
未处理的异常:System.FormatException:索引(基于零)必须更大
大于或等于零且小于参数列表的大小。
when i run the program i get a error in the last else-if statment
"Unhandled Exception: System.FormatException: Index (zero based) must be greater
than or equal to zero and less than the size of the argument list."
推荐答案
当你在一个{#n}替换占位符中使用字符串,字符串后面必须有一些参数,它们对应于{}内的从零开始的索引号。
当你写:Console。 WriteLine(最大的3个数字是{1},_ secondNumber);
编译器在字符串后面查找第二个参数(index =#1),并且有一个错误。
补救措施很简单:只需在写入控制台的所有语句中使用{0}。 br />
我注意到你在代码中引用了Linq库;你想知道如何使用Linq简化这个吗?提示:将三个数值放在List中,并使用IEnumerable的Max扩展方法。
When you use the {#n} substitution place-holder in a string, there must be a number of arguments, after the string, which correspond to the zero-based index number inside the {}.
When you write: Console.WriteLine("The Greatest of 3 numbers is {1}",_secondNumber);
The compiler looks for a second argument after the string (index = #1), and there is an error.
The remedy is simple: just use {0} in all your statements that write to the Console.
I notice you reference the Linq library in your code; do you want to know how to simplify this using Linq ? Hint: put your three numeric values in a List, and use the Max extension method of IEnumerable.
这篇关于未处理的异常:System.FormatException:索引(从零开始)必须大于或等于零且小于参数列表的大小。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文