假设您有三个字段。这是伪代码。将此委托功能附加到所有 3 个文本区域。
// Take 3 fields
FieldA, FieldB, FieldC
// Total target to reach
TotalTarget = 100
// The total we currently have
total = FieldA + FieldB + FieldC
// Work out how much we are under by.
// If positive, we are under. If negative, we are over.
underBy = TotalTarget - total
// Now two variables for the 'other two' fields.
if sender == FieldA:
other1 = FieldB, other2 = FieldC
if sender == FieldB:
other1 = FieldA, other2 = FieldC
if sender == FieldC:
other1 = FieldA, other2 = FieldB
// Split the difference and assign to other 2 fields so they are raised or lowered the same amount.
// If under, add, if over, subtract.
other1 = other1 + overBy / 2
other2 = other2 + overBy / 2
// This will add 1 or 0 to other2 (i.e. compensate for round-down)
other2 = other2 + overBy % 2;
如果您使用整数除法,那么您将遇到舍入问题。提示:如果它很奇怪,您可以在其中一个字段中添加一个。
确保编写测试。我没有尝试过任何这些,但它应该可以工作。