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