計算、割り勘


<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>割り勘計算機</title>
<style>
body {
font-family: Arial, sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: #f0f0f0 ;
}
.calculator {
background-color: white;
padding: 2rem;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
text-align: center;
}
h1 {
color: #333 ;
margin-bottom: 1rem;
}
input {
width: 100%;
padding: 0.5rem;
margin: 0.5rem 0;
border: 1px solid #ddd ;
border-radius: 4px;
box-sizing: border-box;
}
button {
background-color: #4CAF50 ;
color: white;
border: none;
padding: 0.5rem 1rem;
margin: 1rem 0;
border-radius: 4px;
cursor: pointer;
transition: background-color 0.3s;
}
button:hover {
background-color: #45a049 ;
}
#result {
font-weight: bold;
margin-top: 1rem;
}
</style>
</head>
<body>
<div class="calculator">
<h1>割り勘計算機</h1>
<input type="number" id="total" placeholder="合計金額" min="0" step="1">
<input type="number" id="people" placeholder="人数" min="1" step="1">
<button onclick="calculate()">計算する</button>
<div id="result"></div>
</div>

<script>
    function calculate() {
        const total = parseFloat(document.getElementById('total').value);
        const people = parseInt(document.getElementById('people').value);
        const resultElement = document.getElementById('result');

        if (isNaN(total) || isNaN(people) || total <= 0 || people <= 0) {
            resultElement.textContent = '正しい値を入力してください。';
            return;
        }

        const perPerson = Math.ceil(total / people);
        resultElement.textContent = `一人あたり: ${perPerson}円`;
    }
</script>

</body>
</html>

いいなと思ったら応援しよう!