如果/ else语句Lua

时间:2016-08-06 02:19:02

标签: if-statement lua corona

每次生成算术问题时,每当我选择正确或错误的答案时,函数checkanswer()的语句总是在if语句“Correct”上继续。我如何根据算术问题正确创建if / else语句varanswer = var1 + var2

 function checkAnswer(event)

if(#theAnswer == questionGen()) then
        instructionsText.text = "Correct!";
        instructionsText:toFront()
        generateBalls()
        for i=1, allBallsGroup.numChildren do
            display.remove(allBallsGroup[1])
        end
    else
        instructionsText.text = "Incorrect!";
        instructionsText:toFront()
        generateBalls()
        for i=1, allBallsGroup.numChildren do
            display.remove(allBallsGroup[1])
        end
    end
end


function questionGen()

 local questionVar1 = display.newImage("ygSquare.png", 150, 500); 
 local var1 = math.random(1,9)
 local var1Display =display.newText(var1, 200, 500, native.systemFont, 200)
 questionVar1.width = 200
 questionVar1.height = 200
 questionVar1.x = 350
 questionVar1.y = 500
 var1Display.x = 350
 var1Display.y = 500
 var1Display:setTextColor("#000000")
 local questionVar2 = display.newImage("blueSquare.png", 150, 500);
 local var2 = math.random(1,9)
 local var2Display = display.newText(var2, 200, 500, native.systemFont, 200)
 questionVar2.width = 200
 questionVar2.height = 200
 questionVar2.x = 700
 questionVar2.y = 500
 var2Display.x = 700
 var2Display.y = 500
 var2Display:setTextColor("#000000")
 local operator = "         +     " 
 local operatorDisplay = display.newText(operator, 400, 500, native.systemFont, 200)
 operatorDisplay:setTextColor("#000000")
 local varAnswer = var1 + var2
 return varAnswer

end

2 个答案:

答案 0 :(得分:0)

您的随机值大于1,并且添加两个大于1的数字将大于1.这意味着questionGen()始终返回true。我会将var1var2除以9,以便将其标准化。此外,您还需要添加一行来检查varAnswer是否大于1(0.5 + 05)返回true,否则返回false。

function questionGen()

 local varAnswer = var1/9 + var2/9
 if(varAnswer>=1) then return true end
 return false
end

现在,在您的其他功能中,您可以查看if(questionGen()) then

答案 1 :(得分:0)

这用于在我的游戏宏中创建输入或选择

local theAnswer= ""; 
local theAnswerText;

function createBall()

local var numberArray = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "11", "12", "13", "14", "15", "16", "17", "18"};
local tens = {questionGen(#theAnswer)}
local ballType = math.random(8);
local ballSize = math.random(2)
local letterIndex
local letter
if(createTens == true) then

   letterIndex = math.random(#tens)
   letter = tens[letterIndex];
else
 letterIndex = math.random(#numberArray);
 letter = numberArray[letterIndex];
  letterIndex = math.random(#tens)
   letter = tens[letterIndex];
end

local ballGroup = display.newGroup();
local ball
local ballRadius
if(ballType == 1) then
     ball = display.newImage("whiteBall.png"); 
elseif(ballType == 2) then
     ball = display.newImage("brownBall.png");
elseif(ballType == 3) then
     ball = display.newImage("pinkBall.png");
elseif(ballType == 4) then
     ball = display.newImage("whiteBall.png");
elseif(ballType == 5) then
     ball = display.newImage("yellowBall.png");
elseif(ballType == 6) then
     ball = display.newImage("whiteBall.png");
elseif(ballType == 7) then
     ball = display.newImage("greenBall.png");
else
     ball = display.newImage("redBall.png");
end
if(ballSize == 1)then
    ball.width  = 200;
    ball.height = 200;
    ballRadius = 30;
else
    ball.width = 200;
    ball.height = 200;
    ballRadius = 30;    
end

local letterText = display.newText( letter, 0,0, native.systemFontBold, 100 );
letterText:setTextColor(0,0, 0);
letterText.x = ball.x;
letterText.y = ball.y;
ballGroup:insert(ball);
ballGroup:insert(letterText);
ballGroup.x = math.random(ballRadius,display.contentWidth-ballRadius*3);
ballGroup.y= 40;
physics.addBody(ballGroup, 'dynamic',{friction = 0,bounce = 0,radius = ballRadius*3});
ballGroup.name = "ball";
ballGroup.letter = letter;

ballGroup:addEventListener('tap',checkAnswer);
table.insert(allBalls,ballGroup)
allBallsGroup:insert(ballGroup)