Java是现在很受欢迎的编程语言,很多企业开发项目都会使用java,java能够应用在各个领域,利用java还能开发出我们想要的项目,那用java实现加减乘除运算怎么弄?下面来我们就来给大家讲解一下。
1、思路
(1)实现四个方法,分别实现加减乘除四个功能。(一个方法实现一个功能,原子性);
(2)用scanner实现键盘交互(这里我们实现的是两个整数之间的加减乘除,用nextInt()函数获取整数,用next().charAt(0)获取我们输入的第一个字符);
(3)用switch进行+、-、*、/的判断选择,调用相应的方法实现两个整数间的加减乘除运算。
2、Java代码
package com.yy.method; import java.util.Scanner; public class calculate { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("请输入:"); //3 * 5 int x = scanner.nextInt(); char c = scanner.next() .charAt(0); //读入控制台输入的第一个字符 int y = scanner.nextInt(); switch (c) { case '+': sum(x, y); break; case '-': sub(x, y); break; case '*': mul(x, y); break; //15 case '/': div(x, y); break; } } public static void sum(int a, int b) { System.out.println(a + b); } public static void sub(int a, int b) { System.out.println(a - b); } public static void mul(int a, int b) { System.out.println(a * b); } public static void div(int a, int b) { System.out.println(a / b); } }
Java如何实现实现一个简单计算器?
先来看看界面效果:
源码如下:
package test1; import java.awt.Frame; import java.awt.TextField; import java.awt.Button; import java.awt.Panel; import java.awt.GridLayout; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class calculate { TextField tfResult; Button btNum[], btOper[], btPoint, btEqual, btBack, btSqrt, btPingfang, btLifang; int num1, num2; //操作数 char oper; //操作符 boolean isNum; //代表数字标志位 public void init() { Frame f; tfResult = new TextField(); btNum = new Button[10]; //数组对象 for (int i = 0; i <= 9; i++) { btNum[i] = new Button(i + ""); } btOper = new Button[4]; btOper[0] = new Button("+"); btOper[1] = new Button("-"); btOper[2] = new Button("*"); btOper[3] = new Button("/"); btPoint = new Button("."); btEqual = new Button("="); btBack = new Button("Backspace"); btSqrt = new Button("Sqrt"); btPingfang = new Button("2"); btLifang = new Button("3"); f = new Frame("小羊羔的计算器!"); f.setSize(300, 300); //放置组件 f.add(tfResult, "North"); Panel panel = new Panel(); f.add(panel, "Center"); GridLayout gl = new GridLayout(5, 4); panel.setLayout(gl); panel.add(btNum[1]); panel.add(btNum[2]); panel.add(btNum[3]); panel.add(btOper[0]); panel.add(btNum[4]); panel.add(btNum[5]); panel.add(btNum[6]); panel.add(btOper[1]); panel.add(btNum[7]); panel.add(btNum[8]); panel.add(btNum[9]); panel.add(btOper[2]); panel.add(btNum[0]); panel.add(btPoint); panel.add(btEqual); panel.add(btOper[3]); panel.add(btBack); panel.add(btSqrt); panel.add(btPingfang); panel.add(btLifang); f.setVisible(true); //注册监听 f.addWindowListener(new MyWinLis()); //数字按钮注册监听 MyNumLis mnl = new MyNumLis(); for (int i = 0; i <= 9; i++) btNum[i].addActionListener(mnl); //操作符按钮注册监听 MyOperLis mol = new MyOperLis(); for (int j = 0; j <= 3; j++) btOper[j].addActionListener(mol); //等号按钮注册监听 MyEqualLis mel = new MyEqualLis(); btEqual.addActionListener(mel); //删除按钮注册监听 MyBackLis mbl = new MyBackLis(); btBack.addActionListener(mbl); //开根号按钮注册监听 MySqrtLis msl = new MySqrtLis(); btSqrt.addActionListener(msl); //平方按钮注册监听 MyPingfangLis mpl = new MyPingfangLis(); btPingfang.addActionListener(mpl); //百分号注册监听 MyLifangLis mll = new MyLifangLis(); btLifang.addActionListener(mll); } //定义监听器类 class MyNumLis implements ActionListener { public void actionPerformed(ActionEvent e) { String t = e.getActionCommand(); String s = tfResult.getText(); if (isNum == false) tfResult.setText(t); else tfResult.setText(s + t); isNum = true; } } class MySqrtLis implements ActionListener { public void actionPerformed(ActionEvent e) { double t = Double.parseDouble(tfResult.getText()); tfResult.setText(Math.sqrt(t) + ""); isNum = false; } } class MyPingfangLis implements ActionListener { public void actionPerformed(ActionEvent e) { num1 = Integer.parseInt(tfResult.getText()); tfResult.setText(num1 * num1 + ""); } } class MyLifangLis implements ActionListener { public void actionPerformed(ActionEvent e) { num1 = Integer.parseInt(tfResult.getText()); tfResult.setText(num1 * num1 * num1 + ""); } } class MyOperLis implements ActionListener { public void actionPerformed(ActionEvent e) { num1 = Integer.parseInt(tfResult.getText()); //转化为INT oper = e.getActionCommand() .charAt(0); isNum = false; } } class MyEqualLis implements ActionListener { public void actionPerformed(ActionEvent e) { int result = 0; isNum = false; num2 = Integer.parseInt(tfResult.getText()); switch (oper) { case '+': result = num1 + num2; break; case '-': result = num1 - num2; break; case '*': result = num1 * num2; break; case '/': result = num1 / num2; break; } tfResult.setText(result + ""); } } class MyBackLis implements ActionListener { public void actionPerformed(ActionEvent e) { tfResult.setText(""); isNum = false; } } class MyWinLis extends WindowAdapter { public void windowClosing(WindowEvent e) { e.getWindow() .setVisible(false); e.getWindow() .dispose(); System.exit(0); } } public static void main(String[] args) { // TODO Auto-generated method stub new calculate() .init(); } }
11×5=?界面如下:
一个简单计算器就这样利用java开发出来了,其实java开发项目很简单,只要我们理清逻辑,写好代码,一般不会有难度!最后大家如果想要了解更多java实例知识,敬请关注奇Q工具网。
推荐阅读: