2012-11-13 55 views
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,我真的apreciate您的幫助。謝謝

+0

你知道條件語句是什麼嗎? – Xymostech

+0

你的'randomNumber()'函數中不需要'-0'或'+ 0'。 –

+0

@Xymostech是的,我願意。如果,否則,切換。但我不是很熟悉它,因爲我是一個新手。我試圖做一個條件聲明,如果showRandom_txt等於1那麼其他數字應該等於零 – kingst

回答

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

哇!謝謝你的幫助。我試過了,它現在正在工作。 :) – kingst

相關問題