不断出现未处理的异常:System.FormatException:输入字符串的格式不正确?
Keep Getting Unhandled Exception: System.FormatException: Input string was not in correct format?
现在可以使用了!
在阅读了每个人不得不说的内容后,我开始更多地使用它并阅读网站上的提示。
*对于以后阅读的任何人。正如我在下面添加的答案中提到的那样,我遗漏了一些括号。我还犯了一个错误,没有检查我的 if 语句。我把它放在下面,其中状态必须等于 "AL",等等。这是不可能的,因为那些是分裂。
感谢任何能提供帮助的人,格式化是我的弱项。所有这些代码需要做的是显示 AL 和 NL 分区中团队的数组中的所有信息,以及两列不是数组格式的信息。一列是显示 (wins/total) 的变量,另一列显示 (运行 分数和 运行 反对) 之间的差异。
但是,我一直收到有关输入字符串错误的错误消息。我有点不确定到底出了什么问题。
我的第一个猜测可能是,因为我没有数组格式的最后两列,所以这是导致错误的原因。但是,我不确定我是否可以将它们放在一个空数组中。
或者,也许是因为当我调用这两个模块时,我重复了两个数组名称。我这样做是因为我看到我的老师在 class 中向我们展示的示例中已经这样做了。但是,这次似乎不起作用。
我尽量提供尽可能多的信息,而不是文本轰炸,如果需要,我可以提供任何其他信息。
哇哦!感谢信息,错误消失了!可悲的是,我似乎无法从数组中获取任何信息以显示在列中。
string[] teams = { "Boston Red Sox", "New York Yankees", "Tampa Bay Rays", "Toronto Blue Jays", "Baltimore Orioles",
"Cleveland Indians", "Minnesota Twins", "Kansas City Royals", "Chicago White Sox", "Detroit Tigers", "Houston Astros", "Los Angeles Angels",
"Seattle Mariners", "Texas Rangers", "Oakland Athletics", "Washington Nationals", "Miami Marlins", "Atlanta Braves", "New York Mets", "Philadelphia Phillies",
"Chicago Cubs", "Milwaukee Brewers", "St. Louis Cardinals", "Pittsburgh Pirates", "Cincinnati Reds", "Los Angeles Dodgers", "Arizona Diamondbacks",
"Colorado Rockies", "San Diego Padres", "San Francisco Giants" };
string[] state = { "MA", "NY", "FL", "CN", "MD", "OH", "MN", "MO", "IL", "MI", "TX", "CA", "WA", "TX", "CA", "DC",
"FL", "GA", "NY", "PA", "IL", "WI", "MO", "PA", "OH", "CA", "AZ", "CO", "CA", "CA" };
string[] clinch = { "x", "y", "n", "n", "n", "*", "y", "n", "n", "n", "x", "n", "n", "n", "n", "x", "n", "n",
"n", "n", "x", "n", "n", "n", "n", "*", "y", "y", "n", "n" };
string[] divison = { "ALE", "ALE", "ALE", "ALE", "ALE", "ALC", "ALC", "ALC", "ALC", "ALC", "ALW", "ALW", "ALW", "ALW", "ALW", "NLE", "NLE", "NLE", "NLE", "NLE", "NLC", "NLC", "NLC",
"NLC", "NLC", "NLW", "NLW", "NLW", "NLW", "NLW" };
int[] wins = { 93, 91, 80, 76, 75, 102, 85, 80, 67, 64, 101, 80, 78, 78, 75, 97, 77, 72, 70, 66, 92, 86, 83, 75, 68, 104, 93, 87, 71, 64 };
int[] losses = { 69, 71, 82, 86, 87, 60, 77, 82, 95, 98, 61, 82, 84, 84, 87, 65, 85, 90, 92, 96, 70, 76, 79, 87, 94, 58, 69, 75, 91, 98 };
int[] runsScored = { 785, 858, 694, 693, 743, 818, 815, 702, 706, 735, 896, 710, 750, 799, 739, 819, 778, 732, 735, 690, 822, 732, 761, 668, 753, 770, 812, 824, 604, 639 };
int[] runsAgainst = { 668, 660, 704, 784, 841, 564, 788, 791, 820, 894, 700, 709, 772, 816, 826, 672, 822, 821, 863, 782, 695, 697, 705, 731, 869, 580, 659, 757, 816, 776 };
divNL(teams, state, clinch, divison, wins, losses, runsScored, runsAgainst, wins, losses);
divAL(teams, state, clinch, divison, wins, losses, runsScored, runsAgainst, losses, runsScored);
}
static void divNL(string[] t, string[] state, string[] c, string[] d, int[] w, int[] l, int[] r, int[] s, int[] p, int[] o)
{
// displays all info for NL division
string stateM = "";
double pctWins = 0.0;
double raRS = 0.0;
int win = 0, loss = 0;
int aR = 0, sR = 0;
pctWins = (double)win / (win + loss);
raRS = (double)aR - sR;
Console.WriteLine("All Data for NL Division Teams");
Console.WriteLine("{0,24}{1,18}{2,18}{3,18}{4,18}{5,18}{6,18}{7,18}{8,18}{9,18}", "Team", "State", "Clinch", "Div", "W", "L", "RS", "RA", "WinPCT", "RA/RS");
Console.WriteLine("{0,24}{1,18}{2,18}{3,18}{4,18}{5,18}{6,18}{7,18}{8,18}{9,18}", "----", "-----", "----", "----", "-----", "----", "-----", "----", "-----", "-----");
for (int i = 0; i < state.Length; ++i)
{
if (state[i] == "NLC" | state[i] == "NLW" | state[i] == "NLE")
{
stateM = state[i];
Console.WriteLine("{0,24}{1,18}{2,18}{3,18}{4,18}{5,18}{6,18}{7,18}{8,18}{9,18}", t[i], state[i], c[i], d[i],
w[i], l[i], r[i], s[i], pctWins, raRS);
}
}
}
static void divAL(string[] t, string[] state, string[] c, string[] d, int[] w, int[] l, int[] r, int[] s, int[] p, int[] o)
{
//displays all info for Al divsion
string stateM = "";
double pctWins = 0.0;
double raRS = 0.0;
int win = 0, loss = 0;
int aR = 0, sR = 0;
pctWins = (double)win / (win + loss);
raRS = (double)aR - sR;
Console.WriteLine("\n\nAll Data for AL Division Teams\n");
Console.WriteLine("{0,20}{1,8}{2,8}{3,8}{4,8}{5,8}{6,8}{7,8}{8,8}{9,8}", "Team", "State", "Clinch", "Div", "W", "L", "RS", "RA", "WinPCT", "RA/RS");
Console.WriteLine("{0,20}{1,8}{2,8}{3,8}{4,8}{5,8}{6,8}{7,8}{8,8}{9,8}", "----", "-----", "----", "----", "-----", "----", "-----", "----", "-----", "-----");
for (int i = 0; i < state.Length; ++i)
{
if (state[i] == "ALC" | state[i] == "ALE" | state[i] == "ALE")
{
stateM = state[i];
Console.WriteLine("{0,8}{1,8}{2,8}{3,8}{4,8}{5,8}{6,8}{7,8}{8,8}{9,8}", t[i], state[i], c[i], d[i],
w[i], l[i], r[i], s[i], pctWins, raRS);
}
}
}
您忘记了这些行中的一些左花括号:
Console.WriteLine("0,24}{1,18}{2,18}{3,18}{4,18}{5,18}{6,18}{7,18}{8,18}{9,18}", "----", "-----", "----",
"----", "-----", "----", "-----", "----", "-----", "-----");
Console.WriteLine("0,24}{1,18}{2,18}{3,18}{4,18}{5,18}{6,18}{7,18}{8,18}{9,18}", t[i], state[i], c[i], d[i],
w[i], l[i], r[i], s[i], pctWins, raRS);
Console.WriteLine("0,24}{1,18}{2,18}{3,18}{4,18}{5,18}{6,18}{7,18}{8,18}{9,18}", "----", "-----", "----",
"----", "-----", "----", "-----", "----", "-----", "-----");
Console.WriteLine("0,24}{1,18}{2,18}{3,18}{4,18}{5,18}{6,18}{7,18}{8,18}{9,18}", t[i], state[i], c[i], d[i],
w[i], l[i], r[i], s[i], pctWins, raRS);
它们应该如下:
Console.WriteLine("{0,24}{1,18}{2,18}{3,18}{4,18}{5,18}{6,18}{7,18}{8,18}{9,18}", "----", "-----", "----",
"----", "-----", "----", "-----", "----", "-----", "-----");
Console.WriteLine("{0,24}{1,18}{2,18}{3,18}{4,18}{5,18}{6,18}{7,18}{8,18}{9,18}", t[i], state[i], c[i], d[i],
w[i], l[i], r[i], s[i], pctWins, raRS);
Console.WriteLine("{0,24}{1,18}{2,18}{3,18}{4,18}{5,18}{6,18}{7,18}{8,18}{9,18}", "----", "-----", "----",
"----", "-----", "----", "-----", "----", "-----", "-----");
Console.WriteLine("{0,24}{1,18}{2,18}{3,18}{4,18}{5,18}{6,18}{7,18}{8,18}{9,18}", t[i], state[i], c[i], d[i],
w[i], l[i], r[i], s[i], pctWins, raRS);
提示:
使用 $""
导致较少错误的字符串格式。
现在可以使用了! 在阅读了每个人不得不说的内容后,我开始更多地使用它并阅读网站上的提示。
*对于以后阅读的任何人。正如我在下面添加的答案中提到的那样,我遗漏了一些括号。我还犯了一个错误,没有检查我的 if 语句。我把它放在下面,其中状态必须等于 "AL",等等。这是不可能的,因为那些是分裂。
感谢任何能提供帮助的人,格式化是我的弱项。所有这些代码需要做的是显示 AL 和 NL 分区中团队的数组中的所有信息,以及两列不是数组格式的信息。一列是显示 (wins/total) 的变量,另一列显示 (运行 分数和 运行 反对) 之间的差异。
但是,我一直收到有关输入字符串错误的错误消息。我有点不确定到底出了什么问题。
我的第一个猜测可能是,因为我没有数组格式的最后两列,所以这是导致错误的原因。但是,我不确定我是否可以将它们放在一个空数组中。
或者,也许是因为当我调用这两个模块时,我重复了两个数组名称。我这样做是因为我看到我的老师在 class 中向我们展示的示例中已经这样做了。但是,这次似乎不起作用。
我尽量提供尽可能多的信息,而不是文本轰炸,如果需要,我可以提供任何其他信息。
哇哦!感谢信息,错误消失了!可悲的是,我似乎无法从数组中获取任何信息以显示在列中。
string[] teams = { "Boston Red Sox", "New York Yankees", "Tampa Bay Rays", "Toronto Blue Jays", "Baltimore Orioles",
"Cleveland Indians", "Minnesota Twins", "Kansas City Royals", "Chicago White Sox", "Detroit Tigers", "Houston Astros", "Los Angeles Angels",
"Seattle Mariners", "Texas Rangers", "Oakland Athletics", "Washington Nationals", "Miami Marlins", "Atlanta Braves", "New York Mets", "Philadelphia Phillies",
"Chicago Cubs", "Milwaukee Brewers", "St. Louis Cardinals", "Pittsburgh Pirates", "Cincinnati Reds", "Los Angeles Dodgers", "Arizona Diamondbacks",
"Colorado Rockies", "San Diego Padres", "San Francisco Giants" };
string[] state = { "MA", "NY", "FL", "CN", "MD", "OH", "MN", "MO", "IL", "MI", "TX", "CA", "WA", "TX", "CA", "DC",
"FL", "GA", "NY", "PA", "IL", "WI", "MO", "PA", "OH", "CA", "AZ", "CO", "CA", "CA" };
string[] clinch = { "x", "y", "n", "n", "n", "*", "y", "n", "n", "n", "x", "n", "n", "n", "n", "x", "n", "n",
"n", "n", "x", "n", "n", "n", "n", "*", "y", "y", "n", "n" };
string[] divison = { "ALE", "ALE", "ALE", "ALE", "ALE", "ALC", "ALC", "ALC", "ALC", "ALC", "ALW", "ALW", "ALW", "ALW", "ALW", "NLE", "NLE", "NLE", "NLE", "NLE", "NLC", "NLC", "NLC",
"NLC", "NLC", "NLW", "NLW", "NLW", "NLW", "NLW" };
int[] wins = { 93, 91, 80, 76, 75, 102, 85, 80, 67, 64, 101, 80, 78, 78, 75, 97, 77, 72, 70, 66, 92, 86, 83, 75, 68, 104, 93, 87, 71, 64 };
int[] losses = { 69, 71, 82, 86, 87, 60, 77, 82, 95, 98, 61, 82, 84, 84, 87, 65, 85, 90, 92, 96, 70, 76, 79, 87, 94, 58, 69, 75, 91, 98 };
int[] runsScored = { 785, 858, 694, 693, 743, 818, 815, 702, 706, 735, 896, 710, 750, 799, 739, 819, 778, 732, 735, 690, 822, 732, 761, 668, 753, 770, 812, 824, 604, 639 };
int[] runsAgainst = { 668, 660, 704, 784, 841, 564, 788, 791, 820, 894, 700, 709, 772, 816, 826, 672, 822, 821, 863, 782, 695, 697, 705, 731, 869, 580, 659, 757, 816, 776 };
divNL(teams, state, clinch, divison, wins, losses, runsScored, runsAgainst, wins, losses);
divAL(teams, state, clinch, divison, wins, losses, runsScored, runsAgainst, losses, runsScored);
}
static void divNL(string[] t, string[] state, string[] c, string[] d, int[] w, int[] l, int[] r, int[] s, int[] p, int[] o)
{
// displays all info for NL division
string stateM = "";
double pctWins = 0.0;
double raRS = 0.0;
int win = 0, loss = 0;
int aR = 0, sR = 0;
pctWins = (double)win / (win + loss);
raRS = (double)aR - sR;
Console.WriteLine("All Data for NL Division Teams");
Console.WriteLine("{0,24}{1,18}{2,18}{3,18}{4,18}{5,18}{6,18}{7,18}{8,18}{9,18}", "Team", "State", "Clinch", "Div", "W", "L", "RS", "RA", "WinPCT", "RA/RS");
Console.WriteLine("{0,24}{1,18}{2,18}{3,18}{4,18}{5,18}{6,18}{7,18}{8,18}{9,18}", "----", "-----", "----", "----", "-----", "----", "-----", "----", "-----", "-----");
for (int i = 0; i < state.Length; ++i)
{
if (state[i] == "NLC" | state[i] == "NLW" | state[i] == "NLE")
{
stateM = state[i];
Console.WriteLine("{0,24}{1,18}{2,18}{3,18}{4,18}{5,18}{6,18}{7,18}{8,18}{9,18}", t[i], state[i], c[i], d[i],
w[i], l[i], r[i], s[i], pctWins, raRS);
}
}
}
static void divAL(string[] t, string[] state, string[] c, string[] d, int[] w, int[] l, int[] r, int[] s, int[] p, int[] o)
{
//displays all info for Al divsion
string stateM = "";
double pctWins = 0.0;
double raRS = 0.0;
int win = 0, loss = 0;
int aR = 0, sR = 0;
pctWins = (double)win / (win + loss);
raRS = (double)aR - sR;
Console.WriteLine("\n\nAll Data for AL Division Teams\n");
Console.WriteLine("{0,20}{1,8}{2,8}{3,8}{4,8}{5,8}{6,8}{7,8}{8,8}{9,8}", "Team", "State", "Clinch", "Div", "W", "L", "RS", "RA", "WinPCT", "RA/RS");
Console.WriteLine("{0,20}{1,8}{2,8}{3,8}{4,8}{5,8}{6,8}{7,8}{8,8}{9,8}", "----", "-----", "----", "----", "-----", "----", "-----", "----", "-----", "-----");
for (int i = 0; i < state.Length; ++i)
{
if (state[i] == "ALC" | state[i] == "ALE" | state[i] == "ALE")
{
stateM = state[i];
Console.WriteLine("{0,8}{1,8}{2,8}{3,8}{4,8}{5,8}{6,8}{7,8}{8,8}{9,8}", t[i], state[i], c[i], d[i],
w[i], l[i], r[i], s[i], pctWins, raRS);
}
}
}
您忘记了这些行中的一些左花括号:
Console.WriteLine("0,24}{1,18}{2,18}{3,18}{4,18}{5,18}{6,18}{7,18}{8,18}{9,18}", "----", "-----", "----",
"----", "-----", "----", "-----", "----", "-----", "-----");
Console.WriteLine("0,24}{1,18}{2,18}{3,18}{4,18}{5,18}{6,18}{7,18}{8,18}{9,18}", t[i], state[i], c[i], d[i],
w[i], l[i], r[i], s[i], pctWins, raRS);
Console.WriteLine("0,24}{1,18}{2,18}{3,18}{4,18}{5,18}{6,18}{7,18}{8,18}{9,18}", "----", "-----", "----",
"----", "-----", "----", "-----", "----", "-----", "-----");
Console.WriteLine("0,24}{1,18}{2,18}{3,18}{4,18}{5,18}{6,18}{7,18}{8,18}{9,18}", t[i], state[i], c[i], d[i],
w[i], l[i], r[i], s[i], pctWins, raRS);
它们应该如下:
Console.WriteLine("{0,24}{1,18}{2,18}{3,18}{4,18}{5,18}{6,18}{7,18}{8,18}{9,18}", "----", "-----", "----",
"----", "-----", "----", "-----", "----", "-----", "-----");
Console.WriteLine("{0,24}{1,18}{2,18}{3,18}{4,18}{5,18}{6,18}{7,18}{8,18}{9,18}", t[i], state[i], c[i], d[i],
w[i], l[i], r[i], s[i], pctWins, raRS);
Console.WriteLine("{0,24}{1,18}{2,18}{3,18}{4,18}{5,18}{6,18}{7,18}{8,18}{9,18}", "----", "-----", "----",
"----", "-----", "----", "-----", "----", "-----", "-----");
Console.WriteLine("{0,24}{1,18}{2,18}{3,18}{4,18}{5,18}{6,18}{7,18}{8,18}{9,18}", t[i], state[i], c[i], d[i],
w[i], l[i], r[i], s[i], pctWins, raRS);
提示:
使用 $""
导致较少错误的字符串格式。