我怎样才能在lua中获得最大数值? [英] how can i get a maximum number value in lua?

查看:77
本文介绍了我怎样才能在lua中获得最大数值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发应用程序以观察您的跑步速度,为此我需要一个功能来显示您的最大速度.但找不到我是怎么做的.

I am working on app to watch how fast you run, and for that I need a function that shows what your maximum speed has been. but can not find how I do.

local speedText = string.format( '%.3f', event.speed )
speed.y = 250
speed.x = 125
local numValue = tonumber(speedText)*3.6
if numValue ~= nil then
    speed.text = math.round( numValue )
end

我已将我的 speedText 设置为您在上面看到的数字.

I've made my speedText to a number that you see above.

我用 Conora SDK/Lua 编程

I program in Conora SDK/Lua

推荐答案

当您询问有关 Stack Overflow 的问题时,您应该提供更多信息,但无论如何我们都会尽力帮助您.

you should give more information when you ask questions on Stack Overflow, but let's try to help you anyway.

您的代码可能位于如下所示的事件侦听器中:

You code is probably inside an event listener that looks like that:

local listener = function(event)
  local speedText = string.format( '%.3f', event.speed )
  speed.y = 250
  speed.x = 125
  local numValue = tonumber(speedText)*3.6
  if numValue ~= nil then
      speed.text = math.round( numValue )
  end
end

这会显示当前速度.如果您想显示最大速度,只需执行以下操作:

This displays the current speed. If you want to display the maximum speed instead, just do something like this:

local maxSpeed = 0
local listener = function(event)
  local speedText = string.format( '%.3f', event.speed )
  speed.y = 250
  speed.x = 125
  local numValue = tonumber(speedText)*3.6 or 0
  if numValue > maxSpeed then
      maxSpeed = numValue
      speed.text = math.round( numValue )
  end
end

想法是:您需要在侦听器(或全局)之外定义一个变量来存储之前的最大速度.外部.每次调用事件监听器时,如果当前速度高于之前的最大速度,则为新的最大速度,因此您保存并显示它.

The idea is: you need a variable defined outside the listener (or a global) to store the previous maximum speed. Every time the event listener is called, if the current speed is higher than the previous maximum speed, then it is the new maximum speed so you save it and you display it.

这篇关于我怎样才能在lua中获得最大数值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