多维数组检查器

时间:2017-04-11 07:45:27

标签: c# arrays visual-studio multidimensional-array

下面是一个c#程序,类似于我必须创建的程序,我已经做了很多尝试,但没有成功。

如果在main方法中,'int [] [] lottoNumbers'和该数组中的所有相对'new int []'被初始化为int [,] lottoNumbers。 PrintWinners,CheckNumbers,CheckSups和PrintChecks方法需要进行哪些其他更改才能调整此已更改的已声明二维数组。

我已经多次尝试这样做,但是我一直在运行索引超出了数组错误的范围。

    //Number of supplementaries in each draw
    const int SUPP = 2;

    /* Calls the methods to execute the program
     * 
     * precondition: none
     * postcondition: returns a series of strings and integers
     */
    static void Main() {
        const int NUMBER_OF_ROWS = 9;

        int[][] lottoNumbers ={ 
                         new int [] { 4, 7, 19, 23, 28, 36},
                         new int [] { 14, 18, 26, 34, 38, 45},
                         new int [] { 8, 10,11, 19, 28, 30},
                         new int [] {15, 17, 19, 24, 43, 44},
                         new int [] { 10, 27, 29, 30, 32, 41},
                         new int [] {9, 13, 26, 32, 37,  43},
                         new int [] {1, 3, 25, 27, 35, 41},
                         new int [] {7, 9, 17, 26, 28, 44},
                         new int [] {17, 18, 20, 28, 33, 38}
                          };

        int[] drawNumbers = new int[] { 44, 9, 17, 43, 26, 7, 28, 19 };


        TitleMessage();
        PrintYourNumbers(lottoNumbers);
        PrintDrawNumbers(drawNumbers);
        PrintChecks(lottoNumbers, drawNumbers, lottoNumbers.Length);

        ExitProgram();
    }//end Main


    /* Print welcome message
     * 
     * precondition: none
     * postcondition: returns the string
     */
    static void TitleMessage() {
        Console.WriteLine("\n\t Welcome to Lotto Checker \n\n\n");
    } //end TitleMessage


    /* Display the list of games created
     * 
     * precondition: list must be a two-dimentional array containing integers
     * postcondition: returns the list of games on separate lines (as strings)
     */
    static void PrintYourNumbers(int[][] list) {
        Console.WriteLine("Your Lotto numbers are:\n");
        for (int i = 0; i < list.Length; i++) {
            Console.Write("Game  {0,3}:", i + 1);
            for (int j = 0; j < list[i].Length; j++) {
                Console.Write("  {0,2}", list[i][j]);
            }
            Console.WriteLine("\n\n");
        }
    } //end PrintYourNumbers


    /* Display the single draw list created
    * 
    * precondition: list must be a two-dimentional array containing integers
    * postcondition: returns the draw list
    */
    static void PrintDrawNumbers(int[] list) {
        Console.WriteLine("\n\n Lotto Draw Numbers are: \n");
        for (int i = 0; i < list.Length; i++) {
            Console.Write("  {0,2}", list[i]);
        }
        Console.WriteLine("\n");
    } //end PrintDrawNumbers


    /* Display the string of winning numbers and sups
    * 
    * precondition: all parameters must be integers, yourNums must be 2D array,
    *                  drawNums must be single array, 
    *                  gameNum > 0 and < length of yourNums  
    * postcondition: returns the winning values on separate lines (as a string)
    */
    static void PrintWinners(int[][] yourNum, int[] drawNum, int gameNum) {
        int winningNum = 0, winningSup = 0;

        winningNum = CheckNumbers(yourNum, drawNum, gameNum);
        winningSup = CheckSups(yourNum, drawNum, gameNum);

        Console.WriteLine("\n\n\t found {0} winning numbers and {1} supplementary numbers in Game {2}\n\n", winningNum, winningSup, gameNum + 1);
    } //end CheckGame


    /* Compare game numbers against draw numbers
    * 
    * precondition: all parameters must be integers, yourNums must be 2D array,
    *                  drawNums must be single array, 
    *                  gameNum > 0 and < length of yourNums  
    * postcondition: returns the number of winning numbers
    */
    static int CheckNumbers(int[][] yourNum, int[] drawNum, int gameNum) {
        int winningNumber = 0;
        for (int i = 0; i < yourNum[gameNum].Length; i++) {
            for (int j = 0; j < drawNum.Length - SUPP; j++) {
                if (yourNum[gameNum][i] == drawNum[j]) {
                    winningNumber++;
                }
            }
        }
        return winningNumber;
    } //end CheckNumbers


    /* Compare game sup numbers against draw numbers
    * 
    * precondition: all parameters must be integers, yourNums must be 2D array,
    *                  drawNums must be single array, 
    *                  gameNum > 0 and < length of yourNums  
    * postcondition: returns the number of winning sups
    */
    static int CheckSups(int[][] yourNum, int[] drawNum, int gameNum) {
        int winningSup = 0;

        for (int j = 0; j < yourNum[gameNum].Length; j++) {
            for (int k = drawNum.Length - SUPP; k < drawNum.Length; k++) {
                if (yourNum[gameNum][j] == drawNum[k]) {
                    winningSup++;
                }
            }
        }
        return winningSup;
    } //end CheckSups

    /* Prints each game's winning numbers and sups
     * 
     * precondition: all parameters must be integers, yourNums must be 2D array,
     *                  drawNums must be single array, 
     *                  gameNum > 0 and < length of yourNums  
     * postcondition: prints each game's winning number & sup count
     */
    static void PrintChecks(int[][] yourNums, int[] drawNums, int gameNum) {
        for (int i = 0; i < gameNum; i++) {
            PrintWinners(yourNums, drawNums, i);
        }
    } //end PrintChecks


    /* Print exit message
     * 
     * precondition: none
     * postcondition: returns the string
     */
    static void ExitProgram() {
        Console.Write("\n\nPress any key to exit program: ");
        Console.ReadKey();
    }//end ExitProgram


} //end class Program

