標準体重プログラム 2・3

標準体重プログラム 2(ダイアログボックスあり)



<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>標準体重</title>
</head>
<body>
<script type="text/javascript">
<!--
/*
* 標準体重計算プログラム 
* 最終修正日:2012.06.07
*/

var height; //身長
var weight; //体重

// 身長を入力する
height = prompt("身長を入力してください", 170);
height = parseInt(height); 

// 計算を行う
weight = (height -100) * 0.9;

// 結果を表示する
document.write("<h1>");
document.write("身長が", height, "cmの人の標準体重は");
document.write(weight, "kgです。");
document.write("<\/h1>");
// -->
</script>
</body>
</html>


標準体重プログラム 3

confirm( メッセージ )→ユーザーに確認を求めるダイアログボックス



<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>標準体重</title>
</head>
<body>
<script type="text/javascript">
<!--
var height; //身長
var weight; //体重
var man; //男性かどうか

//男性か女性かを入力
man = confirm("あなたは男性ですか?"); 

// 身長を代入する
height = prompt("身長を入力してください", 180);
height = parseInt(height); //文字列を整数に変換

// 計算を行う
if (man) {
	weight = (height -80) * 0.7;
} else {
	weight = (height -70) * 0.6;
}

// 結果を表示する
document.write("<h1>");
document.write("身長が", height, "cmの");
if (man) {
	document.write("男性の標準体重は");
} else {
	document.write("女性の標準体重は");
}
document.write(weight, "kgです。");
document.write("<\/h1>");
// -->
</script>
</body>
</html>