2011-11-28 130 views
0

我寫了一個程序,要求一個人輸入一個團隊號碼(假設0,1或2)以及團隊爲特定遊戲獲得多少個目標。我有兩個問題。首先,試圖判斷是否平局的if語句完全被忽略,其次,第二隊總是以平局中最低,中等和最高的球隊得分。 我非常新的C#,請回答一個新手:程序忽略了第一條語句

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

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     public static void updateScores(int x, int myTeam1, int myGoal1, int myTeam2, int myGoal2, int[,] values) 
     { 
      values[myTeam1, x] = myGoal1; 
      values[myTeam2, x] = myGoal2; 

     } 
     static void Main(string[] args) 
     { 

      //declare variables and integer array 
      //the different locations are set by default to 0 
      int highest; 
      int middle; 
      int lowest; 
      int counter = 0; 
      int x; 
      int y; 
      int z; 
      int team1; 
      int team2; 
      int goals1; 
      int goals2; 
      int[,] teamsGoalArray = new int[3, 4]; 

      //get information about teams playing and goals scored 
      while (counter <= 2) 
      { 
     Console.WriteLine("Please enter the first team playing in the {0} game", counter+1); 
     team1 = Convert.ToInt32(Console.ReadLine()); 
     Console.WriteLine("Please enter the number of goals for the team playing in the {0} game", counter  +1); 
     goals1 = Convert.ToInt32(Console.ReadLine()); 
     Console.WriteLine("Please enter the second team playing in the {0} game", counter+1); 
     team2 = Convert.ToInt32(Console.ReadLine()); 
     Console.WriteLine("Please enter the number of goals for the team playing in the {0} game", counter +1); 
     goals2 = Convert.ToInt32(Console.ReadLine()); 
     updateScores(counter, team1, goals1, team2, goals2, teamsGoalArray); 
     ++counter; 
     } 

     int a = teamsGoalArray[0, 1] + teamsGoalArray[0, 2] + teamsGoalArray[0, 3]; 
     int b = teamsGoalArray[1, 1] + teamsGoalArray[1, 2] + teamsGoalArray[1, 3]; 
     int c = teamsGoalArray[2, 1] + teamsGoalArray[2, 2] + teamsGoalArray[2, 3]; 

     if (a == b && a == c && b == c) 
     { 
      Console.WriteLine("All three teams had a total of {0} goals", a); 
     } 

     if (a >= b && a >= c) 
      { 
       highest = a; 
       x = 0; 
      } 

      else 
       if (b >= a && b >= c) 
       { 
        highest = b; 
        x = 1; 
       } 
       else 
       { 
        highest = c; 
        x = 2; 
       } 
      Console.WriteLine("Team {0} had the highest score with {1} goals", x, highest); 

      if (a < b && a > c || a > b && a < c) 
      { 
       middle = a; 
       y = 0; 
      } 
      else 
       if (b < a && b > c || b > a && b < c) 
       { 
        middle = b; 
        y = 1; 
       } 
       else 
       { 
        middle = c; 
        y = 2; 
       } 
      Console.WriteLine("Team {0} had the middle score with {1} goals", y, middle); 

      if (a < b && a < c) 
      { 
       lowest = a; 
       z = 0; 
      } 
      else 
       if (b < a && b < c) 
       { 
        lowest = b; 
        z = 1; 
       } 
       else 
       { 
        lowest = c; 
        z = 2; 
       } 
      Console.WriteLine("Team {0} had the lowest score with {1} goals", z, lowest); 
     } 
    } 



    } 
+0

首先,如果a = b且a = c,則不需要檢查b = c。你可以在那裏放置一個斷點來看看發生了什麼? –

+0

傳遞性使b == c無用。如果a = b和a = c,那麼由布爾的傳遞屬性和b = c。 – tafoo85

+0

此外,我沒有看到你問隊C –

回答

1

我看到的第一個問題是,當你調用updateScores,你傳遞的counter值,這將是0,1,或2。所以你通過teamsGoalArray[team, 2]填寫teamsGoalArray[team, 0]。但是,當您將總分加起來時,您將通過teamsGoalArray[team, 3]加上teamsGoalArray[team, 1]。所以你錯過了每支球隊第一場比賽的分數。

+0

是的,就是這樣。非常感謝。 –

+0

你如何標記回答的問題? –

+0

請點擊此答案旁邊的打勾標記,表示已接受。 – V4Vendetta