javascript(四)多分岐文

19995 ワード

多分岐文にはif条件判定とswitch文ifが含まれていますが、主な役割は、登録が成功したかどうか、ユーザが存在しているかどうかなどの条件比較判断を行うことです.switchの主な役割は数値マッチングであり、マッチングが成功したら、後のステートメントグループを実行する.if...else文:式判定を行い、式が成立したかどうかを判断する時に関連する動作様式1:if()を行います.else;フォーマット2:if(){}else{}フォーマット3:if(){}else if(){}else{}説明:条件式が真であれば、ifの中のコード体elseを実行する必要はありません.条件式では直接数値などを書くことができますが、データの表現によっては、最後にブール値switch式と数値を比較して、成功した語法にマッチします.switch(){case:break;case:break;…default:break;switchとifの違い1.switchは等値しか処理できませんが、ifは等値でも区間でもいいです.等値を扱う場合はifよりも性能が速いです.
  • 練習1 prompt方法を使用して、入力年齢要求:入力の年齢範囲がどれぐらい小さいかを判断します.15~18コンソールに入ることを禁止するように指示します.「親電話:0717-00000」は18コンソールより大きいです.「個人連絡:0710-00000」
  • を出力します.
    "text/javascript"</span>>
            <span class="hljs-reserved">var</span> a=prompt(<span class="hljs-string">"     "</span>);
            <span class="hljs-regexp">//</span>    <span class="hljs-keyword">if</span>  <span class="hljs-keyword">else</span>
            <span class="hljs-keyword">if</span> (a>=<span class="hljs-number">0</span> && a<=<span class="hljs-number">15</span>) {
                <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"    "</span>);
            }<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (a><span class="hljs-number">15</span> && a<=<span class="hljs-number">18</span>){
                <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"    :0471-0000000"</span>);
            }<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(a>=<span class="hljs-number">18</span> && a<<span class="hljs-number">100</span>){
                <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"    :0471-0000000"</span>);
            }<span class="hljs-keyword">else</span>{
                <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"    "</span>);
            }
            <span class="hljs-regexp">//</span>    <span class="hljs-keyword">switch</span>
            <span class="hljs-keyword">switch</span>(<span class="hljs-literal">true</span>){
                <span class="hljs-reserved">case</span> a>=<span class="hljs-number">0</span> && a<=<span class="hljs-number">15</span>:<span class="hljs-built_in">console</span>.log(<span class="hljs-string">"    "</span>);
                <span class="hljs-keyword">break</span>;
                <span class="hljs-reserved">case</span> a><span class="hljs-number">15</span> && a<=<span class="hljs-number">18</span>:<span class="hljs-built_in">console</span>.log(<span class="hljs-string">"    :0471-0000000"</span>);
                <span class="hljs-keyword">break</span>;
                <span class="hljs-reserved">case</span> a>=<span class="hljs-number">18</span> && a<<span class="hljs-number">100</span>:<span class="hljs-built_in">console</span>.log(<span class="hljs-string">"    :0471-0000000"</span>);
                <span class="hljs-keyword">break</span>;
                <span class="hljs-attribute">default</span>:<span class="hljs-built_in">console</span>.log(<span class="hljs-string">"    "</span>);
            }
        
  • 練習2シーズン判断1、2、3は春、4、5、6は夏、7、8、9は秋、10、11、12は冬
  • です.
    "text/javascript"</span>>
            <span class="hljs-reserved">var</span> a=<span class="hljs-number">5</span>;
            <span class="hljs-regexp">//i</span>f <span class="hljs-keyword">else</span>
            <span class="hljs-keyword">if</span>(a==<span class="hljs-number">1</span> || a==<span class="hljs-number">2</span> || a==<span class="hljs-number">3</span>){
                <span class="hljs-built_in">document</span>.write(<span class="hljs-string">'  '</span>);
            }<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (a==<span class="hljs-number">4</span> || a==<span class="hljs-number">5</span> || a==<span class="hljs-number">6</span>){
                <span class="hljs-built_in">document</span>.write(<span class="hljs-string">'  '</span>);
            }<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (a==<span class="hljs-number">7</span> || a==<span class="hljs-number">8</span> || a==<span class="hljs-number">9</span>) {
                <span class="hljs-built_in">document</span>.write(<span class="hljs-string">'  '</span>);
            }<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (a==<span class="hljs-number">10</span> || a==<span class="hljs-number">11</span> || a==<span class="hljs-number">12</span>) {
                <span class="hljs-built_in">document</span>.write(<span class="hljs-string">'  '</span>);
            }<span class="hljs-keyword">else</span>{
                alert(<span class="hljs-string">"    "</span>);
            }
           <span class="hljs-regexp">//</span><span class="hljs-keyword">switch</span> <span class="hljs-reserved">case</span>
           <span class="hljs-keyword">switch</span>(a){
            <span class="hljs-reserved">case</span> <span class="hljs-number">1</span>:<span class="hljs-reserved">case</span> <span class="hljs-number">2</span>:<span class="hljs-reserved">case</span> <span class="hljs-number">3</span>:
            <span class="hljs-built_in">document</span>.write(<span class="hljs-string">"  "</span>);
            <span class="hljs-keyword">break</span>;
            <span class="hljs-reserved">case</span> <span class="hljs-number">4</span>:<span class="hljs-reserved">case</span> <span class="hljs-number">5</span>:<span class="hljs-reserved">case</span> <span class="hljs-number">6</span>:
            <span class="hljs-built_in">document</span>.write(<span class="hljs-string">"  "</span>);
            <span class="hljs-keyword">break</span>;
            <span class="hljs-reserved">case</span> <span class="hljs-number">7</span>:<span class="hljs-reserved">case</span> <span class="hljs-number">8</span>:<span class="hljs-reserved">case</span> <span class="hljs-number">9</span>:
            <span class="hljs-built_in">document</span>.write(<span class="hljs-string">"  "</span>);
            <span class="hljs-keyword">break</span>;
            <span class="hljs-reserved">case</span> <span class="hljs-number">10</span>:<span class="hljs-reserved">case</span> <span class="hljs-number">11</span>:<span class="hljs-reserved">case</span> <span class="hljs-number">12</span>:
            <span class="hljs-built_in">document</span>.write(<span class="hljs-string">"  "</span>);
            <span class="hljs-keyword">break</span>;
            <span class="hljs-attribute">default</span>:alert(<span class="hljs-string">"    "</span>);
            <span class="hljs-keyword">break</span>;
           }
      
  • 練習3判定閏年
  • <script type="text/javascript">
            var a=2016;
            //if else
            if (a>=0 && a%4==0) {
                document.write("  ");
            }else{
                document.write("  ");
            }
            //switch case
            switch(true){
                case a>=0 && a%4==0:document.write("  ");
                break;
                default:document.write("  ");
                break;
            }
     script>
  • 練習します.ある本屋で割引セールをしています.消費:0-100元です.100元割引していません.100-200元です.5%の割引は500元の7割引より大きいです.「商品の金額+割引+割引後の金額」
  • 元です.
    
            var <span class="hljs-literal">a</span>=<span class="hljs-number">500</span><span class="hljs-comment">;</span>
            //     <span class="hljs-keyword">if</span>  <span class="hljs-keyword">else</span><span class="hljs-comment">;</span>
            <span class="hljs-keyword">if</span> (<span class="hljs-literal">a</span>>=<span class="hljs-number">0</span> && <span class="hljs-literal">a</span><=<span class="hljs-number">100</span>) {
                document.write(<span class="hljs-literal">a</span>+<span class="hljs-string">"&nbsp;   &nbsp;"</span>+<span class="hljs-literal">a</span>)<span class="hljs-comment">;</span>
            }<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (<span class="hljs-literal">a</span>><span class="hljs-number">100</span> && <span class="hljs-literal">a</span><=<span class="hljs-number">200</span>) {
                document.write(<span class="hljs-literal">a</span>+<span class="hljs-string">"&nbsp;95 &nbsp;"</span>+<span class="hljs-literal">a</span>*.<span class="hljs-number">95</span>)<span class="hljs-comment">;</span>
            }<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (<span class="hljs-literal">a</span>><span class="hljs-number">200</span> && <span class="hljs-literal">a</span><=<span class="hljs-number">500</span>) {
                document.write(<span class="hljs-literal">a</span>+<span class="hljs-string">"&nbsp;8 &nbsp;"</span>+<span class="hljs-literal">a</span>*.<span class="hljs-number">8</span>)<span class="hljs-comment">;</span>
            }<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (<span class="hljs-literal">a</span>><span class="hljs-number">500</span>) {
                document.write(<span class="hljs-literal">a</span>+<span class="hljs-string">"&nbsp;7 &nbsp;"</span>+<span class="hljs-literal">a</span>*.<span class="hljs-number">7</span>)<span class="hljs-comment">;</span>
            }<span class="hljs-keyword">else</span>{
                alert(<span class="hljs-string">"     "</span>)<span class="hljs-comment">;</span>
            }
           //    switch case
           switch(<span class="hljs-literal">true</span>){
    <span class="hljs-label">            case a>=0 && a<=100:</span>document.write(<span class="hljs-literal">a</span>+<span class="hljs-string">"&nbsp;   &nbsp;"</span>+<span class="hljs-literal">a</span>)<span class="hljs-comment">;</span>
                <span class="hljs-keyword">break</span><span class="hljs-comment">;</span>
    <span class="hljs-label">            case a>100 && a<=200:</span>document.write(<span class="hljs-literal">a</span>+<span class="hljs-string">"&nbsp;95 &nbsp;"</span>+<span class="hljs-literal">a</span>*.<span class="hljs-number">95</span>)<span class="hljs-comment">;</span>
                <span class="hljs-keyword">break</span><span class="hljs-comment">;</span>
    <span class="hljs-label">            case a>200 && a<=500:</span>document.write(<span class="hljs-literal">a</span>+<span class="hljs-string">"&nbsp;8 &nbsp;"</span>+<span class="hljs-literal">a</span>*.<span class="hljs-number">8</span>)<span class="hljs-comment">;</span>
                <span class="hljs-keyword">break</span><span class="hljs-comment">;</span>
    <span class="hljs-label">            case a>500:</span>document.write(<span class="hljs-literal">a</span>+<span class="hljs-string">"&nbsp;7 &nbsp;"</span>+<span class="hljs-literal">a</span>*.<span class="hljs-number">7</span>)<span class="hljs-comment">;</span>
                <span class="hljs-keyword">break</span><span class="hljs-comment">;</span>
    <span class="hljs-label">            default:</span>alert(<span class="hljs-string">"     "</span>)<span class="hljs-comment">;</span>
           }
          //   
          <span class="hljs-keyword">if</span> (<span class="hljs-literal">a</span>><span class="hljs-number">0</span>){
              switch(<span class="hljs-literal">true</span>){
    <span class="hljs-label">               case a>=0 && a<=100:</span>document.write(<span class="hljs-literal">a</span>+<span class="hljs-string">"&nbsp;   &nbsp;"</span>+<span class="hljs-literal">a</span>)<span class="hljs-comment">;</span>
                   <span class="hljs-keyword">break</span><span class="hljs-comment">;</span>
    <span class="hljs-label">               case a>100 && a<=200:</span>document.write(<span class="hljs-literal">a</span>+<span class="hljs-string">"&nbsp;95 &nbsp;"</span>+<span class="hljs-literal">a</span>*.<span class="hljs-number">95</span>)<span class="hljs-comment">;</span>
                   <span class="hljs-keyword">break</span><span class="hljs-comment">;</span>
    <span class="hljs-label">               case a>200 && a<=500:</span>document.write(<span class="hljs-literal">a</span>+<span class="hljs-string">"&nbsp;8 &nbsp;"</span>+<span class="hljs-literal">a</span>*.<span class="hljs-number">8</span>)<span class="hljs-comment">;</span>
                   <span class="hljs-keyword">break</span><span class="hljs-comment">;</span>
    <span class="hljs-label">               case a>500:</span>document.write(<span class="hljs-literal">a</span>+<span class="hljs-string">"&nbsp;7 &nbsp;"</span>+<span class="hljs-literal">a</span>*.<span class="hljs-number">7</span>)<span class="hljs-comment">;</span>
                   <span class="hljs-keyword">break</span><span class="hljs-comment">;</span>
    <span class="hljs-label">               default:</span>alert(<span class="hljs-string">"     "</span>)<span class="hljs-comment">;</span>
                   <span class="hljs-keyword">break</span><span class="hljs-comment">;</span>
               }
            }<span class="hljs-keyword">else</span>{
                alert(<span class="hljs-string">"     "</span>)<span class="hljs-comment">;</span>
            }
           //   
           switch(<span class="hljs-literal">a</span>>=<span class="hljs-number">0</span> && <span class="hljs-literal">a</span><=<span class="hljs-number">100</span>){
    <span class="hljs-label">            case true:</span>document.write(<span class="hljs-literal">a</span>+<span class="hljs-string">"&nbsp;   &nbsp;"</span>+<span class="hljs-literal">a</span>)<span class="hljs-comment">;</span>
                <span class="hljs-keyword">break</span><span class="hljs-comment">;</span>
           }
           switch(<span class="hljs-literal">a</span>><span class="hljs-number">100</span> && <span class="hljs-literal">a</span><=<span class="hljs-number">200</span>){
    <span class="hljs-label">            case true:</span>document.write(<span class="hljs-literal">a</span>+<span class="hljs-string">"&nbsp;95 &nbsp;"</span>+<span class="hljs-literal">a</span>*.<span class="hljs-number">95</span>)<span class="hljs-comment">;</span>
                <span class="hljs-keyword">break</span><span class="hljs-comment">;</span>
           }
           switch(<span class="hljs-literal">a</span>><span class="hljs-number">200</span> && <span class="hljs-literal">a</span><=<span class="hljs-number">500</span>){
    <span class="hljs-label">            case true:</span>document.write(<span class="hljs-literal">a</span>+<span class="hljs-string">"&nbsp;8 &nbsp;"</span>+<span class="hljs-literal">a</span>*.<span class="hljs-number">8</span>)<span class="hljs-comment">;</span>
                <span class="hljs-keyword">break</span><span class="hljs-comment">;</span>
           }
           switch(<span class="hljs-literal">a</span>><span class="hljs-number">500</span>){
    <span class="hljs-label">            case true:</span>document.write(<span class="hljs-literal">a</span>+<span class="hljs-string">"&nbsp;7 &nbsp;"</span>+<span class="hljs-literal">a</span>*.<span class="hljs-number">7</span>)<span class="hljs-comment">;</span>
                <span class="hljs-keyword">break</span><span class="hljs-comment">;</span>
           }