0

条件演算子の比較のために、object[,] 配列要素を int にキャストしようとしています。私の classesArray は文字列と int で構成されています。参照している列は、フラグに使用しているため、1 または 0 の値しか持てません。1 がある場合は、次の行/行に続けてほしい。使用しているコードはプログラムを正しく実行しません

while (classesArray[classesArrayRow,7] == (object)1)
                {
                    classesArrayRow++;
                }

tempArray 全体が classesArray の最初の行でいっぱいになります。だから、私は試してみました

while ((int)classesArray[classesArrayRow,7] > 0)

エラーはありませんが、このキャストは機能しません。

私のコード:

private void ProcessObjects(object[,] classesArray, object[,] classesAvailabilityArray, Excel.Workbook workbook2, Excel.Sheets excelSheets)
    {
        // once classes are selected, they are copied to a temporary location
        // while they're waiting to be printed
        object[,] tempArray = new object[6,3];

        // This stops the while loop once enough credit hours have been taken 
        // if a break condition has not been met first.
        // It must reach 123 hours for CS Degree .
        int hourCounter = 0;

        int iteration = 0;

        while (hourCounter < 123)
        {
            // this while loop copies some classes from classes array to tempArray
            // so they can be printed into the excel template (NewStudentTemplateCS.xlsx)
            //
            int classes = 1, hours = 0; // stops while loop if limit is reached
            int tempArrayRow = 0, tempArrayCol = 0; // used to select individual elements of tempArray
            int classesArrayRow = 1, classesArrayCol = 1; // used to select individual elements of classesArray

            while(classes < 7 || hours < 17)
            {
                // this loop checks the status of the flag and stops at the first avaliable
                // class/row of classesArray
                while (classesArray[classesArrayRow,7] == (object)1)
                {
                    classesArrayRow++;
                }

                // copies the call EX: "MATH 2313" from classesArray to tempArray
                tempArray[tempArrayRow,tempArrayCol] = classesArray[classesArrayRow,classesArrayCol];
                tempArrayCol ++;
                classesArrayCol += 2;
                // copies the name EX: "Calculus I" from classesArray to tempArray
                tempArray[tempArrayRow, tempArrayCol] = classesArray[classesArrayRow, classesArrayCol];
                tempArrayCol++;
                classesArrayCol++;
                // Copies the hours EX: "3" from classesArray to tempArray
                tempArray[tempArrayRow, tempArrayCol] = classesArray[classesArrayRow, classesArrayCol];

                // increments classes, hours, and hourCounter for exit decision
                classes += 1;

                // converts object element to an int for the following "+=" operator
                int numberOfHours = Convert.ToInt32(classesArray[classesArrayRow, classesArrayCol]);

                // adds numberOfHours to the following varriable to increment loop exit decision
                hours += numberOfHours;
                hourCounter += numberOfHours;

                // sets flag to one
                classesArrayCol += 3;
                classesArray[classesArrayRow, classesArrayCol] = 1;

                //reset column varriables
                classesArrayCol = 1;
                tempArrayCol = 0;

                // increments row for temp array
                tempArrayRow++;

            }// end while loop

            // print method that prints temp array and clears tempArray for next use
            PrintArray(tempArray, iteration, workbook2, excelSheets);

            // iterates iteration
            iteration++;

        } // end while loop


    } // end ProcessObjects method

私のデータ:

ヘッダー = コール、番号、クラス名、時間、前提条件番号、前提条件名、およびフラグ。行 1 = MATH 2313, 1000, Calculus I, 3, 0 , 0 , and 0. 行 2 = MATH 2113, 1001, Calculus Lab I, 1, 0, 0, and 0

行 1 = MATH 2113、微積分 I、および 3 を印刷したい。 行 2 = MATH 2113、微積分演習 1、および 1

すべての null 要素を 0 で埋めました

4

2 に答える 2

0

コードは、ボックス化された整数に対して常に false を返す参照比較classesArray[classesArrayRow,7] == (object)1を実行します。正しい方法は次のとおりです。

while ((int)classesArray[classesArrayRow,7] != 0){
    //...
}
于 2012-04-25T04:40:17.000 に答える
0

問題を解決する簡単な方法の 1 つは、文字列か数値かを確認するコードを作成することです。

if(classesArray[0,7] is int) // values were imported as ints
{
    while ((int)classesArray[classesArrayRow,7] == 1)
    {
        classesArrayRow++;
    }
}
if(classesArray[0,7] is string) // values were imported as strings
{
    while ((string)classesArray[classesArrayRow,7] == "0")
    {
        classesArrayRow++;
    }
}

特に一度の作業ではない場合は、コード全体を間違いなく変更します。

于 2012-04-25T05:29:56.873 に答える