java编程,用代码实现计算器类(Calculator)
这个不难,在calculator类里面写四个方法,传两个参数,分别实现加减乘除,返回结果,在测试类里面初始化calculator类,分别调用这个四个方法就OK了
package calculate;import java.math.BigDecimal;import java.math.BigInteger;import java.util.regex.Matcher;import java.util.regex.Pattern;public class Calculator { public static void main(String[] args) { System.out.println(calculate("x=1+2+3+4,y=1*2*3*4")); System.out.println(new Calculator("2*2*3.1415926")); System.out.println(new Calculator("(10+10)*10-10/10")); System.out.println(new Calculator("2^10")); System.out.println(new Calculator("10!")); System.out.println(new Calculator("(2*(3+2))!")); } public static String calculate(String exp) { return new Calculator(exp).calculate(); } private static String regex_num = "(\\d+(\\.\\d+)?)"; private String exp; private String result; public Calculator(String exp) { this.exp = exp; this.result = exp.replaceAll("\\s+", ""); } public String calculate() { calculateSub(); calcuateFactorial(); calcuate("\\^", Handler.Power); calcuate("\\*", Handler.Multiply); calcuate("/", Handler.Divide); calcuate("\\+", Handler.Add); calcuate("\\-", Handler.Subtract); return result; } private void calculateSub() { Pattern pattern = Pattern.compile("\\(([^\\(\\)]+)\\)"); Matcher matcher = pattern.matcher(result); while (matcher.reset(result).find()) { updateResult(matcher, calculate(matcher.group(1))); } } private void calcuateFactorial() { Pattern pattern = Pattern.compile(regex_num + "!"); Matcher matcher = pattern.matcher(result); while (matcher.reset(result).find()) { BigDecimal decimal = new BigDecimal(result.substring(0, result.length() - 1)); if (decimal.toString().indexOf(".") > -1) { throw new RuntimeException("when a!, a must is a integer"); } updateResult(matcher, factorial(decimal.intValue())); } } public static BigInteger factorial(int num) { if (num -1) { throw new RuntimeException("when a ^ b, b must is a integer"); } return decimal.pow(decimal2.intValue()); } }; }}刚才的用到了一些工具类,现在把它去掉
public class Calculator {private int number1;
private int number2;
public Calculator(int number1, int number2) {
this.number1 = number1;
this.number2 = number2;
}
public double add() {
return number1 + number2;
}
public double subtract() {
return number1 - number2;
}
public double multiple() {
return number1 * number2;
}
public double divide() {
if(number2 == 0) {
return 0;
}
return number1 / number2;
}
}
public class TestCalculator {
public static void main(String[] args) {
Calculator calculator = new Calculator(5, 2);
System.out.println(calculator.add());
System.out.println(calculator.subtract());
System.out.println(calculator.multiple());
System.out.println(calculator.divide());
}
}
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class TestC {
public static void main(String[] args) {
IFrame frame = new IFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}
class IFrame extends JFrame {
public static final int width = 200;
public static final int height = 200;
public IFrame() {
setTitle("Welcome");
setSize(width, height);
IPanels panel = new IPanels();
Container ContentPane = getContentPane();
ContentPane.add(panel);
}
}
class IPanels extends JPanel implements ActionListener {
JLabel res;
JTextField num1;
JTextField num2;
JButton addbtn;
JButton subductionbtn;
JButton multiplybtn;
JButton divisionbtn;
public IPanels() {
setLayout(new FlowLayout(FlowLayout.LEFT));
res = new JLabel("结果: 0");
JLabel la1 = new JLabel("输入整数a:");
JLabel la2 = new JLabel("输入整数b:");
addbtn = new JButton("+");
subductionbtn = new JButton("—");
multiplybtn = new JButton("*");
divisionbtn = new JButton("/");
num1 = new JTextField(10);
num2 = new JTextField(10);
add(la1);
add(num1);
add(la2);
add(num2);
add(addbtn);
add(subductionbtn);add(multiplybtn);add(divisionbtn);
add(res);
addbtn.addActionListener(this);
subductionbtn.addActionListener(this);
multiplybtn.addActionListener(this);
divisionbtn.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==addbtn){
res.setText(String.valueOf(Integer.parseInt(num1.getText())+Integer.parseInt(num2.getText())));
}else if(e.getSource()==subductionbtn){
res.setText(String.valueOf(Integer.parseInt(num1.getText())-Integer.parseInt(num2.getText())));
}else if(e.getSource()==multiplybtn){
res.setText(String.valueOf(Integer.parseInt(num1.getText())*Integer.parseInt(num2.getText())));
}else if(e.getSource()==divisionbtn){
res.setText(String.valueOf(Integer.parseInt(num1.getText())/Integer.parseInt(num2.getText())));
}
}
}
package ch01;
public class CalcTest {
public static float number1,number2;
public static void main(String[] args) {
number1=10;
number2=15;
System.out.println(add());
System.out.println(subtract());
System.out.println(multiple());
System.out.println(divide());
}
public static float add(){
return number1+number2;
}
public static float subtract(){
return number1-number2;
}
public static float multiple(){
return number1*number2;
}
public static float divide(){
if(number2==0)
return -1;
return number1/number2;
}
}
你会不会啊?
名字怎么弄啊脱突
易语言如何自动获取AVA的游戏路径
如果从中找到带有该路径的但又有其他信息的可以用易语言中的“子文本替换”把里面多余的字弄掉。AVA这个游戏我没有,例如QQ路径:取文本注册项 (3, “Software\\Classes\\Tencent\\URL Protocol”)‘去完后发现有多余的“\\Bin\\Timwp.exe”接着用子文本替换这个程序来删除这个内容,总体取QQ路径的代码:...
ava我用了12个媒体特权代码为什么只给了11个
现在是14个了 DZM06 DR8S303FM DR08ZHBJD QK0930220 QK096020W JY008MLDY JYF15 DNB15 YXB15 JYD15 YXY15 DG08DJBFM DNG15 JYF13
java.lang.ClassCastException怎么解决?
这里是把jingyan赋值给baidu,由于类型不一致,所以要进行类型强制转化,用BaiDu这个类来进行强制转换,修改好后,再来执行,发现此时可以正常运行,输出想要的结果。ava是一种可以撰写跨平台应用软件的面向对象的程序设计语言。Java 技术具有卓越的通用性、高效性、平台移植性和安全性,广泛应用于PC、数据中心...
常见的十五种Java开发工具是什么,有什么特点
与Visual Age紧密结合的Websphere Studio本身并不提供源代码和版本管理的支持,它只是包含了一个内置文件锁定系统,当编辑项目的时候可以防止其他人对这些文件的错误修改,软件还支持诸如Microsoft Visual SourceSafe这样的第三方源代码控制系统。Visual Age forJava完全面向对象的程序设计思想使得开发程序非常快速、...
精通Hibernate:ava数据库持久层开发核心编程版权信息
通过阅读本书,读者将能够掌握如何使用Hibernate实现对象关系映射(ORM)、事务处理、缓存策略、二级缓存、事件监听等关键功能。此外,本书还提供了丰富的示例代码,帮助读者更好地理解和实践Hibernate的用法。本书不仅适合初学者作为学习资源,对于有经验的开发人员来说,也可以作为深入理解和掌握Hibernate技术的...
AVA最新大码和可以刷的武器,高手进!
刷代码需谨慎,说不定就被封号了
急求小屋模式纯黑免费色皮肤代码
javascript:window.top.space_addItem(1,2250,0,0,1,1,0); 国旗(爱国就用他哈。~)以下这些是黄钻专用皮肤代码:(全屏)javascript:window.top.space_addItem(1,13046,0,80,0,0,93); 纯黑,无任何装饰 javascript:window.top.space_addItem(1,12287,0,80,0,0,93); 纯黑白点阵 javascript:...
java软件开发要学哪些课程
熟悉常用的JavaAPI,包括集合框架、多线程(并发编程)、I\/O(NIO)、Socket、JDBC、XML、反射等。2.熟悉基于JSP和Servlet的JavaWeb开发,JSP全名为Java Server Pages,中文名叫java服务器页面,是动态网页服务器标准,能够给网页增加动态功能;3.前端技术:需要掌握HTML, CSS, Java Script, JQUERY, ...
windows7下玩AVA时总出现0x77d16b90指令引用的0x0000009c内存。该内...
答案【原创】严禁复制&。朋友!如果你用其它办法解决的问题,请说出:供其他朋友解决问题。谢谢!!!这个问题的出现比较普遍,主要有几个方面:●㊣●如果使用盗版系统,有可能会出现这样的问题,建议:使用正版。★★★☆如果对电脑没有影响或偶尔出现不用管它,重启电脑会自动消失。【1】病毒引起的,对电...
AVA退出时候蓝屏
这是BSOD中包含实际出错消息的位置。它看上去就是下面的代码:Stop: 0x0000001E (0xF24A447A, 0X00000001, 0X0000000)KMODE_EXCEPTION_NOT_HANDLED *** Address F24A447A base at f24A0000, DateStamp 35825ef8d - wdmaud.sys 其中错误代码是一个紧跟在Stop后面的十六进制数,在这里面它由其它四...
荡韩爱西:[答案] public class Calculator { private int number1; private int number2; public Calculator(int number1, int number2) { this.number1 = number1; this.number2 = number2; } public double add() { return numb...
定襄县15965524134: java编程,用代码实现计算器类(Calculator) - ?
荡韩爱西: package ch01; public class CalcTest {public static float number1,number2;public static void main(String[] args) {number1=10;number2=15;System.out.println(add());System.out.println(subtract());System.out.println(multiple());System.out....
定襄县15965524134: 如何用java编写简单的计算器? - ?
荡韩爱西: import java.awt.*; import java.awt.event.*; public class Cal { public static void main(String[] args) { CalFrame f = new CalFrame(); } } class CalFrame extends Frame { double d1, d2; int op = -1; TextField tf; CalPanelL p1; CalPanelR p2; // Constructor ...
定襄县15965524134: 怎么用JAVA编辑出计算器啊,一步一步的说哦,谢谢??
荡韩爱西: 程序类: impor java.util.Scanner; public class gzy2008 { double number1; double number2; public double jia() { double jia = number1 + number2; return jia; } public void showjia() { System.out.println("=" + jia()); } public double jian() { double jian = ...
定襄县15965524134: 求 用java编辑的计算器代码(易简)?
荡韩爱西: 呵呵 我写了一个比较简单的计算器,不知道能不能满足楼主的要求 /***************************java编写的简单的计算器***********************************/ import java.awt.*; import java.awt.event.*; class MyWindow extends Frame implements ...
定襄县15965524134: 求一个简单的JAVA计算器代码 - ?
荡韩爱西: import java.awt.*; import javax.swing.*; import java.awt.event.*; class Counter extends WindowAdapter { static JFrame f=new JFrame("计算器"); static JTextField text1=new JTextField("0."); static String source=""; static String cal=""; ...
定襄县15965524134: 用java代码编写个计算器代码: - ?
荡韩爱西: public class Calculator { void he(float num1,float num2) { System.out.println(num1+"+"+num2+"="+(num1+num2)); } void cha(float num1,float num2) { System.out.println(num1+"-"+num2+"="+(num1-num2)); } void ji(float num1,float num2) ...
定襄县15965524134: 用Java编写一个泛型计算器类 - ?
荡韩爱西: class Num<T> { public Number add(T a, T b) { Double n1 = Double.parseDouble(a.toString()); Double n2 = Double.parseDouble(b.toString()); return n1 + n2; } } public class Test5 { public static void main(String[] args) { Num num = new Num(); ...
定襄县15965524134: java的计算器类代码(类方法)?
荡韩爱西: public Operation(){ private double num1; private double num2; public double add(){ return num1+num2; } public double mlnus(){ return num1-num2; } public double multiple(){ return num1*num2; } public double divide(){ return num1/num2; } } is this your wanting
定襄县15965524134: java编写简单的计算器???? - ?
荡韩爱西: 代码长分几段:(一个类哦.) importjava.awt.*; importjava.awt.event.*; importjavax.swing.*; publicclassCalculatorextendsJFrame{ privateContainercontainer; privateGridBagLayoutlayout; privateGridBagConstraintsconstraints; ...