如何在我的代码中添加if..else语句

时间:2017-04-19 22:47:06

标签: javascript

在我的代码中,如何添加if语句,以便根据不同的情况打印不同的语句。例如,我喜欢有一个总和的声明< 1,否则总和> 1.我尝试w3学校,但示例if语句由于某种原因不起作用

<!DOCTYPE html>
<!-- Network Latency Calculator -->
<html>
<head>
  <meta charset = "utf-8">
  <title>Network Latency Calculation</title>
  <script>

     var firstNumber; // first string entered by user
     var secondNumber; // second string entered by user
     var thirdNumber; // third string entered by user
     var fourthNumber; // fourth string entered by user
     var number1; // first number to add
     var number2; // second number to add
     var number3; // third number to add
     var number4; // fourth number to add
     var sum; // sum of number1 and number2 and number3 and number4



     // read in first number from user as a string
     firstNumber = window.prompt( "Enter the Propagation time  (in milliseconds)" );

     // read in second number from user as a string
    secondNumber = window.prompt( "Enter the Transmission time  (in milliseconds)" );

    // read in third number from user as a string
     thirdNumber = window.prompt( "Enter the Queuing time  (in milliseconds)" );

    // read in fourth number from user as a string
     fourthNumber = window.prompt( "Enter the Propagation delay  (in milliseconds)" );

     // convert numbers from strings to integers
     number1 = parseInt( firstNumber ); 
     number2 = parseInt( secondNumber );
     number3 = parseInt( thirdNumber );
     number4 = parseInt( fourthNumber );

     sum = number1 + number2 + number3 + number4; // add the numbers





     // display the results
     document.writeln( "<h1>The network latency is " + sum + "</h1>" );



  </script>

3 个答案:

答案 0 :(得分:1)

首先,你可能想看看这里: https://www.w3schools.com/jsref/met_doc_writeln.asp

现在你写的是html的头部而不是正文

&#13;
&#13;
<body>

<p>Note that write() does NOT add a new line after each statement:</p>

<pre>
<script>
var NowDate = new Date();
var number1 = NowDate.getHours(); //added current hour 0-23
var number2 = 5; // second number to add
var number3 = 0.3; // third number to add
var sum = number1+number2*number3;
if (sum > 5){
    document.write("That's a");
    document.write(" big Sum ("+sum+")");
} else if (sum === 4) {
    document.write("Sum =");
    document.write(" 4");
}else{
    document.write("Sum is ");
    document.write("small ("+sum+")");
}
</script>
</pre>


<p>Note that writeln() add a new line after each statement:</p>

<pre>
<script>
document.writeln("Hello World!");
document.writeln("Have a nice day!");
</script>
</pre>

</body>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

获得总和之后,如果您愿意,可以添加类似于此代码的if语句:

if (sum < 1) {
   document.write ("The sum is less than one");
} else if (sum > 1) {
   document.write( "The sum is more than one"); 
}

如果您对if条件语句有更多疑问,O'Reilly有很多关于JavaScript的优秀技术书籍。

var firstNumber; // first string entered by user
 var secondNumber; // second string entered by user
 var thirdNumber; // third string entered by user
 var fourthNumber; // fourth string entered by user
 var number1; // first number to add
 var number2; // second number to add
 var number3; // third number to add
 var number4; // fourth number to add
 var sum; // sum of number1 and number2 and number3 and number4



 // read in first number from user as a string
 firstNumber = window.prompt( "Enter the Propagation time  (in milliseconds)" );

 // read in second number from user as a string
secondNumber = window.prompt( "Enter the Transmission time  (in milliseconds)" );

// read in third number from user as a string
 thirdNumber = window.prompt( "Enter the Queuing time  (in milliseconds)" );

// read in fourth number from user as a string
 fourthNumber = window.prompt( "Enter the Propagation delay  (in milliseconds)" );

 // convert numbers from strings to integers
 number1 = parseInt( firstNumber ); 
 number2 = parseInt( secondNumber );
 number3 = parseInt( thirdNumber );
 number4 = parseInt( fourthNumber );

 sum = number1 + number2 + number3 + number4; // add the numbers

if (sum < 1) {
   document.write ("The sum is less than one");
} else if (sum > 1) {
   document.write( "The sum is more than one"); 
}

 // display the results
document.writeln( "<h1>The network latency is " + sum + "</h1>" );

答案 2 :(得分:0)

首先,根据您对所需内容的简短描述判断,我了解您希望在计算总数后想要说出的任何内容。要做到这一点,这真的很容易。

示例:

sum = number1 + number2 + number3 + number4; // add the numbers

if(sum > 1){
    //Your code
} else {
    //Your code
}

我没有放else if的原因是因为如果总和大于1,它会运行你希望它运行的任何语句。如果不是,那么它将运行另一个else语句。

如果您想查看更多if/else示例,可以继续使用此StackOverflow post并查看他们拥有的示例以及如何使用它们。