gpt4 book ai didi

JavaScript选择自己的冒险游戏随机数函数循环问题

转载 作者:行者123 更新时间:2023-12-03 00:08:00 24 4
gpt4 key购买 nike

我正在编写一个选择你自己的冒险程序,如果选择了特定选项(等待的示例),用户将获得 1-10 之间的随机数来进行俯卧撑(俯卧撑将是用户单击提示“确定”按钮,但随机数多次等于)这是到目前为止我的代码,但我不断收到错误。我是个十足的菜鸟,所以对我宽容一些。

 var count = Math.floor((Math.random() * 10) + 1);
var setsOf10 = false;
function pushUps() {
alert("Nice! Lets see you crank out " + pushUps + "!");
}
if (setsOf10 == pushUp) {
alert("Nice! Lets see you crank out " + pushUp + "!");
setsOf10 = true;
}
for (var i=0; i<count; i++){
pushUps();
}
else {
alert("Really, thats it? Try again");
}

while ( setsOf10 == false);
}

在玩了更多之后,我可以说我已经很接近了,但仍然没有。再说一次,我不是要求你帮我解决这个问题,只是需要指出我做错了什么或遗漏了什么。这就是我所拥有的,它给了我随机数,我只需要它来允许我单击“确定”按钮,无论随机数分配给我多少次。

    var pushUpSets = Math.floor((Math.random() * 10) + 1);
function pushUps(){
alert(pushUpSets);
if (pushUpSets < 3){
var weak = "Thats it? Weak sauce!";
alert(weak);
}
else{
alert("Sweet lets get some reps in!");
}
for (i=0; i>3; i++){
pushUps(pushUpSets);
}
}

最佳答案

在这里,“做出选择”按钮只是虚拟的,让我们可以去做俯卧撑。每次点击都会减少我们的计数。

// This is important, we use this event to wait and let the HTML (DOM) load
// before we go ahead and code.
document.addEventListener('DOMContentLoaded', () => {
document.querySelector('#choice').addEventListener('click', makeChoice);
});

function makeChoice() {
// Call a method to set random pushups and setup the click event
setUpPushUp();
// Here we change the display style of the push up section so that it shows to the player.
document.querySelector('.activity').style.display = 'block';
}

// The pushups variable is declared at the document level
// This way our setUpPushUp and doPushUp functions have easy access.
let pushUps = 0;

function setUpPushUp() {
// Create a random number of pushups, in sets of 10.
// We add an extra 1 so we can call the doPushUp method to initialize.
pushUps = (Math.floor((Math.random() * 10)+1)*10)+1 ;

// Add a click event to the push up button and call our doPushUp method on each click.
document.querySelector('#push').addEventListener('click', doPushUp);

// This is just an init call, it will use the extra 1 we added and place test in our P tag.
doPushUp();
}


function doPushUp() {
// Get a reference to our output element, we will put text to player here.
let result = document.querySelector('p');
// They have clicked, so remove a push up.
pushUps--;

// See if the player has done all the required push ups (i.e. pushUps is 0 or less.)
if (pushUps > 0) {
result.innerText = `You need to crank out ${pushUps} pushUps`;
} else {
result.innerText = 'Nice work!';
}

}
.activity {
display: none;
}
<button id="choice">Make a choice !</button>
<div class="activity">
<p></p>
<button id="push">Push</button>
</div>

关于JavaScript选择自己的冒险游戏随机数函数循环问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54874807/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com