If 语句返回错误答案
If-statement returning wrong answer
我正在尝试让我的函数 printResult(total3,total4);
向我的参数 'Player' 和 'Dealer' 显示文本 'Safe' 或 'Busted',具体取决于变量的总值; total3
和 total4
.
当我尝试在 console.log(ANSWER) 中预览 ANSWER 时,我得到:
Player: busted, Dealer: safe
我正在尝试为我的答案寻找解决方案以显示:
Player: safe, Dealer: safe
是否像我在下面的代码中那样使用两个 if 语句来尝试获取 var total3 和 total4 的总值的正确方法?
var c4 =5;
var c5 =1;
var c6 =4;
var d4 =1;
var d5 =11;
var d6 =1;
var total3 = c4+c5+c6;
var total4 = d4+d5+d6;
var printResult = function(player,dealer){
var game1 = "Player: "+total3 +", Dealer: "+total4;
return game1;
}
if (total3 > total4){
total3 = 'safe';
total4 = 'busted';
}
if (total4 > total3) {
total4 = 'safe';
total3 = 'busted';
}
ANSWER = printResult(total3,total4);
当您到达第二个 if
语句时,您已经更改了变量 total3
和 total4
,因此您要比较的是 'busted' > 'safe'
.
对要打印的字符串使用单独的变量,不要重复使用现有变量,并使用else if
。
此外,请检查您的 printResult
函数,它没有使用您传递给它的参数。
它做的正是它应该做的。
total4(13) 高于 total3 (10)。
此外,您将变量 player 和 dealer 放入函数 printResult 中,并且您没有在函数范围内使用它们。相反,您使用全局变量 total3 和 total4.
您的语法不允许出现该结果。
如果第一个 if 循环的计算结果为真,则第二个必须为假,反之亦然。
并且由于 if 循环中的代码将一个值设置为 safe,将另一个值设置为 busted,因此情况总是如此。
我假设这是一个 21 点风格的游戏,建议您分别评估变量,然后检查谁是赢家。
// Declare static max value the indicates if safe or busted
var MAX = 21;
var c4 =5;
var c5 =1;
var c6 =4;
var d4 =1;
var d5 =11;
var d6 =1;
var total3 = c4+c5+c6;
var total4 = d4+d5+d6;
// Declares 3 variables to hold results for player, dealer and winner
var player = '';
var dealer = '';
var winner = '';
var printResult = function(player, dealer, winner){
var game1 = "Player: "+ player +", Dealer: "+ dealer + ", " + winner + " has won.";
return game1;
}
if (total3 > MAX)
{
player = 'busted';
}
else
{
player = 'safe';
}
if (total4 > MAX)
{
dealer = 'busted';
}
else
{
dealer = 'safe';
}
if (dealer == 'busted' || (total3 > total4 && player == 'safe'))
{
winner = 'player';
}
else
{
winner = 'dealer';
}
printResult(player, dealer, winner);
我正在尝试让我的函数 printResult(total3,total4);
向我的参数 'Player' 和 'Dealer' 显示文本 'Safe' 或 'Busted',具体取决于变量的总值; total3
和 total4
.
当我尝试在 console.log(ANSWER) 中预览 ANSWER 时,我得到:
Player: busted, Dealer: safe
我正在尝试为我的答案寻找解决方案以显示:
Player: safe, Dealer: safe
是否像我在下面的代码中那样使用两个 if 语句来尝试获取 var total3 和 total4 的总值的正确方法?
var c4 =5;
var c5 =1;
var c6 =4;
var d4 =1;
var d5 =11;
var d6 =1;
var total3 = c4+c5+c6;
var total4 = d4+d5+d6;
var printResult = function(player,dealer){
var game1 = "Player: "+total3 +", Dealer: "+total4;
return game1;
}
if (total3 > total4){
total3 = 'safe';
total4 = 'busted';
}
if (total4 > total3) {
total4 = 'safe';
total3 = 'busted';
}
ANSWER = printResult(total3,total4);
当您到达第二个 if
语句时,您已经更改了变量 total3
和 total4
,因此您要比较的是 'busted' > 'safe'
.
对要打印的字符串使用单独的变量,不要重复使用现有变量,并使用else if
。
此外,请检查您的 printResult
函数,它没有使用您传递给它的参数。
它做的正是它应该做的。 total4(13) 高于 total3 (10)。 此外,您将变量 player 和 dealer 放入函数 printResult 中,并且您没有在函数范围内使用它们。相反,您使用全局变量 total3 和 total4.
您的语法不允许出现该结果。
如果第一个 if 循环的计算结果为真,则第二个必须为假,反之亦然。
并且由于 if 循环中的代码将一个值设置为 safe,将另一个值设置为 busted,因此情况总是如此。
我假设这是一个 21 点风格的游戏,建议您分别评估变量,然后检查谁是赢家。
// Declare static max value the indicates if safe or busted
var MAX = 21;
var c4 =5;
var c5 =1;
var c6 =4;
var d4 =1;
var d5 =11;
var d6 =1;
var total3 = c4+c5+c6;
var total4 = d4+d5+d6;
// Declares 3 variables to hold results for player, dealer and winner
var player = '';
var dealer = '';
var winner = '';
var printResult = function(player, dealer, winner){
var game1 = "Player: "+ player +", Dealer: "+ dealer + ", " + winner + " has won.";
return game1;
}
if (total3 > MAX)
{
player = 'busted';
}
else
{
player = 'safe';
}
if (total4 > MAX)
{
dealer = 'busted';
}
else
{
dealer = 'safe';
}
if (dealer == 'busted' || (total3 > total4 && player == 'safe'))
{
winner = 'player';
}
else
{
winner = 'dealer';
}
printResult(player, dealer, winner);