Differences between Java, C # and JavaScript (how to determine the degree of obesity)

I understand Java and C #, but as for JavaScript, it ended up flowing, so I output it to look back.

BMI is Weight (kg) / (height (m) * height (m)) Is required by.

[Execution example]

Weight (kg)> 70.2 Height (cm)> 175.2 Your BMI is 22.87.

If you ask for this in the following languages ...

Java (console)

Example
import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        System.out.print("body weight(kg)>");
        double weight=sc.nextDouble();
        System.out.print("height(cm)>");
        double height=sc.nextDouble();
        //Convert cm to m
        height/=100; 
        double bmi=weight/(height*height);
        System.out.printf("Your BMI%.It is 2f.",bmi);
    }
}

I think there are simply other ways.

C # (console)

Example
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("height(cm)>");
            var height = float.Parse(Console.ReadLine());
            Console.Write("body weight(kg)>");
            var weight = float.Parse(Console.ReadLine());
            CalcBMI(height, weight, out float bmi, out string result);
            Console.WriteLine($"height:{height}cm,body weight:{weight}kg your BMI{bmi:F2}。\n{result}is");
        }
        static void CalcBMI(float heightCm,float weightKg,out float bmi,out string result) {
            bmi = weightKg / (heightCm / 100 * heightCm / 100);
            if (bmi >= 25.0f) {
                result = "obesity";
            }else if(bmi >=18.5f) {
                result = "Standard weight";
            } else {
                result = "Skinny type";
            }
        }
    }
}

Method carved version Personally, I feel that C # is made by taking advantage of other languages.

@albireo's refactoring example
using System;

namespace BmiApp {
    class Program {
        static void Main(string[] args) {
            Console.Write("height(cm)>");
            var height = float.Parse(Console.ReadLine());
            Console.Write("body weight(kg)>");
            var weight = float.Parse(Console.ReadLine());
            var (bmi, result) = CalcBMI(height/100, weight);
            Console.WriteLine($"height:{height}cm,body weight:{weight}kg your BMI{bmi:F2}。\n{result}is");
        }
        static (float, string) CalcBMI(float height, float weight) {
            float bmi = weight / (height * height);
            if (bmi >= 25.0f) {
                return (bmi, "obesity");
            }else if(bmi >=18.5f) {
                return (bmi, "Standard weight");
            } else {
                return (bim, "Skinny type");
            }
        }
    }
}

Version using Value Tuple informative.

JavaScript(Jsp)

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

<script>
//strict mode (strict mode):To Java Like//
/*Because more accurate error checking is performed
Ambiguous implementations that previously did not result in an error are treated as an error.*/
//Even if the expression is ambiguous ... How to write depends on the person
'use strict';
//Load Html and then execute
window.onload=function(){
    //Get 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 conversion//
        let height=parseFloat(eleHeight.value)/100;
        let weight=parseFloat(eleWeight.value);
        let bmi=weight/(height*height);
        //toFixed two decimal places//
        eleBmi.textContent='BMI:'+bmi.toFixed(2); 
        let result;
        if(bmi<18.5){
            result='Underweight';
        }else if(bmi<25){
            result='usually';
        }else if(bmi<30){
            result='obesity(Once)';
        }else if(bmi<35){
            result='obesity(Twice)';
        }else if(bmi<40){
            result='obesity(3 times)';
        }else{
            result='obesity(4 degrees)';
        }
        ////Set text content////
        eleResult.textContent=result;
    });
}
</script>
</body>
</html>
@albireo's refactoring example
<!DOCTYPE html>
<html lang="ja">
<head>
    <meta charset="utf-8">
    <title>title</title>
    <script>
    'use strict';
    //Execute by clicking the measurement button
    function CalcBMI(){
        //Flot conversion//
        let height=parseFloat(document.getElementById('height').value)/100;
        let weight=parseFloat(document.getElementById('weight').value);
        //toFixed two decimal places//
        let bmi=weight/(height*height).toFixed(2);
        document.getElementById("bmi").textContent='BMI:'+bmi;
        let result = '';
        if(bmi<18.5){
            result='Underweight';
        }else if(bmi<25){
            result='usually';
        }else if(bmi<30){
            result='obesity(Once)';
        }else if(bmi<35){
            result='obesity(Twice)';
        }else if(bmi<40){
            result='obesity(3 times)';
        }else{
            result='obesity(4 degrees)';
        }
        ////Set text content////
        document.getElementById("result").textContent=result;
    }
    </script>
</head>
<body>
height(cm):<input type="number" step="0.1" min="0" id="height"><br>
body weight(kg):<input type="number" step="0.1" min="0" id="weight"><br>
    <button id="bt" onclick="CalcBMI()">measurement</button>
    <div id="bmi"></div>
    <div id="result"></div>
</body>
</html>

It's going to be pretty simple If you think

@htsign says that document.getElementById (id) .valueAsNumber is better than parseFloat (document.getElementById (id) .value).

informative. Thank you to both of you.

Recommended Posts

Differences between Java, C # and JavaScript (how to determine the degree of obesity)
Difference between Java and JavaScript (how to find the average)
Let's sort out the differences between Java substring and C # Substring, and how to port them.
Summarize the differences between C # and Java writing
Differences in how to handle strings between Java and Perl
How to determine the number of parallels
[Java improvement case] How to reach the limit of self-study and beyond
Differences in writing Java, C # and Javascript classes
[Java] How to get the authority of the folder
[Java] How to get the URL of the transition source
How to write Scala from the perspective of Java
[Java] Types of comments and how to write them
Java language from the perspective of Kotlin and C #
[Java] How to get the maximum value of HashMap
A memo about the types of Java O/R mappers and how to select them
I tried to summarize the basics of kotlin and java
Command to check the number and status of Java threads
Comparison of how to write Callback function (Java, JavaScript, Ruby)
[Rails] Differences between redirect_to and render methods and how to output render methods
Think about the differences between functions and methods (in Java)
How to derive the last day of the month in Java
How to check the extension and size of uploaded files
[Ruby] How to get the value by specifying the key. Differences between hashes, symbols and fetch
Differences between "beginner" Java and Kotlin
The road from JavaScript to Java
Differences between Java and .NET Framework
java Eclipse How to debug javaScript
[Java] Various summaries attached to the heads of classes and members
[Rails] How to get the URL of the transition source and redirect
How to set the IP address and host name of CentOS8
From fledgling Java (3 years) to Node.js (4 years). And the impression of returning to Java
I tried to summarize the methods of Java String and StringBuilder
[Java] How to convert from String to Path type and get the path
How to check for the contents of a java fixed-length string
How to get the length of an audio file in java
How to increment the value of Map in one line in Java
[Java] How to use the File class
The story of forgetting to close a file in Java and failing
[Java] How to use the hasNext function
[java] Summary of how to handle char
[Java] How to use the HashMap class
Differences between preface and postfix of operators
[Java] How to get the current date and time and specify the display format
[Java] How to use the toString () method
The content of the return value of executeBatch is different between 11g and 12c
Studying how to use the constructor (java)
[Processing × Java] How to use the loop
[Java] Differences between instance variables and class variables
[Java] How to output and write files!
[Java] How to set the Date time to 00:00:00
Get a rough idea of the differences between protocols, classes and structs!
[Java] How to get the current directory
How to change the maximum and maximum number of POST data in Spark
How to find out the Java version of a compiled class file
[Java] [Maven3] Summary of how to use Maven3
[Processing × Java] How to use the class
[Java] How to get to the front of a specific string using the String class
How to find the total number of pages when paging in Java
How to sort the List of SelectItem
How to install the legacy version [Java]
How to find the tens and ones