zoukankan      html  css  js  c++  java
  • Lesson_5 作业_1 简单计算器模拟

    一、作业描述 

      编写Java程序,模拟简单的计算器。

      定义名为Number的类,其中有两个整型数据成员n1和n2,应声明为私有。编写构造方法,赋予n1和n2初始值,再为该类定义加(addition)、减(subtraction)、乘(multiplication)、除(division)等公有成员方法,分别对两个成员变量执行加、减、乘、除的运算。

      在main方法中创建Number类的对象,调用各个方法,并显示计算结果。

    二、代码

    // Lesson_5 2013-01-15

     

     1 /***********************************************************
     2 *
     3 *                     简单计算器模拟
     4 *                      2013-01-16
     5 *                     by CocoonFan
     6 *
     7 ***********************************************************/
     8 
     9 
    10 import java.util.Scanner;
    11 
    12 public class Calculator{
    13     public static void main(String []args){
    14         while(true){
    15             System.out.println("请输入两个个操作数 (输入0 0结束):");
    16             Scanner sc_a = new Scanner(System.in);
    17             Scanner sc_b = new Scanner(System.in);
    18 
    19             int a = sc_a.nextInt();
    20             int b = sc_a.nextInt();
    21 
    22             if(a ==0 && b==0) break;
    23 
    24             Number calc = new Number(a, b);
    25             calc.addition();
    26             calc.subtraction();
    27             calc.multiplication();
    28             calc.division();
    29         }
    30         
    31     }
    32 }
    33 
    34 class Number{
    35     private int n1;
    36     private int n2;
    37 
    38     //构造方法,设置初始值
    39     public Number(){
    40         n1 = 1;
    41         n2 = 1;
    42     }
    43     public Number(int n1, int n2){
    44         this.n1 = n1;
    45         this.n2 = n2;
    46     }
    47 
    48     //加法
    49     public int addition(){
    50         System.out.println(n1 + " + " + n2 + " = " + (n1 + n2));
    51         return n1 + n2;
    52     }
    53 
    54     //减法
    55     public int subtraction(){
    56         System.out.println(n1 + " - " + n2 + " = " + (n1 - n2));
    57         return n1 - n2;
    58     }
    59 
    60     //乘法
    61     public int multiplication(){
    62         System.out.println(n1 + " x " + n2 + " = " + (n1 * n2));
    63         return n1 * n2;
    64     }
    65 
    66     //除法
    67     public double division(){
    68         if(n2 == 0){
    69             System.out.println("执行除法的时候出错了!--> 被除数为0\n");
    70             return 0.0;
    71         }else{
    72             System.out.println(n1 + " / " + n2 + " = " + ((n1*1.0) / n2) + "\n");
    73         }
    74         return ((n1*1.0)/n2);
    75     }
    76 }

     老师写的

     1 /****************************************************************
     2 *                           作业解答
     3 *                    题目一:简单计算器模拟
     4 *                      note by CocoonFan
     5 *                          2013-01-16
     6 ****************************************************************/
     7 
     8 /* -------------------------复习--------------------------------
     9     [访问修饰符] class 类名{
    10         1.属性-特征(变量)
    11         [访问修饰符] 数据类型 变量名(成员变量) [ = 初始值];
    12 
    13         2.方法-功能(作用)
    14         [访问修饰符] 方法返回 void|数据类型 方法名([数据类型 参数1,···]){
    15             当方法返回某个数据类型的时候,方法体中必须要return相应的值。
    16         }
    17 
    18         3.构造方法:方法名与类名一致,且没有返回类型。
    19     }
    20 
    21 ----------------------------------------------------------------*/
    22 
    23 public class Ans_Calculator{
    24     public static void main(String []args){
    25         Number number = new Number(12, 38);
    26         System.out.println("加法" + number.add());
    27         System.out.println("减法" + number.sub());
    28         System.out.println("乘法" + number.mul());
    29         System.out.println("除法" + number.div());
    30     }
    31 }
    32 
    33 class Number{
    34     private int n1;//全局变量
    35     private int n2;
    36 
    37     //参数为局部变量,当全局变量和局部变量同名的时候局部变量会隐藏全局变量
    38     public Number(int n1, int n2){
    39         //如果想要访问到同名的成员变量,则需要使用this关键字
    40         this.n1 = n1;
    41         this.n2 = n2;
    42     }
    43 
    44     public int add(){
    45         return n1 + n2;
    46     }
    47 
    48     public int sub(){
    49         return this.n1 - this.n2;//加不加this效果一样
    50     }
    51 
    52     public int mul(){
    53         return n1*n2;
    54     }
    55 
    56     public int div(){
    57         if(n2 != 0){
    58             return n1/n2;
    59         }else{
    60             return -1;
    61         }
    62     }
    63 }

    三、运行结果

      

  • 相关阅读:
    MySQL之----在java编程加强知识点
    走进windows编程的世界-----入门篇
    对Java、C#转学swift的提醒:学习swift首先要突破心理障碍。
    LeetCode--N-Queens
    美国人与欧洲人为什么都吃转基因食品?
    hdu1290
    OS 中文斜体 Italic Font Chinese
    为什么那些每三年跳一次槽的人越跳越好?
    [博弈] hdu 3683 Gomoku
    HDU 5358 First One(枚举)
  • 原文地址:https://www.cnblogs.com/CocoonFan/p/2862110.html
Copyright © 2011-2022 走看看