LoginSignup
0
0

More than 3 years have passed since last update.

JavaとC#とJavaScriptの違い(肥満度の求め方)

Last updated at Posted at 2019-10-12

JavaやC#なら理解できてるがJavaScriptに関しては流れるように終わっていったので振り返るためアウトプット

BMIというのは
体重(kg)/(身長(m)*身長(m))
で求められる。

[実行例]

体重(kg)>70.2
身長(cm)>175.2
あなたのBMIは22.87です。

これを以下の言語で求めると・・・

Java(コンソール)

import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        System.out.print("体重(kg)>");
        double weight=sc.nextDouble();
        System.out.print("身長(cm)>");
        double height=sc.nextDouble();
        //cmをmに変換
        height/=100; 
        double bmi=weight/(height*height);
        System.out.printf("あなたのBMIは%.2fです。",bmi);
    }
}

シンプルに他にもやり方はあると思う。

C#(コンソール)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BmiApp {
    class Program {
        static void Main(string[] args) {
            Console.Write("身長(cm)>");
            var height = float.Parse(Console.ReadLine());
            Console.Write("体重(kg)>");
            var weight = float.Parse(Console.ReadLine());
            CalcBMI(height, weight, out float bmi, out string result);
            Console.WriteLine($"身長:{height}cm,体重:{weight}kgのあなたのBMIは{bmi:F2}。\n{result}です");
        }
        static void CalcBMI(float heightCm,float weightKg,out float bmi,out string result) {
            bmi = weightKg / (heightCm / 100 * heightCm / 100);
            if (bmi >= 25.0f) {
                result = "肥満";
            }else if(bmi >=18.5f) {
                result = "標準体重";
            } else {
                result = "痩せ型";
            }
        }
    }
}

メソッドで切り分けたバージョン
個人的にC#は他の言語のいいとこ取りをして作られてる気がしている。

@albireo さんがリファクタリングしてくれた例
using System;

namespace BmiApp {
    class Program {
        static void Main(string[] args) {
            Console.Write("身長(cm)>");
            var height = float.Parse(Console.ReadLine());
            Console.Write("体重(kg)>");
            var weight = float.Parse(Console.ReadLine());
            var (bmi, result) = CalcBMI(height/100, weight);
            Console.WriteLine($"身長:{height}cm,体重:{weight}kgのあなたのBMIは{bmi:F2}。\n{result}です");
        }
        static (float, string) CalcBMI(float height, float weight) {
            float bmi = weight / (height * height);
            if (bmi >= 25.0f) {
                return (bmi, "肥満");
            }else if(bmi >=18.5f) {
                return (bmi, "標準体重");
            } else {
                return (bim, "痩せ型");
            }
        }
    }
}

ValueTupleを使用したバージョン
勉強になります。

JavaScript(Jsp)

<!DOCTYPE html>
<html lang="ja">
<head>
    <meta charset="utf-8">
    <title>title</title>
</head>
<body>
    身長(cm):<input type="number" step="0.1" min="0" id="height"><br>
    体重(kg):<input type="number" step="0.1" min="0" id="weight"><br>
    <button id="bt">計測</button>
    <div id="bmi"></div>
    <div id="result"></div>

<script>
//strictモード(厳格モード):JavaLikeに//
/*より的確なエラーチェックが行われるため
これまでエラーにならなかったような曖昧な実装がエラー扱いになる。*/
//曖昧表現のままでも・・書き方は人による
'use strict';
//Htmlを読み込んでから実行
window.onload=function(){
    //Domの取得:
    const eleHeight=document.getElementById("height");
    const eleWeight=document.getElementById("weight");
    const eleBt=document.getElementById("bt");
    const eleBmi=document.getElementById("bmi");
    const eleResult=document.getElementById("result");
    eleBt.addEventListener("click",function(){
        //Flot変換//
        let height=parseFloat(eleHeight.value)/100;
        let weight=parseFloat(eleWeight.value);
        let bmi=weight/(height*height);
        //toFixed小数点二桁//
        eleBmi.textContent='BMI:'+bmi.toFixed(2); 
        let result;
        if(bmi<18.5){
            result='低体重';
        }else if(bmi<25){
            result='普通';
        }else if(bmi<30){
            result='肥満(1度)';
        }else if(bmi<35){
            result='肥満(2度)';
        }else if(bmi<40){
            result='肥満(3度)';
        }else{
            result='肥満(4度)';
        }
        //// テキスト内容を設定する////
        eleResult.textContent=result;
    });
}
</script>
</body>
</html>

@albireo さんがリファクタリングしてくれた例
<!DOCTYPE html>
<html lang="ja">
<head>
    <meta charset="utf-8">
    <title>title</title>
    <script>
    'use strict';
    //計測ボタンクリックで実行
    function CalcBMI(){
        //Flot変換//
        let height=parseFloat(document.getElementById('height').value)/100;
        let weight=parseFloat(document.getElementById('weight').value);
        //toFixed小数点二桁//
        let bmi=weight/(height*height).toFixed(2);
        document.getElementById("bmi").textContent='BMI:'+bmi;
        let result = '';
        if(bmi<18.5){
            result='低体重';
        }else if(bmi<25){
            result='普通';
        }else if(bmi<30){
            result='肥満(1度)';
        }else if(bmi<35){
            result='肥満(2度)';
        }else if(bmi<40){
            result='肥満(3度)';
        }else{
            result='肥満(4度)';
        }
        //// テキスト内容を設定する////
        document.getElementById("result").textContent=result;
    }
    </script>
</head>
<body>
    身長(cm):<input type="number" step="0.1" min="0" id="height"><br>
    体重(kg):<input type="number" step="0.1" min="0" id="weight"><br>
    <button id="bt" onclick="CalcBMI()">計測</button>
    <div id="bmi"></div>
    <div id="result"></div>
</body>
</html>

かなりシンプルになるんですなー
と思ってたら

@htsign さんからparseFloat(document.getElementById(id).value) よりは document.getElementById(id).valueAsNumber の方がいいと。

勉強になります。お二方に感謝です。

0
0
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0