目前我正在尝试创建一个程序,它将从头开始自动生成一个完整的数独板,尽管相同的功能可以通过微小的改动来解决部分填充的数独板。问题是,当你在板上加载一个新的数字时,你显然必须检查这个数字是否已经存在于同一列或行中,而且也是在加载它的3x3方块中。对程序的一部分使用嵌套的switch语句,你必须根据相同的3x3方格中的数字检查数字,尽管break语句都显示在正确的位置。 compliler说的是第二个case语句的第一个数字,它是嵌套switch语句的一部分:"错误:case标签值已经出现在这个switch"即使这个数字还没有,问题是为什么?
以下是代码:
bool SudokuClass::checkPresentGrid(int &temporary, int row, int column)
{
bool indicator;
switch (row)
{
case 0 || 3 || 6: //If the number is being loaded into row 0,3 or 6
switch (column)
{
case 0 || 3 || 6: //Check all other spaces in the same 3x3 grid
if (temporary == (completeSudoku[row + 1][column + 1] || completeSudoku[row + 1][column + 2] || completeSudoku[row + 2][column + 1] || completeSudoku[row + 2][column + 2]))
{
indicator = true;
}
break;
case 1 || 4 || 7:
if (temporary == (completeSudoku[row + 1][column - 1] || completeSudoku[row + 1][column + 1] || completeSudoku[row + 2][column - 1] || completeSudoku[row + 2][column + 1]))
{
indicator = true;
}
break;
case 2 || 5 || 8:
if (temporary == (completeSudoku[row + 1][column - 2] || completeSudoku[row + 1][column - 1] || completeSudoku[row + 2][column - 2] || completeSudoku[row + 2][column - 1]))
{
indicator = true;
}
break;
}
break;
case 1 || 4 || 7: //If the number is being loaded into row 1, 4 or 7
switch (column)
{
case 0 || 3 || 6: //Check all other spaces in the same 3x3 grid
if (temporary == (completeSudoku[row - 1][column + 1] || completeSudoku[row - 1][column + 2] || completeSudoku[row + 1][column + 1] || completeSudoku[row + 1][column + 2]))
{
indicator = true;
}
break;
case 1 || 4 || 7:
if (temporary == (completeSudoku[row - 1][column - 1] || completeSudoku[row - 1][column + 1] || completeSudoku[row + 1][column - 1] || completeSudoku[row + 1][column + 1]))
{
indicator = true;
}
break;
case 2 || 5 || 8:
if (temporary == (completeSudoku[row - 1][column - 2] || completeSudoku[row - 1][column - 1] || completeSudoku[row + 1][column - 2] || completeSudoku[row + 1][column - 1]))
{
indicator = true;
}
break;
}
break;
case 2 || 5 || 8: //If the number is being loaded into row 2, 5 or 8
switch (column)
{
case 0 || 3 || 6: //Check all other spaces in the same 3x3 grid
if (temporary == (completeSudoku[row - 2][column + 1] || completeSudoku[row - 2][column + 2] || completeSudoku[row - 1][column + 1] || completeSudoku[row - 1][column + 2]))
{
indicator = true;
}
break;
case 1 || 4 || 7:
if (temporary == (completeSudoku[row - 2][column - 1] || completeSudoku[row - 2][column + 1] || completeSudoku[row - 1][column - 1] || completeSudoku[row - 1][column + 1]))
{
indicator = true;
}
break;
case 2 || 5 || 8:
if (temporary == (completeSudoku[row - 2][column - 2] || completeSudoku[row - 2][column - 1] || completeSudoku[row - 1][column - 2] || completeSudoku[row - 1][column - 1]))
{
indicator = true;
}
break;
}
break;
}
return indicator;
}
答案 0 :(得分:6)
更改
case 0 || 3 || 6: //If the number is being loaded into row 0,3 or 6
到
case 0:
case 3:
case 6: //If the number is being loaded into row 0,3 or 6
...依此类推,与您的其他case
语句一起使用。
您的代码case 0 || 3 || 6
评估为case (0 || 3 || 6)
变为case (false || true || true)
变成case (true)
,这不是你想要的。
答案 1 :(得分:1)
case
语句必须是值。您正在尝试与EXPRESSIONS的结果进行比较。
e.g。鉴于此代码:
switch($foo) {
case $bar || $baz:
...
}
重写为传统的if(),它将执行完全相同的:
$temp = $bar || $baz; // boolean OR operation, $temp becomes true/false
if ($foo == $temp) {
...
您正试图将其视为:
if (($temp == $bar) || ($temp == $baz))
它肯定不是。
如果要比较多个值并让每个值执行相同的代码,请使用switch
直通行为:
switch($foo) {
case $bar:
case $baz:
.... do something
break;