任何建议都将不胜感激,谢谢!

1 个答案:

答案 0 :(得分:0)

要获得“尺寸0”的长度,您需要yourNums.GetLength(0),以获得“尺寸1”的长度,您必须yourNums.GetLength(1)。由此变化变得简单。

const int SUPP = 2;

/* Calls the methods to execute the program
 * 
 * precondition: none
 * postcondition: returns a series of strings and integers
 */
static void Main() {
    const int NUMBER_OF_ROWS = 9;

    int[,] lottoNumbers ={
                     { 4, 7, 19, 23, 28, 36 },
                     { 14, 18, 26, 34, 38, 45 },
                     { 8, 10,11, 19, 28, 30 },
                     { 15, 17, 19, 24, 43, 44 },
                     { 10, 27, 29, 30, 32, 41 },
                     { 9, 13, 26, 32, 37, 43 },
                     { 1, 3, 25, 27, 35, 41 },
                     { 7, 9, 17, 26, 28, 44 },
                     {17, 18, 20, 28, 33, 38 }
                      };

    int[] drawNumbers = new int[] { 44, 9, 17, 43, 26, 7, 28, 19 };


    TitleMessage();
    PrintYourNumbers(lottoNumbers);
    PrintDrawNumbers(drawNumbers);
    PrintChecks(lottoNumbers, drawNumbers, lottoNumbers.GetLength(0));

    ExitProgram();
}//end Main


/* Print welcome message
 * 
 * precondition: none
 * postcondition: returns the string
 */
static void TitleMessage() {
    Console.WriteLine("\n\t Welcome to Lotto Checker \n\n\n");
} //end TitleMessage


/* Display the list of games created
 * 
 * precondition: list must be a two-dimentional array containing integers
 * postcondition: returns the list of games on separate lines (as strings)
 */
static void PrintYourNumbers(int[,] list) {
    Console.WriteLine("Your Lotto numbers are:\n");
    for (int i = 0; i < list.GetLength(0); i++) {
        Console.Write("Game  {0,3}:", i + 1);
        for (int j = 0; j < list.GetLength(1); j++) {
            Console.Write("  {0,2}", list[i, j]);
        }
        Console.WriteLine("\n\n");
    }
} //end PrintYourNumbers


/* Display the single draw list created
* 
* precondition: list must be a two-dimentional array containing integers
* postcondition: returns the draw list
*/
static void PrintDrawNumbers(int[] list) {
    Console.WriteLine("\n\n Lotto Draw Numbers are: \n");
    for (int i = 0; i < list.Length; i++) {
        Console.Write("  {0,2}", list[i]);
    }
    Console.WriteLine("\n");
} //end PrintDrawNumbers


/* Display the string of winning numbers and sups
* 
* precondition: all parameters must be integers, yourNums must be 2D array,
*                  drawNums must be single array, 
*                  gameNum > 0 and < length of yourNums  
* postcondition: returns the winning values on separate lines (as a string)
*/
static void PrintWinners(int[,] yourNum, int[] drawNum, int gameNum) {
    int winningNum = 0, winningSup = 0;

    winningNum = CheckNumbers(yourNum, drawNum, gameNum);
    winningSup = CheckSups(yourNum, drawNum, gameNum);

    Console.WriteLine("\n\n\t found {0} winning numbers and {1} supplementary numbers in Game {2}\n\n", winningNum, winningSup, gameNum + 1);
} //end CheckGame


/* Compare game numbers against draw numbers
* 
* precondition: all parameters must be integers, yourNums must be 2D array,
*                  drawNums must be single array, 
*                  gameNum > 0 and < length of yourNums  
* postcondition: returns the number of winning numbers
*/
static int CheckNumbers(int[,] yourNum, int[] drawNum, int gameNum) {
    int winningNumber = 0;
    for (int i = 0; i < yourNum.GetLength(1); i++) {
        for (int j = 0; j < drawNum.Length - SUPP; j++) {
            if (yourNum[gameNum, i] == drawNum[j]) {
                winningNumber++;
            }
        }
    }
    return winningNumber;
} //end CheckNumbers


/* Compare game sup numbers against draw numbers
* 
* precondition: all parameters must be integers, yourNums must be 2D array,
*                  drawNums must be single array, 
*                  gameNum > 0 and < length of yourNums  
* postcondition: returns the number of winning sups
*/
static int CheckSups(int[,] yourNum, int[] drawNum, int gameNum) {
    int winningSup = 0;

    for (int j = 0; j < yourNum.GetLength(1); j++) {
        for (int k = drawNum.Length - SUPP; k < drawNum.Length; k++) {
            if (yourNum[gameNum, j] == drawNum[k]) {
                winningSup++;
            }
        }
    }
    return winningSup;
} //end CheckSups

/* Prints each game's winning numbers and sups
 * 
 * precondition: all parameters must be integers, yourNums must be 2D array,
 *                  drawNums must be single array, 
 *                  gameNum > 0 and < length of yourNums  
 * postcondition: prints each game's winning number & sup count
 */
static void PrintChecks(int[,] yourNums, int[] drawNums, int gameNum) {
    for (int i = 0; i < gameNum; i++) {
        PrintWinners(yourNums, drawNums, i);
    }
} //end PrintChecks


/* Print exit message
 * 
 * precondition: none
 * postcondition: returns the string
 */
static void ExitProgram() {
    Console.Write("\n\nPress any key to exit program: ");
    Console.ReadKey();
}//end ExitProgram