寻找那些比我更了解情况的人的指导。
我正在编写一个程序,提示用户点击一个按钮,然后生成一个从1到100的随机整数。
我可以很好地管理那部分。我正在努力的是让程序打印出这个随机整数的范围,即i <= 33。
例如,当我点击我想看的按钮时:
&#34; 10是一个小于33&#34;
的数字或
&#34; 55是33到66之间的数字&#34;
到目前为止,这是我的代码,我已将我遇到问题的代码放在评论中。
<!DOCTYPE HTML5>
<html>
<head>
<meta charset="UTF-8">
<title>question2</title>
</head>
<body>
<p>Click the button to display a random number from 1 to 100.</p>
<button onclick="myFunction()">Click here</button>
<p id="demo"></p>
<script>
function myFunction() {
var text = "";
var i = Math.floor((Math.random() * 100) + 1);
document.getElementById("demo").innerHTML = i;
}
// if (myFunction <= 33) {
// text += ("<br>" + i + " is a number less than or equal to 33");
// }
// if (myFunction > 34 and i < 65) {
// text += ("<br>" + i + " is a number between 33 and 66 (exclusive)");
// }
// if (my Function >= 66) {
// text += ("<br>" + i + " is a number greater than or equal to 66");
// }
</script>
</body>
</html>
谢谢你们。
答案 0 :(得分:2)
您可以测试,true
是否显示结果并退出。
function getRandom() {
var i = Math.floor((Math.random() * 100) + 1);
if (i <= 33) {
document.getElementById("demo").innerHTML += i + " is a number less than or equal to 33<br>";
return;
}
if (i < 66) {
document.getElementById("demo").innerHTML += i + " is a number between 33 and 66 (exclusive)<br>";
return;
}
document.getElementById("demo").innerHTML += i + " is a number greater than or equal to 66<br>";
}
&#13;
<p>Click the button to display a random number from 1 to 100.</p>
<button onclick="getRandom()">Click here</button>
<p id="demo"></p>
&#13;
答案 1 :(得分:1)
你的意思是这样的:
function myFunction() {
var text = "";
var i = Math.floor((Math.random() * 100) + 1);
if (i <= 33) {
text += ("<br>" + i + " is a number less than or equal to 33");
}
if (i > 34 && i < 65) {
text += ("<br>" + i + " is a number between 33 and 66 (exclusive)");
}
if (i >= 66) {
text += ("<br>" + i + " is a number greater than or equal to 66");
}
document.getElementById("demo").innerHTML = text;
}
答案 2 :(得分:1)
问题是你在该函数之外的myFunction()
内部调用变量。示例包括i
和myFunction()
。最好将if else
语句放在main函数中。
function myFunction() {
var demo = document.getElementById("demo");
var i = Math.floor((Math.random() * 100) + 1);
if (i <= 33) {
demo.innerHTML = i + " is a number less than 33";
}
else if (i < 65) {
demo.innerHTML = i + "is a number between 33 and 65";
}
else {
demo.innerHTML = i + " is a number greater than or equal to 66";
}
}
<p>Click the button to display a random number from 1 to 100.</p>
<button onclick="myFunction()">Click here</button>
<p id="demo"></p>
答案 3 :(得分:1)
您的功能不是您在if语句中应该比较的功能。
但是,我确实扔了一些东西让我知道这是不是你想要的。
你走在正确的轨道上。同样在中间比较中,我将“和”更改为短路“&amp;&amp;”。
JSfiddle:https://jsfiddle.net/xew8dvbd/
$(document).ready(function() {
function myFunction() {
var i = Math.floor((Math.random() * 100) + 1);
var text = "";
if (i <= 33) {
text += ("<br>" + i + " is a number less than or equal to 33");
}
if (i > 34 && i < 65) {
text += ("<br>" + i + " is a number between 33 and 66 (exclusive)");
}
if (i >= 66) {
text += ("<br>" + i + " is a number greater than or equal to 66");
}
document.getElementById("demo").innerHTML = text;
};
$("#submit").click(myFunction);
});
答案 4 :(得分:0)
不确定原因,但我必须将其作为函数表达式而不是函数定义才能使其工作。奇怪的是,这是第二次在最后一小时内作为陷阱工作。
public class Form1 : Form
{
private readonly ListArticle _articles = new ListArticle();
// assume strings have been added to articles by this point.
// option 1 - use same ListArticle instance
public void Foo()
{
var form = new Form2();
form.Articles = _articles;
}
// option 2 - add strings to new ListArticle
public void Bar()
{
var articles = new ListArticle();
articles.Clothes.AddRange(_articles.Clothes);
var form = new Form2();
form.Articles = articles;
}
}
public class Form2 : Form
{
public ListArticle Articles { get; set; }
}
&#13;
myFunction = function() {
var text = "";
var i = Math.floor((Math.random() * 100) + 1);
document.getElementById("demo").innerHTML = i;
switch (true) {
case (i < 20):
console.log(i + " is less than 20.");
break;
case (i < 40):
console.log(i + " is between 21 and 40.");
break;
case (i < 60):
console.log(i+ " is between 21 and 60.");
break;
default:
console.log(i+ " is over 60.");
}
};
&#13;