我正在尝试将 object[,] 数组元素转换为 int 以进行条件运算符比较。我的 classesArray 由字符串和整数组成。我引用的列的值只能为 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、微积分 I、3、0、0 和 0。第 2 行 = MATH 2113、1001,微积分实验室 I、1、0、0 和 0
我想打印第 1 行 = MATH 2113、微积分 I 和 3。第 2 行 = MATH 2113、微积分实验室 1 和 1
我用 0 填充了所有空元素