1

我正在做一个带有 5 个动态文本的随机数生成器。我想要做的是当 showRandom_txt = 1 那么其他文本应该等于零

//1.
function randomNumbers(min:Number,max:Number) {
   var Results:Number=Math.floor(Math.random()*max)+min;
   return Results;
}

function randomNumber(){
   var Results:Number=Math.floor(Math.random()*(1+1-0))+0;
   return Results;
}


//2.
generate_btn.addEventListener(MouseEvent.CLICK, showRandomnumber);

//3.
function showRandomnumber(event:MouseEvent):void{
   showRandom_txt.text = randomNumber();
   showRandom2_txt.text = randomNumbers(0,9);
   showRandom3_txt.text = randomNumbers(0,9);
   showRandom4_txt.text = randomNumbers(0,9);
   showRandom5_txt.text = randomNumbers(0,9);
}

我是 AS3 的新手,非常感谢您的帮助。谢谢

4

1 回答 1

0

要评估相等性,请使用运算符==

这可以从文本字段中进行测试,如下所示:

if(showRandom_txt.text == "1") {}

也许更好的是针对数字类型进行测试,例如:

var n:Number = randomNumber();
if(n == 1) {}

使用 if / else 实现:

function showRandomNumber(event:MouseEvent):void
{
    var n:Number = randomNumber();
    showRandom_txt.text = n.toString();

    if (n == 1)
    {
        showRandom2_txt.text = showRandom3_txt.text = showRandom4_txt.text = showRandom5_txt.text = "0";
    }
    else
    {
        showRandom2_txt.text = randomNumbers(0, 9).toString();
        showRandom3_txt.text = randomNumbers(0, 9).toString();
        showRandom4_txt.text = randomNumbers(0, 9).toString();
        showRandom5_txt.text = randomNumbers(0, 9).toString();
    }
}

使用开关块:

function showRandomNumber(event:MouseEvent):void
{
    var n:Number = randomNumber();
    showRandom_txt.text = n.toString();

    switch(n)
    {
        case 1:
            showRandom2_txt.text = showRandom3_txt.text = showRandom4_txt.text = showRandom5_txt.text = "0";
            break;
        default:
            showRandom2_txt.text = randomNumbers(0, 9).toString();
            showRandom3_txt.text = randomNumbers(0, 9).toString();
            showRandom4_txt.text = randomNumbers(0, 9).toString();
            showRandom5_txt.text = randomNumbers(0, 9).toString();
            break;
    }
}
于 2012-11-13T06:55:57.743 回答