英文:
Not displaying output in JS number game
问题
以下是您代码的翻译部分:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>随机数游戏</title>
</head>
<body>
<div>
<h1>猜一个随机数!</h1>
<input type="text" id="input" placeholder="在此输入猜测!"><br>
<button onclick="displayOutput()">提交</button>
<p>在上方的空格中输入1到100之间的数字,看看你是否猜对了。</p>
<span id="output"></span>
</div>
<script>
function displayOutput() {
let inputValue = document.getElementById("input").value;
let randomNumber = Math.floor(Math.random() * 100) + 1;
if (inputValue == null || inputValue == "") {
document.getElementById("output").innerHTML = "你必须输入1到100之间的值。";
} else if (inputValue != randomNumber) {
document.getElementById("output").innerHTML = "你的猜测不正确。再试一次。";
} else {
document.getElementById("output").innerHTML = "你猜对了!你的数字是 " + inputValue + ",做得好。";
}
}
</script>
</body>
</html>
请注意,我已经修复了代码中的几个错误,包括随机数生成方式和条件语句的语法。希望这可以帮助您让您的随机数游戏正常运行。如果您还有其他问题,请随时提出。
英文:
I'm a student and I'm doing an assignment for my coding class which involves me making a random number game that asks the user for input and then display the output. So far my code looks like this. We just started conditionals and I'm unsure about a couple of things.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Random Number Game</title>
</style>
</head>
<body>
<div>
<h1>Guess a random number!</h1>
<input type="text" id="input" placeholder="Type guess here!"><br>
<button onclick="displayOutput()">Submit</button>
<p>Type in a number 1 - 100 in the space above and see if you were right or not.</p>
<span id="output"></span>
</div>
<script>
function displayOutput() {
let inputValue = document.getElementById("input").value;
let randomNumber = Math.floor(Math.random() * 1, 101);
if (inputValue == null || inputValue == "") {
document.getElementById("output").innerHTML =
"You have to enter a value between 1 and 100";
} else if (inputValue != randomNumber) {
document.getElementById("output").innerHTML =
"Your guess was incorrect. Try again.";
} else (inputValue == randomNumber) {
document.getElementById("output").innerHTML =
"You guessed correctly! Your number was " + inputValue "Good job.";
}
}
</script>
</body>
</html>
I put a number in and was obviously, expecting it to display and it didn't. I thought maybe I got some names wrong or messed up in the conditionals. Can someone tell me what I did wrong?
答案1
得分: 1
你的代码中存在一些小问题。
-
首先,你添加了
</style>
,这没有任何意义。我只是将其删除了。 -
else
语句不能带有条件,如果你想添加更多条件,应该使用else if (条件)
,但else
是最后一个,不带条件的。
这是经过小改动的你的代码。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Random Number Game</title>
</head>
<body>
<div>
<h1>猜一个随机数!</h1>
<input type="text" id="input" placeholder="在这里输入猜测!"><br>
<button onclick="displayOutput()">提交</button>
<p>在上面的空格中输入1 - 100之间的数字,看看你是否猜对了。</p>
<span id="output"></span>
</div>
<script>
function displayOutput() {
let inputValue = document.getElementById("input").value;
let randomNumber = Math.floor(Math.random() * 100) + 1;
if (inputValue == null || inputValue == "") {
document.getElementById("output").innerHTML =
"你必须输入一个1到100之间的值";
} else if (inputValue != randomNumber) {
document.getElementById("output").innerHTML =
"你的猜测不正确,请再试一次。";
} else {
document.getElementById("output").innerHTML =
"你猜对了!你的数字是 " + inputValue + ",做得好。";
}
}
</script>
</body>
</html>
英文:
There were small issues in your code.
-
First of all you added
</style>
that isn't making any sense. I simply removed that. -
else
statement cannot have a condition if you want to add more conditions you should useelse if (condition)
butelse
is the last one with no condition.
Here, is your code with little changes.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Random Number Game</title>
</head>
<body>
<div>
<h1>Guess a random number!</h1>
<input type="text" id="input" placeholder="Type guess here!"><br>
<button onclick="displayOutput()">Submit</button>
<p>Type in a number 1 - 100 in the space above and see if you were right or not.</p>
<span id="output"></span>
</div>
<script>
function displayOutput() {
let inputValue = document.getElementById("input").value;
let randomNumber = Math.floor(Math.random() *1,101);
if (inputValue == null || inputValue == "") {
document.getElementById("output").innerHTML =
"You have to enter a value between 1 and 100";
} else if (inputValue != randomNumber) {
document.getElementById("output").innerHTML =
"Your guess was incorrect. Try again.";
} else{
document.getElementById("output").innerHTML =
"You guessed correctly! Your number was " + inputValue+ "Good job.";
}
}
</script>
</body>
</html>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论