package com.sw.dualscreen class Test { private var lastWeight = 0 // private var currentWeight = 0 data class WeightRecord( var eatWeight: Int = 0, var deviceWeight: Int = 0, var lastWeight: Int = 0, var state: Boolean = false ) private val weightRecord by lazy { WeightRecord() } fun main() { readWeight { weight -> if (weightRecord.state) { //数据已记录 return@readWeight } if (weight == 0 || lastWeight == 0) { return@readWeight } if (weight < lastWeight) { weightRecord.let { it.deviceWeight = weight it.eatWeight = it.deviceWeight - it.lastWeight it.state = true } } lastWeight = weight } } fun readWeight(block: (Int) -> Unit) { } }