Java代码问题: Capping an amount

标签 java if-statement

我是 java 编程的初学者,并且在弄清楚如何限制代码中的金额时遇到问题。

我的代码有一个问题,该代码由 if-else if 语句组成,这更多的是如何限制奖金最高金额的问题。我希望我的奖金最高金额为 300 美元。我不希望任何参赛者收到比这更多的东西。确定这一点的方程是

奖品=(60分钟4)*10/2;

这根据参赛者的时间决定他们收到的现金数额。问题是如果超过300美元,我希望能够让系统自动将其退回到300美元。我想问我可以使用什么方法来做到这一点,或者是否有办法限制金额?

最佳答案

正如评论所暗示的,您有两种常见的途径:使用数学实用程序,或使用 IF-THEN-ELSE 测试。

if-then-else

使用 if-then-else 的示例。

int input = … ;  
int calculation = runMyCalculation( input ) ; 
int result ;
if( calculation > 300 ) {  // If over the cap, override calculated amount to use the maximum, the cap amount of 300. 
    result = 300 ;  // Limit specified by Marie Leclerc on 2018-01-23 in email, and verified in conversation. --Basil Bourque
} else {  // Else not over the cap, so use result of calculation.
    result = calculation ;
}

注意评论。实际上,我确实在所有生产代码中都写了这样的注释,用自然的人类语言(对我来说是英语)阐明了 if-then-else 语句背后的逻辑和业务规则。写下这些评论澄清了我目前的想法,并有助于验证我的逻辑是否正确。这些注释对于审查我的代码的人来说也是一样的。稍后,这些评论在调试时非常宝贵,尤其是在半夜解决一些紧急修复问题时。最终它们成为继承该代码的程序员的重要文档。

更简短地将初始计算分配给结果

int input = … ;
int calculation = runMyCalculation( input ) ; // Run your calculation code.
int result = calculation ;
if( result >= 300 ) {
    result = 300 ;  // Override the first assigned value with the cap amount, 300.
} 

如果使用更简短的版本,我们实际上并不需要两个单独的变量。

int input = … ;
int result = runMyCalculation( input ) ; // Run your calculation code.
if( result >= 300 ) {
    result = 300 ;  // Override the calculated value with the cap amount, 300.
}

最好将上限金额声明为命名 constant而不是分散 “magic” number关于。

static final int CAP_AMOUNT = 300 ;  // Use `static final` if this number is fixed at runtime, never varying. 
…
int input = … ;
int result = runMyCalculation( input ) ; // Run your calculation code.
if( result >= myClass.CAP_AMOUNT ) {
    result = myClass.CAP_AMOUNT ;  // Override the calculated value with the cap.
}

您可以使用 ternary conditional operator 进一步缩短此时间,if-then-else 语句的简写)。三元运算符是完全可选的。仅在可以使代码更易读的地方使用,而不是更少。在一些传统的编译语言中,三元可能会快一点点,但我希望任何具有 runtime optimizer 的 JVM无论使用三元还是不使用 if-then-else,速度都会一样快。

static final int CAP_AMOUNT = 300 ;  // Use `static final` if this number is fixed at runtime, never varying. 
…
int input = … ;
int result = … ; // Run your calculation code.
result = ( result > myClass.CAP_AMOUNT ) ? myClass.CAP_AMOUNT : result ;  // Shorthand version of if-then-else. 

数学实用程序类

Math类提供了方便的实用程序。其中之一是 min 方法,您可以在其中传递一对数字。您将返回较小的那个。

int result = Math.min( runMyCalculation( input ) , myClass.CAP_AMOUNT ) ;  // Return whichever is smaller.

就我个人而言,我会采用这种方法。这段代码很简短,但仍然清楚地表达了我的意图。

但我会单独分解对计算的调用。拥有单独的计算变量有助于debugginglogging .

int calculation = runMyCalculation( input ) ;  // Run your calculation code.
int result = Math.min( calculation , myClass.CAP_AMOUNT ) ;  // Return whichever is smaller.

验证输入

最后,您可以验证输入,拒绝某些值来控制输出。但我怀疑这不是你问题的主旨。

关于Java代码问题: Capping an amount,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49345113/

相关文章:

C 中的复合条件 : if (0. 0 < a < 1.0)

java - 检测 if 和 else 语句执行了多长时间

javascript - 随机访客看到图像

jquery - 使用 JSON 时 jQuery 中 if 语句的说明

PHP While 语句显示与查询结果相同的结果

java - Java 中的拆分按钮

java - 启动运行命令时出现问题

java - 我可以从静态方法调用非静态方法吗?

java - 使用 Volley 和 Gson : Parse item and items list

java - 仅在 charAt() 和 isSpaceChar() 的帮助下使 java 中的首字母大写