簡體   English   中英

猜數字游戲程序無法正常運行

[英]Guessing Number Game Program not Functioning Correctly

在我的程序中,用戶設置了一系列數字供計算機猜測。 然后,用戶必須以5開始的猜測來猜測計算機選擇了哪個數字。我的運行程序中有幾個問題,其中我不知道如何解決。 這些錯誤包括:

-剩余的猜測數始終保持為0。每次單擊btnCheck按鈕時,它都不會從5開始,而是減少1。

-每當我單擊btnCheck按鈕以獲取新的猜測數字時,如果您猜測過高或過低的語句都將保持不變。

-當我按btnNewGame時,將不會清除我在低值和高值文本輸入中插入的值。

-計算機如何根據我設置的數字范圍生成隨機整數?

將我的代碼修改為以下內容將不勝感激。

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

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

// Declare Global Variables
var computerGuess:String;   // the computer's guess
var Statement:String;   // Statement based on your outcome


// This is the checkGuess function
// e:MouseEvent is the click event experienced by the button
// void indicates that the function does not return a value
function checkGuess(e:MouseEvent):void
{  
    var LowValue:Number;    // the user's low value
    var HighValue:Number;    // the user's high value
    var UserGuess:Number;     // the user's guess
    var CorrectGuess:int;       // the correct number
    var FirstGuess:String; //the user's guess

    // get the user's range and guess
    LowValue = Number(txtinLow.text);
    HighValue = Number(txtinHigh.text);
    UserGuess = Number(txtinGuess.text);


    // determine the number of the user
    GuessesLeft = checkCorrectGuess(FirstGuess);
    lblNumber.text = GuessesLeft.toString();
    lblStatement.text = "You have guessed " + Statement.toString() + "\r";

  }

// This is function checkColoursCorrect
// g1– the user's guess
function checkCorrectGuess(g1:String):int
{
    var GuessesLeft:int = 5; // How many guesses are left

    if (g1 != computerGuess)
    {
       GuessesLeft - 1;
    }

    else
    {
        GuessesLeft = 0;
    }

    return GuessesLeft;
}


// This is the newGame function
// e:MouseEvent is the click event experienced by the button
// void indicates that the function does not return a value
function newGame(e:MouseEvent):void
{
    var Guess1:int;     // computer's guess in numbers
    var UserGuess1:int;     // user's guess in numbers
    Guess1 = randomWholeNumber(100,1); //It is not (100,1). How do I change this to the range the user put?
    UserGuess1 = randomWholeNumber(100,1); //It is not (100,1). How do I change this to the range the user put?


if (Guess1 > UserGuess1) {
    Statement = "TOO HIGH";
} else if (Guess1 < UserGuess1) {
    Statement = "TOO LOW";
} else if (Guess1 == UserGuess1) {
    Statement = "CORRECTLY";
} 

txtinGuess.text = "";
lblStatement.text = "";
}
// This is function randomWholeNumber
// highNumber – the maximum value desired
// lowNumber – the minimum value desired
// returns – a random whole number from highNumber to lowNumber inclusive
function randomWholeNumber(highNumber:int,lowNumber:int):int //How do I make a whole random number based on the range the user made?
{
    return Math.floor((highNumber - lowNumber + 1) * Math.random() + lowNumber);
}

要回答您的問題...

  1. 您已經在checkCorrectGuess()聲明了GuessesLeft ,這意味着它是一個局部變量,每次調用該函數時都會對其進行重新定義。 此外,因為您要傳入var FirstGuess:String; (未初始化的,未引用的string變量) (g1 != computerGuess)返回false,答案始終為0。
  2. GuessesLeft - 1; 不會將結果保存回變量。 您需要使用諸如GuessesLeft = GuessesLeft - 1類的賦值運算符,或者只需鍵入GuessesLeft-- ,即可。 您也可以編寫GuessesLeft -= 1 ,從左邊減去右邊,然后將值賦給左邊的變量。 請參閱AS3操作員...
  3. 您之前已經為這些TextFields分配了值; 只需在txtinLow.text = "" (與高相同newGame()重復newGame()內部的過程
  4. 使用您的變量。 您之前在checkGuess()中將它們定義為UserGuessLowValueHighValue

請注意,如果該代碼段可能在其他地方調用,則只需要將功能拆分為單獨的功能。 否則,堆棧上的每個函數都會導致更多的內存和性能下降。 checkCorrectGuess()屬於該類別,因此是不必要的。

另外,您將在newGame()函數而不是checkGuess()向用戶打印反饋。 好像是疏忽大意。

btnCheckGuess.addEventListener(MouseEvent.CLICK, checkGuess);
btnNewGame.addEventListener(MouseEvent.CLICK, newGame);

// Global Variables
var computerGuess:int;
var remainingGuesses:int;

newGame();

function newGame(e:MouseEvent):void {
    // Reset our guess limit
    remainingGuesses = 5;

    // Generate a new number
    computerGuess = random(int(txtinLow.text), int(txtinHigh.text));

    // Reset our readouts.
    txtinGuess.text = "";
    lblStatement.text = "";
}

function checkGuess(e:MouseEvent):void {
    var guess:int = int(txtinGuess.text);
    var msg:String;

    if (guess == computerGuess) { // Win
        remainingGuesses = 0; // Zero our count
        msg = "CORRECT";
    } else { // Missed
        remainingGuesses--; // Decrement our count

        if (guess > computerGuess) {
            msg = "TOO HIGH";
        } else if (guess < computerGuess) {
            msg = "TOO LOW";
        }
    }

    lblNumber.text = remainingGuesses.toString();
    lblStatement.text = "You have guessed " + msg;
}

function random(low:int, high:int):int {
    return Math.floor((high - low + 1) * Math.random() + low);
}

暫無
暫無

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

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