如何使用条件语句更改随机数的值

时间:2012-11-13 06:19:34

标签: flash actionscript adobe conditional

我正在做一个带有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的新手,我真的会帮助你。感谢

1 个答案:

答案 0 :(得分:0)

要评估平等,请使用== operator

可以在文本字段中对此进行测试,如:

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;
    }
}