我有一个完整的工作程序,但是在我试图让程序按照我想要的方式进行操作之后,我对几个条件语句中的逻辑感到非常困惑。
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);
现在我的程序会提示输入Columns的数量,然后显示Rows的数量。例如,我将为列添加20,为行添加5 - 列显然比行更多。所以这发生了:
col 20
row 5
colmore
size 20
显然,这就是我想要它做的事情,但我因为第一个条件而被挂断了
if (row>col)
应该表示行是否超过列,并且程序应该继续下一个语句......或者我只是完全失去理智......?
答案 0 :(得分:1)
我会尝试这样的事情:
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);
}
的变化:
答案 1 :(得分:0)
使用if(row&gt; col)表示如果rows number(int)大于col number(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()方法。