java - 没有实际使用任何条件语句的条件?

标签 java math

我正在学习入门级 Java 类(class),我们的第一份作业让我难住了,主要是因为要求使它变得非常不方便。

The output y shall be equal to two times the value of the input x if such input is an integer number between 0 and 4, inclusive.

The output y shall be equal to the value of the input x if such input is an integer number between 5 and 9, inclusive.

The output y shall be equal to 0 if the input x is an integer number between 10 and 14, inclusive.

The code MUST NOT make use of "if" statements, "switch-case" statements, "for" loops, "while" loops, Boolean variables, or Boolean expressions

如果不实际使用条件语句,我该如何做到这一点?

最佳答案

您可以使用一个简单的数学方程:

int y = x * (2 - (x / 5));

关于java - 没有实际使用任何条件语句的条件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46309804/

相关文章:

algorithm - 在图形轴上自动选择对数和线性刻度

java - 更新库的 Gradle 同步项目

java - 无法在不删除前一个元素的情况下在 Arraylist 中添加 HashMap

java - 模拟使用外部类的方法,mockito

java - 使用 JDBC 时出现 MissingResourceException?

c++ - std::discrete_distribution 的意外行为

java - Android 应用程序的自定义日志记录

math - 如何在Mathematica中获得准确的绘图曲线?

algorithm - 检索二维图上最外面的点

c# - 如何在 C# 中使用教科书 RSA 加密 ASCII 字符?