猜数字游戏程序无法正常运行

Posted

技术标签:

【中文标题】猜数字游戏程序无法正常运行【英文标题】:Guessing Number Game Program not Functioning Correctly 【发布时间】:2018-01-01 03:50:19 【问题描述】:

在我的程序中,用户设置了一系列数字供计算机猜测。然后用户必须猜测计算机选择了哪个数字,猜测的限制从 5 开始。在我的正常运行的程序中有几个问题,我不知道如何解决。这些错误包括:

-剩下的猜测次数始终保持为0。它不会从5开始,每次点击btnCheck按钮时减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。 GuessesLeft - 1; 没有将结果保存回变量。您需要使用赋值运算符,例如GuessesLeft = GuessesLeft - 1,或者如果您只想递减,则只需键入GuessesLeft--。你也可以写GuessesLeft -= 1,它从左边减去右边,然后将值赋给左边的变量。 See AS3 Operators... 您之前已经为这些TextFields 赋值;只需使用txtinLow.text = "" 重复newGame() 内部的过程(与高相同) 使用您的变量。您之前在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);

【讨论】:

以上是关于猜数字游戏程序无法正常运行的主要内容,如果未能解决你的问题,请参考以下文章

实验任务五:猜数字游戏

用Java编程这个猜数字游戏

猜数字游戏(Tkinter)

Qt5小Demo之猜数字游戏

java之猜数字游戏

猜数字游戏c语言编程一到五?