-1
我一直在嘗試這個,它仍然說這是錯誤的。它一直說numberOfRounds是未定義的。有另一種方法可以做到嗎?我希望它運行「選擇___顏色」語句的用戶需要的次數。如何將用戶輸入存儲在變量中?
<html>
<!--This is the start screen that you see to begin the game. You also select how many rounds of the game you would like.-->
<font size="6"><center><strong><p id="startScreen">Welcome to the Random Color Game.</p></strong></center></font>
<font size="4"><center><p id="startScreen2">How many many rounds of the game would you like?</p>
<form id="numberOfRounds"><p id="startScreen3">I would like <input id="numberOfRounds" type="number" min="1" max="20" name="numberOfRounds"> rounds.</p>
<p id="startScreen5">To start playing the game, push begin.</p></center></font>
<center><h4 id="sayColor"></h4></center>
<center><p id="startButton"><button type="button" onclick="startGame();buttonColors();">Begin</button></p></center>
<!--this is the paragraph that will have all the buttons placed inside.-->
<center><p id="game"></p><center>
<script>
\t
\t var randomNumber = 0;
\t var redPressed = false;
\t var bluePressed = false;
\t var greenPressed = false;
\t var purplePressed = false;
\t var orangePressed = false;
\t var x = 1;
function startGame()
{
\t buttonColors();
\t //gets rid of the start screen text
\t document.getElementById("startScreen").innerHTML = "";
\t document.getElementById("startScreen2").innerHTML = "";
\t document.getElementById("startScreen3").innerHTML = "";
\t document.getElementById("startScreen5").innerHTML = "";
\t //makes the text for the game
\t document.getElementById("startButton").innerHTML = "<button type='button' onclick='location.reload()'>Restart</button>";
\t document.getElementById("game").innerHTML = "<button type='button' onclick='redPressed = true;redCheck();' style='background-color:red'>Red</button><button type='button' onclick='bluePressed = true;blueCheck();' style='background-color:blue'>Blue</button><button type='button' onclick='greenPressed = true;greenCheck();' style='background-color:green'>Green</button><button type='button' onclick='purplePressed = true;purpleCheck();' style='background-color:purple'>Purple</button><button type='button' onclick='orangePressed = true;orangeCheck();' style='background-color:orange'>Orange</button>";
\t //checks to see if the function ran
\t console.log("startGame() ran.");
\t makeRounds();
}
function makeRounds()
{
\t //takes the number of rounds and puts it into a variable so it shows the amount of questions you want
\t var numberOfRounds = document.getElementById("numberOfRounds").value;
\t console.log(numberOfRounds);
\t x = numberOfRounds*2;
\t console.log(x);
\t while (x<=numberOfRounds)
\t {
\t \t randomNumber = (Math.floor(Math.random() * 5));
\t \t console.log(randomNumber);
\t }
}
function buttonColors()
{
\t if (randomNumber==1)
\t {
\t \t document.getElementById("sayColor").innerHTML = "Push the red button."
\t }
\t if (randomNumber==2)
\t {
\t \t document.getElementById("sayColor").innerHTML = "Push the blue button."
\t }
\t if (randomNumber==3)
\t {
\t \t document.getElementById("sayColor").innerHTML = "Push the green button."
\t }
\t if (randomNumber==4)
\t {
\t \t document.getElementById("sayColor").innerHTML = "Push the purple button."
\t }
\t if (randomNumber==5)
\t {
\t \t document.getElementById("sayColor").innerHTML = "Push the orange button."
\t }
}
function redCheck()
{
\t if (randomNumber==1)
\t {
\t \t correct();
\t }
\t else
\t {
\t \t incorrect();
\t }
\t x--;
}
function blueCheck()
{
\t if (randomNumber==2)
\t {
\t \t correct();
\t }
\t else
\t {
\t \t incorrect();
\t }
\t x--;
}
function greenCheck()
{
\t if (randomNumber==3)
\t {
\t \t correct();
\t }
\t else
\t {
\t \t incorrect();
\t }
\t x--;
}
function purpleCheck()
{
\t if (randomNumber==4)
\t {
\t \t correct();
\t }
\t else
\t {
\t \t incorrect();
\t }
\t x--;
}
function orangeCheck()
{
\t if (randomNumber==5)
\t {
\t \t correct();
\t }
\t else
\t {
\t \t incorrect();
\t }
\t x--;
}
function correct()
{
\t console.log("DATS RIGHT!!");
\t window.alert("Correct!")
}
function incorrect()
{
\t console.log("Incorrect.");
\t window.alert("Incorrect.");
}
</script>
</html>
您有'numberOfRounds' id的多個聲明。 – Cyval
您的startGame()函數會清除startScreen3中包含numberOfRounds元素的內容。在刪除內容之前捕獲這些數據。 – jeff