【问题标题】:Why does If Else statment not output anything?为什么 If Else 语句不输出任何内容?
【发布时间】:2016-06-01 23:42:00
【问题描述】:

试图让用户输入四个数字、他们在哪里以及他们的目的地。然后输出他们将要前进的方向。
无论我输入哪个值,都会发生同样的事情。如何让代码使用提示的结果?

var output = document.getElementById("output");
var number=prompt ("What is your current Latitude?");
var number=prompt ("What is your current Longitude?");
var number=prompt ("What is your destination Latitude?");
var number=prompt ("What is your destination longitude?");


var intCurrentLatitude = 0;
var intCurrentLongitude = 0;
var intDestinationLatitude = 0;
var intDestinationLongitude = 0;

if ( (intCurrentLatitude<=intDestinationLatitude) && (intCurrentLongitude<=intDestinationLongitude) ) {
output.textContent = "We'd be headed North East, capt'n!";
}
else if ( ( intCurrentLatitude<=intDestinationLatitude) && (intCurrentLongitude>=intDestinationLongitude) ) {
    output.textContent = "Ye'd best head North West, captain!";
}
else if ( ( intCurrentLatitude>=intDestinationLatitude) && (intCurrentLongitude>=intDestinationLongitude) ) {
output.textContent = "Ye'd best head South West, captain!";
}
else if ( ( intCurrentLatitude>=intDestinationLatitude) && (intCurrentLongitude<=intDestinationLongitude) ) {
output.textContent = "Ye'd best head South East, captain!";
}
else{
output.textContent = "Land Ho!";
}

HTML

<head>
<meta charset="utf-8">
  <title>More if</title>

<body>
 <div id= "output">


 <div id= "input2">
    </div>

</body>


<script src="moreif.js"></script>
<script src="moreif2.js"></script>
</head>
</html>

【问题讨论】:

  • 提示:4 个变量,全为零
  • “似乎什么都不做” 不是正确的技术问题描述。是否抛出任何错误?
  • 它正在做你告诉它做的事情——什么也没做。
  • 旁注:prompt() 返回字符串值。如果您需要数字值,则需要转换它们。 How do I convert a string into an integer in JavaScript?
  • 看我的回答。我想我知道你想要什么了。

标签: javascript if-statement logic


【解决方案1】:

我不确定你想要完成什么,但代码正在做它应该做的事情。您的每个条件都在询问变量是否“小于或等于零”。

您正在测试的变量都为 0,因此第一个条件满足并且您的程序退出。

【讨论】:

    【解决方案2】:

    你不断改变数值,提示返回一个字符串。使用parseFloat 将字符串形式的小数转换为数字。您没有使用提示的结果。

    var output = document.getElementById("output");
    
    var intCurrentLatitude = parseFloat(prompt("What is your current Latitude?"));
    var intCurrentLongitude = parseFloat(prompt("What is your current Longitude?"));
    var intDestinationLatitude = parseFloat(prompt("What is your destination Latitude?"));
    var intDestinationLongitude = parseFloat(prompt("What is your destination longitude?"));
    
    if ( (intCurrentLatitude<=intDestinationLatitude) && (intCurrentLongitude<=intDestinationLongitude) ) {
    output.textContent = "We'd be headed North East, capt'n!";
    }
    else if ( ( intCurrentLatitude<=intDestinationLatitude) && (intCurrentLongitude>=intDestinationLongitude) ) {
        output.textContent = "Ye'd best head North West, captain!";
    }
    else if ( ( intCurrentLatitude>=intDestinationLatitude) && (intCurrentLongitude>=intDestinationLongitude) ) {
    output.textContent = "Ye'd best head South West, captain!";
    }
    else if ( ( intCurrentLatitude>=intDestinationLatitude) && (intCurrentLongitude<=intDestinationLongitude) ) {
    output.textContent = "Ye'd best head South East, captain!";
    }
    else{
    output.textContent = "Land Ho!";
    }
    <head>
    <meta charset="utf-8">
      <title>More if</title>
    
    <body>
     <div id= "output">
    
    
     <div id= "input2">
        </div>
    
    </body>
    
    
    <script src="moreif.js"></script>
    <script src="moreif2.js"></script>
    </head>
    </html>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-22
      • 1970-01-01
      • 1970-01-01
      • 2014-10-08
      • 2015-10-04
      • 1970-01-01
      • 2019-04-09
      • 2014-05-17
      相关资源
      最近更新 更多