这是我需要帮助的问题。
我需要随机更改名称。我还需要两个名称都相同。
如果您注意到,可以使两个随机名称相同。我犯了一些错误,我需要解决这个问题,以便第一个和第二个名称是唯一的。我想将生成的第一个名称传递给第二个名称的getoption
函数,以便第二个名称不能是此名称。这就是我遇到麻烦,有人可以帮助我吗?
到目前为止,我有代码来改变数字,但我不确定如何随机化名称
function getrandomnumber(min, max, notin) {
return min + Math.floor((max - min + 1) * Math.random())
}
function getoption(s, ch, num) {
var a = s.split(ch);
return a[num - 1];
}
var marymoney = getrandomnumber(50, 100, "");
var johnmoney = getrandomnumber(50, 100, "");
var maryitem = getrandomnumber(5, 20, "");
var johnitem = getrandomnumber(5, 20, "");
var marystuff = getoption("notebook,pencil,ruler,pen,eraser,binder,backpack", ",", getrandomnumber(1, 7));
var johnstuff = getoption("notebook,pencil,ruler,pen,eraser,binder,backpack", ",", getrandomnumber(1, 7));
var totalleft = marymoney + johnmoney - maryitem - johnitem;
var str = "Mary had $" + marymoney + " and John had $" + johnmoney + ". Mary buys a " + marystuff + " for $" + maryitem + " and John buys a " + +" for $" + johnitem + ". They have $" + totalleft + ".";
document.write(str);

答案 0 :(得分:1)
您可以添加以下内容以确保项目名称不相同:
while(marystuff === johnstuff) {
johnstuff = getoption("notebook,pencil,ruler,pen,eraser,binder,backpack", ",", getrandomnumber(1, 7));
}
最后,您johnstuff
也遗漏了str
。
尝试运行下面的代码,看看它是否有效:
var person1, person2;
function getPeople() {
var people = ['Mary', 'John', 'Misa', 'Steve', 'Amy', 'David'];
person1 = people[Math.floor(Math.random() * people.length)];
person2 = people[Math.floor(Math.random() * people.length)];
while (person1 === person2) {
person2 = people[Math.floor(Math.random() * people.length)];
}
}
function getrandomnumber(min, max, notin) {
return min + Math.floor((max - min + 1) * Math.random());
}
function getoption(s, ch, num) {
var a = s.split(ch);
return a[num - 1];
}
var marymoney = getrandomnumber(50, 100, "");
var johnmoney = getrandomnumber(50, 100, "");
var maryitem = getrandomnumber(5, 20, "");
var johnitem = getrandomnumber(5, 20, "");
var marystuff = getoption("notebook,pencil,ruler,pen,eraser,binder,backpack", ",", getrandomnumber(1, 7));
var johnstuff = getoption("notebook,pencil,ruler,pen,eraser,binder,backpack", ",", getrandomnumber(1, 7));
while(marystuff === johnstuff) {
johnstuff = getoption("notebook,pencil,ruler,pen,eraser,binder,backpack", ",", getrandomnumber(1, 7));
}
getPeople();
var totalleft = marymoney + johnmoney - maryitem - johnitem;
var str = person1 + " had $" + marymoney + " and " + person2 + " had $" + johnmoney + ". " + person1 + " buys a " + marystuff + " for $" + maryitem + " and " + person2 + " buys a " + johnstuff + " for $" + johnitem + ". They have $" + totalleft + ".";
document.write(str);
答案 1 :(得分:1)
试试这个,它对我有用:
function getrandomnumber(min, max, notin) {
return min + Math.floor((max - min + 1) * Math.random())
}
function getoptions(s, ch) {
var a = s.split(ch);
a.sort( function() { return 0.5 - Math.random() } );
return a;
}
var marymoney = getrandomnumber(50, 100, "");
var johnmoney = getrandomnumber(50, 100, "");
var maryitem = getrandomnumber(5, 20, "");
var johnitem = getrandomnumber(5, 20, "");
var stuff = "notebook,pencil,ruler,pen,eraser,binder,backpack";
var options = getoptions(stuff, ',');
var marystuff = options[0];
var johnstuff = options[1];
var totalleft = marymoney + johnmoney - maryitem - johnitem;
var str = "Mary had $" + marymoney + " and John had $" + johnmoney +
". Mary buys a " + marystuff + " for $" + maryitem +
" and John buys a " + johnstuff + " for $" + johnitem +
". They have $" + totalleft + ".";
document.write(str);