Chinaunix首页 | 论坛 | 博客
  • 博客访问: 322659
  • 博文数量: 69
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 759
  • 用 户 组: 普通用户
  • 注册时间: 2014-09-09 14:15
个人简介

〆 人生就是拼命地奔跑,和华丽的跌倒。 つ

文章分类

全部博文(69)

文章存档

2017年(2)

2016年(16)

2015年(21)

2014年(30)

分类: Java

2015-12-12 19:12:53

一:

代码:

点击(此处)折叠或打开

  1. Scanner sc = new Scanner(System.in);
  2.      System.out.println("请输入第1个数:");
  3.      double numOne = sc.nextDouble();
  4.      System.out.println("请输入第2个数:");
  5.      double numTwo = sc.nextDouble();
  6.      System.out.println("请输入:+. -. *. /");
  7.      String action = sc.next();
  8.      if(action.equals("+"))
  9.      {
  10.          System.out.println("结果为:"+(numOne + numTwo));
  11.      }
  12.      else if(action.equals("-"))
  13.      {
  14.          System.out.println("结果为:"+(numOne - numTwo));
  15.      }
  16.      else if(action.equals("*"))
  17.      {
  18.          System.out.println("结果为:"+(numOne * numTwo));
  19.      }
  20.      else if(action.equals("/"))
  21.      {
  22.          System.out.println("结果为:"+(numOne / numTwo));
  23.      }
  24.      else
  25.      {
  26.          System.out.println("运算有误!!");
  27.      }

运行结果:



二:

代码:


点击(此处)折叠或打开

  1. package Object;

  2. import java.util.Scanner;

  3. public class Calculator {
  4.     double num1;
  5.     double num2;
  6.     
  7.     public double add(double num1,double num2){
  8.         //double add = num1 + num2;
  9.         //return add;
  10.         return num1 + num2;
  11.     }
  12.     
  13.     public double sub(double num1,double num2){
  14.         //double sub = num1 - num2;
  15.         //return sub;
  16.         return num1 - num2;
  17.     }
  18.     
  19.     public double mul(double num1,double num2){
  20.         //double mul = num1 * num2;
  21.         //return mul;
  22.         return num1 * num2;
  23.     }
  24.     
  25.     public double div(double num1,double num2){
  26.         //double div = num1 / num2;
  27.         //return div;
  28.         return num1 / num2;
  29.     }
  30. }


点击(此处)折叠或打开

  1. package Object;

  2. import java.util.Scanner;

  3. public class CalculatorTest {

  4.     public static void main(String[] args) {
  5.         // TODO Auto-generated method stub
  6.         
  7.         Calculator reCalculator = new Calculator();
  8.         Scanner Input = new Scanner(System.in);
  9.         boolean flag=true; //布尔类型变量,只有true和false两个值
  10.         double result = 0.0;
  11.         String action="";
  12.         String answer="";
  13.         System.out.println("请输入第一个数:");
  14.         reCalculator.num1 = Input.nextDouble();
  15.         System.out.println("请输入运算符");
  16.         action = Input.next();
  17.         System.out.println("请输入第二个数:");
  18.         reCalculator.num2 = Input.nextDouble();
  19.         
  20.         if(action.equals("+")){
  21.             result = reCalculator.add(reCalculator.num1, reCalculator.num2);
  22.             //System.out.println("和为:" + reCalculator.add(num1,num2));
  23.         }else if(action.equals("-")){
  24.             result = reCalculator.sub(reCalculator.num1, reCalculator.num2);
  25.             //System.out.println("差为:" + reCalculator.sub(num1,num2));
  26.         }else if(action.equals("*")){
  27.             result = reCalculator.mul(reCalculator.num1, reCalculator.num2);
  28.             //System.out.println("积为:" + reCalculator.mul(num1,num2));
  29.         }else if(action.equals("/")){
  30.             result = reCalculator.div(reCalculator.num1, reCalculator.num2);
  31.             //System.out.println("商为:" + reCalculator.div(num1,num2));
  32.         }
  33.         
  34.         while(flag){
  35.             System.out.println("是否继续输入数!");
  36.             answer= Input.next();
  37.             if(answer.equals("yes")){
  38.                 System.out.println("请输入运算符!");
  39.                 action = Input.next();
  40.                 System.out.println("请输入运算数!");
  41.                 double num = Input.nextDouble();
  42.                 if(action.equals("+")){
  43.                     result = result + num;
  44.                 }else if(action.equals("-")){
  45.                     result = result - num;
  46.                 }else if(action.equals("*")){
  47.                     result = result * num;
  48.                 }else if(action.equals("/")){
  49.                     result = result / num;
  50.                 }
  51.                 flag = true;    
  52.             }else{
  53.                 flag = false;
  54.                 System.out.println("计算结果为:" + result);
  55.                 Input.close();
  56.             }
  57.         }
  58.     }
  59. }

运行结果:




阅读(1801) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~