码迷,mamicode.com
首页 > Web开发 > 详细

JS入门基础(if else 与 switch case / node安装)

时间:2017-05-16 00:39:18      阅读:270      评论:0      收藏:0      [点我收藏+]

标签:strong   parse   创建文件   pac   rip   bsp   int   pre   oba   

在 JavaScript 中,为不同的决定来执行不同的动作,我们可使用以下条件语句:

  • if 语句 - 只有当指定条件为 true 时,使用该语句来执行代码
  • if...else 语句 - 当条件为 true 时执行代码,当条件为 false 时执行其他代码
  • if...else if....else 语句 - 使用该语句来选择多个代码块之一来执行
  • switch 语句 - 使用该语句来选择多个代码块之一来执行

 

多重判断(if..else嵌套语句)

if(条件1)
{ 条件1成立时执行的代码}
else  if(条件2)
{ 条件2成立时执行的代码}
...
else  if(条件n)
{ 条件n成立时执行的代码}
else
{ 条件1、2至n不成立时执行的代码}

案例一:

//  分别输入姓名,身高,体重,性别,判断身体状况。(体重按公斤计算)

    计算公式man=体重-(身高-100),woman=体重-(身高-110);大于3为超重,小于-3为偏瘦,其余为正常

var readline=require(readline-sync)
console.log(请输入姓名)
var name=readline.question();
console.log(请输入身高)
var height=parseInt(readline.question());
console.log(请输入体重)
var weight=parseInt(readline.question());
console.log(请输入性别(man/woman))
var sex=readline.question();

if(sex==man){
    var manhealth=weight-(height-100);
    if(manhealth>3){
        console.log(超重)
    }
    else if(manhealth<3){
        console.log(偏瘦)
    }
    else{
        console.log(健康)
    }
}
if(sex==woman){
    var health=parseInt(weight-(height-110));
    if(health>3){
        console.log(超重)
    }
    else if(health<3){
        console.log(偏瘦)
    }
    else{
        console.log(健康)
    }
}

当有很多种选项的时候,switch比if else使用更方便。

switch(表达式)
{
case值1:
  执行代码块 1
  break;
case值2:
  执行代码块 2
  break;
...
case值n:
  执行代码块 n
  break;
default:
  与 case值1 、 case值2...case值n 不同时执行的代码

break;循环就会结束,不会输出后面循环的内容
continue本次循环将被跳过,而后续的循环则不会受到影响

 

安装 node步骤:

1.node 安装

直接点一下就OK

2.修改镜像地址(下载模块)

npm conflig set registry=https://registry.npm.taobao.org

(学校局域网)服务器镜像:在命令行中敲入

npm config set registry=http://192.168.8.10:7001

3.查看是否安装成功:node -v

4.创建文件夹,cmd中进入文件

输入npm init 初始化文件,成功后会创建出一个package.json文件

5.在文件中创建一个***.js文件,里面写 console.log(‘111111‘)

6.用cmd打开,dos命令进入文件夹,运行nodee *** 或 node ***.js

安装输入模块:npm install readline-sync

下载成功会自动生成一个 node_modules 文件夹

 

JS入门基础(if else 与 switch case / node安装)

标签:strong   parse   创建文件   pac   rip   bsp   int   pre   oba   

原文地址:http://www.cnblogs.com/lingzi940924/p/6859007.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!