关于c#:Keep获取未处理的异常:System.FormatException:输入字符串格式不正确?

Keep Getting Unhandled Exception: System.FormatException: Input string was not in correct format?

它现在起作用了!在阅读了每个人都要说的内容之后,我开始更多地使用它,并阅读网站上的提示。

*为将来阅读的任何人。我在下面添加的答案中缺少了一些括号。我也犯了一个错误,不检查我的if语句。我把它放在下面,各州必须等于"al",等等,这是不可能的,因为它们是分裂的。

感谢所有能帮忙的人,格式化是我的弱点。所有这些代码需要做的就是为AL和NL分区中的团队显示来自数组的所有信息,以及不以数组格式显示的两列。一个是变量,显示(获胜/总数),另一个列显示两者之间的差异(跑动得分和跑动对手)。

但是,我不断地得到一个关于输入字符串错误的错误。我有点不确定到底出了什么问题。

我的第一个猜测是,可能是因为我没有数组格式的最后两列导致了这个错误。但是,我不确定是否可以将它们放入空数组中。

或者,可能是因为当我调用这两个模块时,我重复了两个数组名。我这样做是因为我看到我的老师在课堂上给我们举了一个例子。不过,这次似乎不起作用。

我尽量提供尽可能多的信息而不使用文本轰炸,如果需要,我可以提供任何其他信息。

呜呜!多亏了这些信息,错误就消失了!遗憾的是,我似乎无法从数组中获得任何列中显示的信息。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
        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("

All Data for AL Division Teams
"
);

        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);
            }
        }

    }


在这些行中,您忘记了一些开放的花括号:

1
2
3
4
5
6
7
8
9
10
11
12
    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);

它们应该如下:

1
2
3
4
5
6
7
8
9
10
11
12
    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);

提示:使用导致错误较少的$""字符串格式。