Javascript 逻辑运算符混乱

Javascript Logical Operator Confusion

我有一个完整的工作程序,但我对我的几个条件语句中的逻辑非常困惑,因为我摆弄它们试图让程序执行我想要的操作。

    while (col < 5 || 20 < col || Number.isInteger(col)) {
        col = prompt("Columns (Int between 5 and 20): ","10");
        console.log("col " + col);
    }
    while (row < 5 || 20 < row || Number.isInteger(row)) {
        row = prompt("Rows (Int between 5 and 20): ","10");
        console.log("row " + row);
    }

    if (row>col) {size = 400/col;console.log("colmore");}
    else if (col>row) {size = 400/row;console.log("rowmore");}
    else {size = 400/col;console.log("same");}
    console.log("size " + size);

现在我的程序提示输入列数,然后提示行数。例如,我将为列输入 20,为行输入 5 - 列显然比行多。所以会发生这种情况:

col 20
row 5
colmore
size 20

显然这就是我想要它做的,但我挂断电话是因为第一个条件

if (row>col)

应该意味着如果行多于列,程序应该继续下一条语句......或者我只是完全失去理智......?

我会尝试这样的事情:

while (col < 5 || col > 20 || !Number.isInteger(col)) {
    col = Number(prompt("Columns (Int between 5 and 20): ","10"));
    console.log("col " + col);
}

while (row < 5 || row > 20 || !Number.isInteger(row)) {
    row = Number(prompt("Rows (Int between 5 and 20): ","10"));
    console.log("row " + row);
}

变化:

  • 将字符串输入转换为数字,以便我们将提示响应包装在 Number() 调用中;这会将字符串转换为数字,如果不是数字则返回数字或 NaN
  • 如果它不是整数,则留在 while 循环中(如果它是整数,则你有);注意 !
  • 暂时切换第二个条件,让它读起来更自然(也许是个人喜好问题,但我发现 col < 5 或 col > 20 比 col < 5 或 20 < col 更容易阅读)

用 if (row>col) 你是说如果行数 (int) 大于列数 (int) 那么 运行 这个代码

size = 400/col;console.log("colmore");

并跳过此代码

else if (col>row) {size = 400/row;console.log("rowmore");}
else {size = 400/col;console.log("same");}

然后运行这个代码

console.log("size " + size);

我已经编辑了您的代码并分解了步骤来帮助您。

//declare variable as prompt first
var col = prompt("Columns (Int between 5 and 20): ","10");
/* ** continue to display prompt if user enters a number below 5 or a number above 20 or a character which is not a number ** */
while (col < 5 || col < 20 || isNaN(col)) {
  col = prompt("Columns (Int between 5 and 20): ","10");
  console.log("col " + col);
}
// notice the use of isNaN(row) which is checking the col variable

//declare variable as prompt first
var row = prompt("Rows (Int between 5 and 20): ","10");
/* ** continue to display prompt if user enters a number below 5 or a number above 20 or a character which is not a number ** */
while (row < 5 || row < 20 || isNaN(row)) {
  row = prompt("Rows (Int between 5 and 20): ","10");
  console.log("row " + row);
}
// notice the use of isNaN(row) which is checking the row variable

//First check if row is greated than col
if (row > col) {
  size = 400/col;console.log("More Rows " + row + "rows and " + col + "cols");//if true run this code
}
//if row is not greater than col then check if col is greater than row
else if (col > row) {
  size = 400/row;console.log("More Cols " + col + "cols" + row + "rows and ");//if true then run this code
}
//else row and col must be equal
else {
  size = 400/col;console.log("same amount");//so run this code run this code
}
//end if statement and move on with code
console.log("size " + size);

你绝对是在正确的轨道上。 推荐使用 isNaN() 方法。