---
# System prepended metadata

title: 'マイクラ検定 問題4 答え '
tags: [マイクラ検定答え]

---


    os.loadAPI("h2touchpanel")

    local num1 = 0
    local num2 = 0
    local a = 0
    local answer = 0
    local count = 0

    function input(num)
        if count == 0 then
            num1 = num1 * 10 + num
            print(num1)
        else
            num2 = num2 * 10 + num
            print(num2)
        end
    end

    function add()
        a = 1
        count = count + 1
        print("+")
    end

    function sub()
        a = 2
        count = count + 1
        print("-")
    end

    function multi()
        a = 3
        count = count + 1
        print("*")
    end

    function div()
        a = 4
        count = count + 1
        print("/")
    end

    function enter()
        if a == 1 then
            answer = num1 + num2

        elseif a == 2 then
            answer = num1 - num2

        elseif a == 3 then
            answer = num1 * num2

        else
            answer = num1 / num2 

        end
        print("answer is ", answer)

        num1 = 0
        num2 = 0
        a = 0
        answer = 0
        count = 0


    end

    local buttons = {}

    buttons[1] = { name = "1", cmd = function() input(1) end }
    buttons[2] = { name = "2", cmd = function() input(2) end }
    buttons[3] = { name = "3", cmd = function() input(3) end }
    buttons[4] = { name = "4", cmd = function() input(4) end }
    buttons[5] = { name = "5", cmd = function() input(5) end }
    buttons[6] = { name = "6", cmd = function() input(6) end }
    buttons[7] = { name = "7", cmd = function() input(7) end }
    buttons[8] = { name = "8", cmd = function() input(8) end }
    buttons[9] = { name = "9", cmd = function() input(9) end }
    buttons[10] = { name = "0", cmd = function() input(0) end }
    buttons[11] = { name = "+", cmd = function() add() end }
    buttons[12] = { name = "-", cmd = function() sub() end }
    buttons[13] = { name = "*", cmd = function() multi() end }
    buttons[14] = { name = "/", cmd = function() div() end }
    buttons[15] = { name = "=", cmd = function() enter() end }

    local monitor = peripheral.wrap("top")
    local option = { topPos = 1, bottomPos = 9}
    local panel = h2touchpanel.makePanel(buttons, 3, 5, monitor, option)
    panel:draw()
    while true do
        local event, btn = panel:pullButtonPushEvent()
        btn:run()
        panel:drawPushedButtonEffect(btn, 0.5)
    end