我是编码的新手,我遇到了一些困难。谁能告诉我我的代码有什么问题?提前致谢。我试图将每个夏天分配给1-4之间的数字,但它不起作用。我认为我的条件声明可能会被取消。
function randomSeason() {
var s = prompt("Enter a number between one and four");
var r = Math.floor(Math.random() * 4 ) ;
var x= "";
if (r===1) ;
x=" fall";
alert(" It's fall");
"else if"; (r===2); {
x =" spring";
alert(" It's spring") ;
}
"else"; }
if (r===3);
x="winter";
alert(" It's winter!"); {
}
"else if"; ( r===4);
alert(" It's summer!");
{
alert(r);
}
答案 0 :(得分:1)
你想做这样的事吗?
function randomSeason() {
var s = prompt("Enter a number between one and four");
var r = Math.floor(Math.random() * 4) + 1);
var x= "";
if (r===1){
x="fall";
alert("It's fall");
}else if(r===2){
x ="spring";
alert("It's spring");
}else if(r===3){
x="winter";
alert("It's winter!");
}else if(r===4){
x="summer";
alert("It's summer!");
}else{
alert(r);
}
}
答案 1 :(得分:0)
r
将始终位于0-3
之间,因为您使用的是Math.floor
解决这个问题的三种方法:
Math.ceil
代替Math.floor
Math.floor
答案 2 :(得分:-1)
var seasons = ['spring', 'summer', 'fall', 'winter'];
var which = Math.floor(Math.random() * 4);
alert('It\'s ' + seasons[which] + '!');