《Android游戏开发详解》——第2章,第2.6节构建一个简单的计算器程序

    xiaoxiao2024-02-20  145

    本节书摘来自异步社区《Android游戏开发详解》一书中的第2章,第2.6节构建一个简单的计算器程序,作者 【美】Jonathan S. Harbour,更多章节内容可以访问云栖社区“异步社区”公众号查看

    2.6 构建一个简单的计算器程序Android游戏开发详解现在,我们已经尝到了甜头,让我们回过头来看看第1章介绍过的一些概念,并且构建一个简单的计算器程序。让我们给出一些动手实践的指导,来构建一个新的Java程序。请记住如下的主要步骤。

    ① 创建一个新的Java项目(将其命名为SecondProject)。

    ② 在src文件夹中创建一个新的类(将其命名为SimpleCalculator)。

    ③ 创建一个main方法。

    如果任何时候你碰到困难,应该参考前面的小节。一旦按照上面的步骤进行,应该会看到程序清单2.4所示的内容。

    程序清单2.4 SimpleCalcualtor类

    public class SimpleCalculator {     public static void main(String[] args) {     } }

    计算器应用程序背后的思路很简单。我们将创建两个float变量,表示两个运算数。我们将创建第3个变量来表示想要执行的计算。

    我们将使用一个整数来表示计算,规则如下。

    1:加法。

    2:减法。

    3:乘法。

    4:除法。

    源代码将检查3个变量中的值,并且使用它们来产生所请求的算术计算的结果。给类SimpleCalculator添加如下代码。新的代码如程序清单2.5的第4行到第31行所示。

    程序清单2.5 带有逻辑的SimpleCalcualtor类

    01 public class SimpleCalculator { 02   03  public static void main(String[] args) { 04    float operand1 = 5; 05    float operand2 = 10; 06    int operation = 1; 07 08    if (operation == 1) { 09   10       // Addition 11       System.out.println(operand1 + " + " + operand2 + " ="); 12       System.out.println(operand1 + operand2); 13 14    } else if (operation == 2) { 15 16       // Subtraction 17       System.out.println(operand1 + " - " + operand2 + " ="); 18       System.out.println(operand1 - operand2); 19 20    } else if (operation == 3) { 21 22       // Multiplication 23       System.out.println(operand1 + " * " + operand2 + " ="); 24       System.out.println(operand1 * operand2); 25 26    } else { 27 28       // Division 29       System.out.println(operand1 + " / " + operand2 + " ="); 30       System.out.println(operand1 / operand2); 31    } 32 33  } 34 } 运行该程序!应该会得到如下所示的输出。 5.0 + 10.0 = 15.0

    花时间看一下这段代码。确保可以一行一行地浏览代码,并搞清楚发生了什么。

    我们首先是声明了两个新的float变量,名为operand1和operand2,并使用值5和10来初始化它们。我们声明的第3个变量名为operation,将值1赋给了它。

    然后,是一系列的if语句,它们测试operation变量的值并确定执行正确的计算。当一条if语句满足的时候,执行两条System.out.println()语句,打印出了我们所看到的结果。注意,在这里我们使用加法运算,将两个带有float值的字符串连接(组合)起来。

    那么,如果我们想要计算25×17的值的话,该如何修改呢?我们直接将operand1的值改为25,将operand2的值改为17,将operation的值改为3,如程序清单2.6所示。

    程序清单2.6 修改后的SimpleCalcualtor类

    public class SimpleCalculator {     public static void main(String[] args) {         float operand1 = 5;         float operand2 = 10;         int operation = 1;         float operand1 = 25;         float operand2 = 17;         int operation = 3;         if (operation == 1) {             // Addition             System.out.println(operand1 + " + " + operand2 + " =");             System.out.println(operand1 + operand2);         } else if (operation == 2) {             // Subtraction             System.out.println(operand1 + " - " + operand2 + " =");             System.out.println(operand1 - operand2);         } else if (operation == 3) {              // Multiplication              System.out.println(operand1 + " * " + operand2 + " =");              System.out.println(operand1 * operand2);         } else {              // Division              System.out.println(operand1 + " / " + operand2 + " =");              System.out.println(operand1 / operand2);         }     } }

    再次运行该程序,应该看到如下结果。

    25.0 * 17.0 = 425.0

    SimpleCalculator现在还不是非常有用。每次要执行一个简单的计算的时候,它都要求我们修改代码。最好的解决方案是,要求程序的用户为我们的operand1、operand2和operation提供想要的值。实际上,Java提供了一种方法可以做到这一点,但是,需要我们先理解如何使用对象,因此,我们现在先不讨论这种方法。

    最新回复(0)