<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Gioco dei Numeri: Indovinare i Numeri Casuali</title>
    <style>
<pre><code>&lt;/style&gt;
</code></pre>
</head>
<body>
    <table>
        <tr>
            <td><input type="text" name="" id="InputA" placeholder="Indovina"></td>
            <td><button></button></td>
        </tr>
        <tr>
            <td><input type="text" name="" id="InputB" placeholder="Indovina"></td>
            <td><button></button></td>
        </tr>        <tr>
            <td><input type="text" name="" id="InputC" placeholder="Indovina"></td>
            <td><button></button></td>
        </tr>
        <tr>
            <td>Tentativi primo:</td>
            <td><div class="risultato" id="Risultato A">0</div></td>
        </tr>
        <tr>
            <td>Tentativi secondo:</td>
            <td><div class="risultato" id="Risultato B">0</div></td>
        </tr>
        <tr>
            <td>Tentativi terzo:</td>
            <td><div class="risultato" id="Risultato C">0</div></td>
        </tr>
        <tr>
            <td colspan="2"><button>Gioca ancora</button></td>
        </tr>
    </table>
    <script>
// Function to generate a random number between min and max (inclusive)
function getRandomNumber(min, max) {
    return Math.floor(Math.random() * (max - min + 1) + min);
}
<p>// Generate 3 random numbers
var numberA = getRandomNumber(1, 10);
var numberB = getRandomNumber(1, 10);
var numberC = getRandomNumber(1, 10);</p>
<p>// Get user inputs
var inputA = document.getElementById(&quot;InputA&quot;);
var inputB = document.getElementById(&quot;InputB&quot;);
var inputC = document.getElementById(&quot;InputC&quot;);</p>
<p>// Get result elements
var resultA = document.getElementById(&quot;Risultato A&quot;);
var resultB = document.getElementById(&quot;Risultato B&quot;);
var resultC = document.getElementById(&quot;Risultato C&quot;);</p>
<p>// Set initial result values to 0
resultA.innerText = 0;
resultB.innerText = 0;
resultC.innerText = 0;</p>
<p>// Function to check user's input against the random number and update the result
function checkNumber(input, randomNumber, result) {
if (input.value == randomNumber) {
result.innerText = &quot;Vincita!&quot;;
} else {
result.innerText = &quot;Tentativo non riuscito&quot;;
}
}</p>
<p>// Event listeners for the buttons
document.querySelector(&quot;button:nth-of-type(1)&quot;).addEventListener(&quot;click&quot;, function() {
checkNumber(inputA, numberA, resultA);
});</p>
<p>document.querySelector(&quot;button:nth-of-type(2)&quot;).addEventListener(&quot;click&quot;, function() {
checkNumber(inputB, numberB, resultB);
});</p>
<p>document.querySelector(&quot;button:nth-of-type(3)&quot;).addEventListener(&quot;click&quot;, function() {
checkNumber(inputC, numberC, resultC);
});</p>
<p>// Reset the game
document.querySelector(&quot;button:nth-of-type(4)&quot;).addEventListener(&quot;click&quot;, function() {
numberA = getRandomNumber(1, 10);
numberB = getRandomNumber(1, 10);
numberC = getRandomNumber(1, 10);</p>
<pre><code>inputA.value = &quot;&quot;;
inputB.value = &quot;&quot;;
inputC.value = &quot;&quot;;

resultA.innerText = 0;
resultB.innerText = 0;
resultC.innerText = 0;
</code></pre>
<p>});
</script></p>
</body>
</html>

原文地址: https://www.cveoy.top/t/topic/pcYL 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录