Javascript 如何将随机数分配给用户输入?

Javascript 如何将随机数分配给用户输入?,javascript,html,Javascript,Html,在我的代码中,我试图设置一个神奇的八分球,从我选择的案例中输出一个随机答案。我想知道是否有可能给用户的输入分配一个随机数,然后输出其中一个案例。我可以在没有用户问题的情况下分配问题,但是否有可能有用户问题,然后忽略他们输入的内容,将所述问题随机分配 <html> <body> <p>Click the button to ask a question.</p> <button onclick="myEightball()&quo

在我的代码中,我试图设置一个神奇的八分球,从我选择的案例中输出一个随机答案。我想知道是否有可能给用户的输入分配一个随机数,然后输出其中一个案例。我可以在没有用户问题的情况下分配问题,但是否有可能有用户问题,然后忽略他们输入的内容,将所述问题随机分配

<html>
<body>

<p>Click the button to ask a question.</p>

<button onclick="myEightball()">I will answer any question</button>

<p id="eightball"></p>

<script>
function myEightball() {
  var text;
  var userQuestion = prompt("What would you like to know?");
  var randomNumber = Math.floor(Math.random()*8);
switch(randomNumber){
  case 1: 
  console.log('It is certain');
  break;

  case 2: 
  console.log('It is decidely so');
  break;

  case 3:
  console.log('Reply hazy try again');
  break;

  case 4:
  console.log('Cannot predict now');
  break;

  case 5:
  console.log('Do not count on it');
  break;

  case 6:
  console.log('My sources say no!');
  break;

  case 7:
  console.log('Outlook is not good');
  break;

  case 0:
  console.log('Signs point to yes!');
  break;
  
  default:
  console.log('Ask again later!');
  break;
  }
  document.getElementById("eightball").innerHTML = text;
}
</script>

</body>
</html>```

单击按钮提出问题

我会回答任何问题

函数myEightball(){ var文本; var userQuestion=prompt(“您想知道什么?”); var randomNumber=Math.floor(Math.random()*8); 开关(随机数){ 案例1: console.log(“它是确定的”); 打破 案例2: log(“事实确实如此”); 打破 案例3: log('Reply hazy重试'); 打破 案例4: log('现在无法预测'); 打破 案例5: log('不要指望它'); 打破 案例6: log('我的消息来源说不!'); 打破 案例7: console.log('Outlook不好'); 打破 案例0: log('符号指向yes!'); 打破 违约: log('稍后再问!'); 打破 } document.getElementById(“eightball”).innerHTML=text; } ```
您可以使用
Math.random
生成值。然后,选择您的
输入
并更新其

例如:

const rando=Math.floor(Math.random()*8);
控制台日志(rando);
document.getElementById('input')。value=rando

只需将文本变量的值设置为要显示给用户的字符串,即可执行以下操作。下面的代码如下所示

<html>
<body>

<p>Click the button to ask a question.</p>

<button onclick="myEightball()">I will answer any question</button>

<p id="eightball"></p>

<script>
function myEightball() {
  var text;
  var userQuestion = prompt("What would you like to know?");
  var randomNumber = Math.floor(Math.random()*8);
switch(randomNumber){
  case 1: 
  console.log('It is certain');
  text = 'It is certain';
  break;

  case 2: 
  console.log('It is decidely so');
  text = 'It is decidely so';
  break;

  case 3:
  console.log('Reply hazy try again');
  text = 'Reply hazy try again';
  break;

  case 4:
  console.log('Cannot predict now');
  text = 'Cannot predict now';
  break;

  case 5:
  console.log('Do not count on it');
  text = 'Do not count on it';
  break;

  case 6:
  console.log('My sources say no!');
  text = 'My sources say no!';
  break;

  case 7:
  console.log('Outlook is not good');
  text = 'Outlook is not good';
  break;

  case 0:
  console.log('Signs point to yes!');
  text = 'Signs point to yes!';
  break;
  
  default:
  console.log('Ask again later!');
  text = 'Ask again later!';
  break;
  }
  document.getElementById("eightball").innerHTML = text;
}
</script>

</body>
</html>

单击按钮提出问题

我会回答任何问题

函数myEightball(){ var文本; var userQuestion=prompt(“您想知道什么?”); var randomNumber=Math.floor(Math.random()*8); 开关(随机数){ 案例1: console.log(“它是确定的”); text='它是确定的'; 打破 案例2: log(“事实确实如此”); text=‘确实如此’; 打破 案例3: log('Reply hazy重试'); text='回复模糊,请重试'; 打破 案例4: log('现在无法预测'); text='现在无法预测'; 打破 案例5: log('不要指望它'); text='不要指望它'; 打破 案例6: log('我的消息来源说不!'); text='我的消息来源说不!'; 打破 案例7: console.log('Outlook不好'); text='前景不好'; 打破 案例0: log('符号指向yes!'); text='符号指向是!'; 打破 违约: log('稍后再问!'); text='稍后再问!'; 打破 } document.getElementById(“eightball”).innerHTML=text; }
so,而不是
console.log('some string')
do
text='some string'
,因为您在switch语句之后向文档输出了文本?