以下是我根据用户在应用中输入的一组数字生成强力球票的代码。
num1 = ""
num2 = ""
num3 = ""
num4 = ""
num5 = ""
num6 = ""
var numString = "1325102719740829201002101968241083020081601103414208523811"
function megaNum(numString){
numType1 = [(Math.floor(Math.random() * 6 ))];
if (numType1 == 0){
do{
num1 = numString.split('')[(Math.floor(Math.random() * numString.length ))];
}
while (num1 == 0);
num1 = "0" + num1
}
if (numType1 != 0){
do{
digit1 = numString.split('')[(Math.floor(Math.random() * numString.length ))];
}
while (digit1 > 6 || digit1 == 0);
digit2 = numString.split('')[(Math.floor(Math.random() * numString.length ))];
num1 = digit1.concat(digit2)
}
numType2 = [(Math.floor(Math.random() * 6 ))];
if (numType2 == 0){
do{
num2 = numString.split('')[(Math.floor(Math.random() * numString.length ))];
}
while (num2 == 0);
num2 = "0" + num2
}
if (numType2 != 0){
do{
digit3 = numString.split('')[(Math.floor(Math.random() * numString.length ))];
}
while (digit3 > 6 || digit3 == 0);
digit4 = numString.split('')[(Math.floor(Math.random() * numString.length ))];
num2 = digit3.concat(digit4)
}
numType3 = [(Math.floor(Math.random() * 6 ))];
if (numType3 == 0){
do{
num3 = numString.split('')[(Math.floor(Math.random() * numString.length ))];
}
while (num3 == 0);
num3 = "0" + num3
}
if (numType3 != 0){
do{
digit5 = numString.split('')[(Math.floor(Math.random() * numString.length ))];
}
while (digit5 > 6 || digit5 == 0);
digit6 = numString.split('')[(Math.floor(Math.random() * numString.length ))];
num3 = digit5.concat(digit6)
}
numType4 = [(Math.floor(Math.random() * 6 ))];
if (numType4 == 0){
do{
num4 = numString.split('')[(Math.floor(Math.random() * numString.length ))];
}
while (num4 == 0);
num4 = "0" + num4
}
if (numType4 != 0){
do{
digit7 = numString.split('')[(Math.floor(Math.random() * numString.length ))];
}
while (digit7 > 6 || digit7 == 0);
digit8 = numString.split('')[(Math.floor(Math.random() * numString.length ))];
num4 = digit7.concat(digit8)
}
numType5 = [(Math.floor(Math.random() * 6 ))];
if (numType5 == 0){
do{
num5 = numString.split('')[(Math.floor(Math.random() * numString.length ))];
}
while (num5 == 0);
num5 = "0" + num5
}
if (numType5 != 0){
do{
digit9 = numString.split('')[(Math.floor(Math.random() * numString.length ))];
}
while (digit9 > 6 || digit9 == 0);
digit10 = numString.split('')[(Math.floor(Math.random() * numString.length ))];
num5 = digit9.concat(digit10)
}
numType6 = [(Math.floor(Math.random() * 6 ))];
if (numType6 == 0){
do{
num6 = numString.split('')[(Math.floor(Math.random() * numString.length ))];
}
while (num6 == 0);
num6 = "0" + num6
}
if (numType6 != 0){
do{
digit11 = numString.split('')[(Math.floor(Math.random() * numString.length ))];
}
while (digit11 > 2 || digit11 == 0);
do{
digit12 = numString.split('')[(Math.floor(Math.random() * numString.length ))];
}
while (digit12 > 6);
num6 = digit11.concat(digit12)
}
return (num1 + "-" + num2 + "-" + num3 + "-" + num4 + "-" + num5 + "-" + num6)
}
我的问题是如何设置数字1 - 5必须唯一的条件 - 不能重复?我尝试使用while循环,但陷入无限循环。我尝试了一些不同的方法,但不能让它出来正确。我知道我需要一种if语句,但不确定语法可以确保前五个数字是唯一的。
答案 0 :(得分:0)
很难说你试图从你提供的代码中解决问题,但我想我明白你在做什么。
这并不能完全满足您的需要,但您可以使用它来重构您的代码。特别是,查看doWork
函数,它是执行实际工作的函数。 getRandomNumber
和checkForUniqueness
函数只是帮助函数,以允许doWork
以更简洁的方式执行它需要做的事情。
function getRandomNumber(ceiling) {
// gets a random number lower than ceiling
let retVal = 0;
while (retVal == 0) {
retVal = Math.floor(Math.random() * ceiling);
}
return retVal;
}
function checkForUniqueness(arr, num) {
// check the previous indeces of the array to ensure uniqueness
let retVal = true;
if (arr.length > 0) {
for (let i = 0; i < arr.length; ++i) {
if (arr[i] == num) {
retVal = false;
break; // no need to check the rest of the numbers
}
}
}
return retVal;
}
function doWork() {
let numbers = [];
let num = 0;
while(numbers.length < 5) {
num = getRandomNumber(6);
while (!checkForUniqueness(numbers, num)) {
num = getRandomNumber(6);
}
numbers.push(num);
}
return numbers;
}
doWork(); // this results in an array containing [1,2,3,4,5] in a random order
基本上,您需要遵循的逻辑是:
// get a random number
// make sure the array (or whatever structure you're using) doesn't
// already contain that number
// insert that number