高尔夫比分阵列-ActionScript 3.0

时间:2018-07-26 15:57:15

标签: arrays actionscript-3

我正在制作一个程序,让用户输入高尔夫球得分,然后将其存储在数组中。但是,用户最多只能输入18个分数,而我试图对提示进行编码,该提示指示阵列中/输入的分数的数量。该标签称为lblPrompt1,它不起作用。当用户输入所有18个分数时,我也想禁用addcore按钮。提示不起作用。请指教。谢谢!

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

    }
}

1 个答案:

答案 0 :(得分:3)

尽管您不清楚要问什么,但您遇到的一个问题是您的holeNumber变量永远不会有数值-它将始终为NaN N ot A N um。

)。

只要在点击处理程序函数(displayScores)内单击“显示分数”按钮,就会创建此变量(holeNumber),但不会赋予它任何值。数字默认为NaN,因此以后用holeNumber++递增时,您仍然会以NaN结束(因为NaN1仍然NaN

该问题的另一部分是,您在点击处理程序的范围内创建变量,并且仅将其递增一次,因此,即使将var定义更改为var holeNumber:Number = 0;,它仍然会有一个值每次点击1的值,因为每次点击都会重新创建变量,然后将其递增1。

您可能想要做的是完全放弃holeNumber变量,而只引用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;

    }
}