You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
-- 设置布局
self:setContentView(loadlayout{
LinearLayout,
orientation = "vertical",
{
EditText,
id = "@+id/guessEdit",
hint = "猜测总点数(6-36)",
layout_width = "match_parent",
layout_height = "wrap_content"
},
{
Button,
id = "@+id/rollButton",
text = "掷骰子",
layout_width = "wrap_content",
layout_height = "wrap_content",
onClick = "onRollButtonClick"
},
{
TextView,
id = "@+id/resultText",
text = "结果将显示在这里",
layout_width = "match_parent",
layout_height = "wrap_content"
}
})
-- 初始化骰子图像
self.diceImages = {}
for i = 1, 6 do
self.diceImages[i] = self.findViewById(self, "@+id/dice" .. i)
end
end
}
-- 掷骰子按钮点击事件
function onRollButtonClick(v)
local guess = tonumber(activity.findViewById(activity, "@+id/guessEdit").getText().toString())
if guess and guess >= 6 and guess <= 36 then
local total = 0
local results = {}
for i = 1, 6 do
local roll = math.random(1, 6)
total = total + roll
results[i] = roll
end
local resultText = activity.findViewById(activity, "@+id/resultText")
if total == guess then
resultText.setText("恭喜!你猜对了,总点数是 " .. total)
else
resultText.setText("很遗憾,总点数是 " .. total .. ",你的猜测是 " .. guess)
end
else
activity.findViewById(activity, "@+id/resultText").setText("请输入有效的点数(6-36)")
end
end
-- 应用入口点
function main()
local activity = DiceGameActivity()
activity.onCreate(nil)
end
-- 启动应用
main()
The text was updated successfully, but these errors were encountered:
require "import"
import "android.app.Activity"
import "android.os.Bundle"
import "android.widget.TextView"
import "android.widget.Button"
import "android.widget.EditText"
import "android.view.View"
import "android.widget.ImageView"
import "math"
-- 创建骰子游戏Activity类
DiceGameActivity = Activity:extend{
onCreate = function(self, savedInstanceState)
self.super.onCreate(self, savedInstanceState)
}
-- 掷骰子按钮点击事件
function onRollButtonClick(v)
local guess = tonumber(activity.findViewById(activity, "@+id/guessEdit").getText().toString())
if guess and guess >= 6 and guess <= 36 then
local total = 0
local results = {}
for i = 1, 6 do
local roll = math.random(1, 6)
total = total + roll
results[i] = roll
end
local resultText = activity.findViewById(activity, "@+id/resultText")
if total == guess then
resultText.setText("恭喜!你猜对了,总点数是 " .. total)
else
resultText.setText("很遗憾,总点数是 " .. total .. ",你的猜测是 " .. guess)
end
else
activity.findViewById(activity, "@+id/resultText").setText("请输入有效的点数(6-36)")
end
end
-- 应用入口点
function main()
local activity = DiceGameActivity()
activity.onCreate(nil)
end
-- 启动应用
main()
The text was updated successfully, but these errors were encountered: