簡體   English   中英

Golf Scores Array - Actionscript 3.0

[英]Golf Scores Array - Actionscript 3.0

我正在制作一個程序,讓用戶輸入高爾夫分數,然后將其存儲在一個數組中。 但是,用戶最多只能輸入18個分數,並且我嘗試編寫一個提示符,該提示符表示陣列/他們輸入的分數。 該標簽名為lblPrompt1,它不起作用。 我還想在用戶輸入所有18個分數時禁用addscore按鈕。 提示不起作用。 請指教。 謝謝!

// Purpose: To add golf scores to an array

// This line makes the button, btnAddScore wait for a mouse click
// When the button is clicked, the addName function is called
btnAddScore.addEventListener(MouseEvent.CLICK, addScore);

// This line makes the button, btnDisplayScores wait for a mouse click
// When the button is clicked, the displayScores function is called
btnDisplayScores.addEventListener(MouseEvent.CLICK, displayScores);

// declare the global variables
var scores: Array = new Array(); // array of golf scores

// This is the addName function
// e:MouseEvent is the click event experienced by the button
// void indicates that the function does not return a value
function addScore(e: MouseEvent): void {

    // declare the variables
    var golfScore: String; // friend's name entered by user
    // get the name from the user
    golfScore = txtinScore.text;
    // append the name to the end of the array
    scores.push(golfScore);
    // display the length of the array in the label
    lblArraySize.text = "Number of Golf Scores Entered: " + scores.length;

}
// This is the displayNames function
// e:MouseEvent is the click event experienced by the button
// void indicates that the function does not return a value
function displayScores(e: MouseEvent): void {
    var holeNumber: Number;
    lblScores.text = "";
    for (var x = 0; x < scores.length; x++) {

        lblScores.text += scores[x] + "\r";

    }

    holeNumber++;
    if (holeNumber <= 18) {

        lblPrompt1.text = "Enter the score for hole #" + holeNumber.toString() + ":";

    } else {

        lblPrompt1.text = "All scores are entered.";
        txtinScore.text = "";
        btnAddScore.enabled = false;

    }
}

雖然你不清楚你在問什么,但你遇到的一個問題是你的holeNumber變量永遠不會有數值 - 它總是NaNN ot A N umber)。

每當單擊顯示分數按鈕時,在單擊處理函數( displayScores )中,您創建此變量( holeNumber )並且不為其賦值。 數字默認為NaN ,所以稍后當你用holeNumber++遞增它時,你最終仍然會使用NaN (因為NaN1仍然是NaN )。

該問題的另一部分是,您在單擊處理程序的范圍內創建變量,並且只增加一次,因此即使您將var定義更改為var holeNumber:Number = 0; ,每次單擊時它的值仍然為1因為每次單擊變量得到重新創建然后遞增1。

您可能想要做的是,完全放棄holeNumber var並僅引用scores.length因為這基本上是當前的漏洞。

function displayScores(e: MouseEvent): void {
    lblScores.text = "";
    for (var x = 0; x < scores.length; x++) {
        lblScores.text += scores[x] + "\r";
    }

    //use scores.length instead of holeNumber
    if (scores.length < 18) {

        lblPrompt1.text = "Enter the score for hole #" + String(scores.length + 1) + ":";
        //adding 1 to the length because that would be the next hole number

    } else {

        lblPrompt1.text = "All scores are entered.";
        txtinScore.text = "";
        btnAddScore.enabled = false;

    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM