当前位置: 首页 > news >正文

就是这个样的粗爆,手搓一个计算器:卡路里计算器

       作为程序员,没有合适的工具,就得手搓一个,PC端,移动端均可适用。废话不多说,直接上代码。

HTML:

<div class="calculator"><h1>卡路里计算器</h1><div class="instructions"><p>请输入以下信息以计算您的每日卡路里需求:</p><ul><li><strong>性别:</strong>选择您的性别 (男性/女性)。</li><li><strong>年龄:</strong>输入您的年龄。</li><li><strong>体重:</strong>输入您的体重 (公斤)。</li><li><strong>身高:</strong>输入您的身高 (厘米)。</li><li><strong>活动水平:</strong>选择您的活动水平。</li></ul><p>点击“计算”按钮以获取每日卡路里需求。</p></div><div class="form"><label for="gender">性别:</label><select id="gender"><option value="male">男性</option><option value="female">女性</option></select><label for="age">年龄:</label><input type="number" id="age" placeholder="输入年龄"><label for="weight">体重 (公斤):</label><input type="number" id="weight" placeholder="输入体重"><label for="height">身高 (厘米):</label><input type="number" id="height" placeholder="输入身高"><label for="activity-level">活动水平:</label><select id="activity-level"><option value="sedentary">久坐不动</option><option value="light">轻度活动</option><option value="moderate">中度活动</option><option value="active">高度活动</option><option value="very-active">极度活动</option></select><button onclick="calculateCalories()">计算</button><div class="result"><h2>每日卡路里需求</h2><p id="calories"></p></div></div></div>

JS:

function calculateCalories() {const gender = document.getElementById('gender').value;const age = parseInt(document.getElementById('age').value);const weight = parseFloat(document.getElementById('weight').value);const height = parseFloat(document.getElementById('height').value);const activityLevel = document.getElementById('activity-level').value;let bmr;if (gender === 'male') {bmr = 88.362 + (13.397 * weight) + (4.799 * height) - (5.677 * age);} else {bmr = 447.593 + (9.247 * weight) + (3.098 * height) - (4.330 * age);}let multiplier;switch (activityLevel) {case 'sedentary':multiplier = 1.2;break;case 'light':multiplier = 1.375;break;case 'moderate':multiplier = 1.55;break;case 'active':multiplier = 1.725;break;case 'very-active':multiplier = 1.9;break;default:multiplier = 1.2;}const calories = bmr * multiplier;document.getElementById('calories').textContent = `每日卡路里需求: ${calories.toFixed(2)} 卡路里`;
}

CSS:

body {font-family: Arial, sans-serif;background-color: #f0f0f0;display: flex;justify-content: center;align-items: center;height: 100vh;margin: 0;
}.calculator {background-color: white;border-radius: 10px;box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);padding: 20px;max-width: 400px;width: 100%;
}h1 {font-size: 24px;margin-bottom: 20px;color: #333;
}.instructions {margin-bottom: 20px;font-size: 14px;color: #555;
}ul {padding-left: 20px;
}li {margin-bottom: 10px;
}label {display: block;margin: 10px 0 5px;font-weight: bold;color: #555;
}input, select {width: calc(100% - 10px);padding: 8px;margin-bottom: 10px;border: 1px solid #ddd;border-radius: 4px;
}button {background-color: #4CAF50;color: white;padding: 10px 20px;border: none;border-radius: 4px;cursor: pointer;margin-top: 10px;font-size: 16px;
}button:hover {background-color: #45a049;
}.result {margin-top: 20px;
}p {font-size: 18px;color: #333;
}

 线上运行,可以直接打开:卡路里计算器


http://www.mrgr.cn/news/52060.html

相关文章:

  • 产品经理图书推荐 《人工智能产品经理:AI时代PM修炼手册》
  • 基于单片机的搬运机器人控制系统
  • OpenCV高级图形用户界面(15)注册一个回调函数来处理鼠标事件的函数setMouseCallback()的使用
  • Cannot determine local hostname
  • KNN的 k 设置的过大会有什么问题
  • ZBrush和3D-Coat各自的优缺点是什么?
  • 番茄工作法计时器:高效时间管理利器
  • 鸿蒙开发案例:绘制中国象棋棋盘与棋子的技术教程
  • Java 枚举类
  • 自定义类型——结构体
  • c#编写的各类应用程序
  • 65.DDR3读写控制器的设计与验证(2)
  • 60. 排列序列
  • Windows11桌面快捷方式图标变为白色方块-如何解决
  • C++11 使用 {} 进行初始化
  • 【微信小程序_18_WXS脚本】
  • TemporalBench:一个专注于细粒度时间理解的多模态视频理解的新基准。
  • 从实习到春招成为一名安全工程师,我经历了什么
  • mqtt与云服务器
  • 2024了,传统行业转行AI,可不可行?