简体   繁体   English

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

[英]how to add if..else statement in my code

In my code, how can I add the if statement so it print different statements depend on different situtation. 在我的代码中,如何添加if语句,以便它根据不同的情况打印不同的语句。 for example, I like to have a statement for the sum < 1, and else sum > 1. I try w3 school but the example if statement does not work for some reason 例如,我喜欢对sum <1进行声明,否则对sum> 1进行测试。

<!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>

First of all you might want to look here: https://www.w3schools.com/jsref/met_doc_writeln.asp 首先,您可能想在这里查看: https : //www.w3schools.com/jsref/met_doc_writeln.asp

Right now you writing in the head of the html not in the body 现在,您在html的头部而不是正文中书写

 <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> 

After you get the sum, you could add an if-statement similar to this code if you so desire: 得到总和后,如果您愿意,可以添加类似于以下代码的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 you have more questions about if-conditional statements O'Reilly has a number of excellent technical books dealing with JavaScript. 如果您对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>" ); 

First off, judging by your short description on what you want, I understand you want the statement of whatever you want to say after you calculate the total. 首先,通过对所需内容的简短描述来判断,我知道您希望在计算出总额之后就可以说出想要说的话。 To do that, it is really easy. 为此,这确实很容易。

Example: 例:

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

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

The reason I didn't put else if because if the sum is greater than one, it runs whatever statement you want it to run. 我之所以没有提出else if理由, else if因为如果总和大于1,它将运行您希望其运行的任何语句。 If it isn't, then it will run the other, the else statement. 如果不是,它将运行另一个else语句。

If you want to look at more if/else examples, you can go on this StackOverflow post and check the examples they have and how to use it. 如果您想查看更多的if/else示例,则可以继续此StackOverflow 帖子,并检查其示例以及如何使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM