var a = Math.floor(Math.random() * 10)
var b = 10 - a
var c = Math.floor(Math.random() * b )
var d = a + c
console.log("Jupiter threw " + a + " for the first round!")
console.log("Jupiter threw " + c + " for the second round!")
console.log("Jupiter scored a total of " + d + " points!")
console.log("Jupiter threw " + a + " for the first round!")
console.log("Jupiter threw " + c + " for the second round!")
console.log("Jupiter scored a total of " + d + " points!")
我是javascript尝试制作游戏的初学者,想知道你是否可以多次使用同一个变量,或者我必须为每一轮制作一个新的变量?
提前致谢!
答案 0 :(得分:0)
JavaScript具有动态数据类型,因此基本上您可以将String
或number
或boolean
分配给同一个无关紧要的变量。
这实际上取决于您的架构如何构建您的游戏。你必须花一些时间在这上面,但如果你的架构师可以处理一个变量,那就没问题了。否则你也可以使用多个变量。
答案 1 :(得分:0)
您可以重复使用相同的变量,这些变量在首次初始化后不会用var声明它们。请参阅下面的示例。
var a = Math.floor(Math.random() * 10)
var b = 10 - a
var c = Math.floor(Math.random() * b )
var d = a + c
console.log("Jupiter threw " + a + " for the first round!")
console.log("Jupiter threw " + c + " for the second round!")
console.log("Jupiter scored a total of " + d + " points!")
a = Math.floor(Math.random() * 20)
b = 20 - a
c = Math.floor(Math.random() * b )
d = a + c
console.log("Jupiter threw " + a + " for the first round!")
console.log("Jupiter threw " + c + " for the second round!")
console.log("Jupiter scored a total of " + d + " points!")