[英]Javascript Logical Operator Confusion
I have a complete working program, but I am very confused about the logic in a couple of my conditional statements after I fiddled with them in an attempt to get the program to do what I want. 我有一个完整的工作程序,但是在我试着让条件程序执行我想要的事情之后,我对几个条件语句中的逻辑感到非常困惑。
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);
Now my program prompts for the number of Columns and then Rows. 现在,我的程序提示输入“列数”和“行数”。 For example, I'll put in 20 for columns, and 5 for rows - columns are obviously more than rows then.
例如,我将在列中输入20,在行中输入5-列显然比行多。 So this happens:
因此,这发生了:
col 20
row 5
colmore
size 20
Obviously that's what I want it to do, but I'm getting hung up because that first condition 显然,这就是我想要的,但是我要挂断电话,因为第一个条件
if (row>col)
should mean if rows are more than columns, and the program should continue to the next statement... or am I just completely losing my mind...? 应该意味着如果行多于列,并且程序应该继续执行下一条语句……还是我完全失去了理智……?
I would try something like this: 我会尝试这样的事情:
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);
}
Changes: 变化:
With if (row>col) you are saying if rows number(int) is greater than col number(int) then run this code 如果使用if(row> col),则表示行号(int)大于列号(int),然后运行此代码
size = 400/col;console.log("colmore");
and skip this code 并跳过此代码
else if (col>row) {size = 400/row;console.log("rowmore");}
else {size = 400/col;console.log("same");}
and then run this code 然后运行此代码
console.log("size " + size);
I have edited your code and broken down the steps to help you. 我已经编辑了您的代码并分解了为您提供帮助的步骤。
//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);
Your definately on the right track. 您一定在正确的轨道上。 Would recommend using isNaN() method.
建议使用isNaN()方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.