首页 > 基础知识 > 正文

java 计算器,编写一个股票买卖收益计算器

2020-04-11 16:10:57  来源:http://www.capsicumpm.com  编辑:admin

4.编写Java程序,模拟简单的计算器 。 定义名为Number的类,其中有两个整型数据成员n1和n2。编写构造方法

publicclassTest{publicstaticvoidmain(String[]args){Numbernum=newNumber(100,20);System.out.println(num.getN1()+"+"+num.getN2()+"="+num.addition());System.out.println(num.getN1()+"-"+num.getN2()+"="+num.subtration());System.out.println(num.getN1()+"×"+num.getN2()+"="+num.multiplication());System.out.println(num.getN1()+"÷"+num.getN2()+"="+num.division());}}classNumber{privateintn1;privateintn2;publicNumber(intn1,intn2){this.n1=n1;this.n2=n2;}publicintaddition(){returnn1+n2;}publicintsubtration(){returnn1-n2;}publicintmultiplication(){returnn1*n2;}publicintdivision(){if(n2==0){System.out.println("除数不能为0!");return-0xfffffff;}returnn1/n2;}publicintgetN1(){returnn1;}publicvoidsetN1(intn1){this.n1=n1;}publicintgetN2(){returnn2;}publicvoidsetN2(intn2){this.n2=n2;}}

制作java计算器

有分加没啊 !!!!!! import java.awt.*;import java.awt.event.*;public class jisuanqi extends WindowAdapter { Panel p1 = new Panel(); Panel p2 = new Panel(); Panel p3 = new Panel(); TextField txt; private Button[] b = new Button[17]; private String ss[] = { "7", "8", "9", "+", "4", "5", "6", "-", "1", "2", "3", "*", "清空", "0", "=", "/", "关闭" }; static double a; static String s, str;// 定义变量 创建对像 public static void main(String args[]) { (new jisuanqi()).frame(); } public void frame() { Frame fm = new Frame("简单计算器"); for (int i = 0; i <= 16; i++) { b[i] = new Button(ss[i]); } for (int i = 0; i <= 15; i++) { p2.add(b[i]); } // 创建按钮 并添加到P2 b[16].setBackground(Color.yellow); txt = new TextField(15); txt.setEditable(false); for (int i = 0; i <= 16; i++) { b[i].addActionListener(new buttonlistener());// 添加监听器 } b[16].addActionListener(new close()); fm.addWindowListener(this); fm.setBackground(Color.red); p1.setLayout(new BorderLayout()); p1.add(txt, "North"); p2.setLayout(new GridLayout(4, 4)); p3.setLayout(new BorderLayout()); p3.add(b[16]); fm.add(p1, "North"); fm.add(p2, "Center"); fm.add(p3, "South"); fm.pack(); fm.setVisible(true);// 都是些窗中设置 添加相关组件和监听器 } public void windowClosing(WindowEvent e) { System.exit(0);// 退出系统 } class buttonlistener implements ActionListener {// 编写监听器事件 通过按键得出给果 public void actionPerformed(ActionEvent e) { Button btn = (Button) e.getSource(); if (btn.getLabel() == "=") { jisuan(); str = String.valueOf(a); txt.setText(str); s = ""; } else if (btn.getLabel() == "+") { jisuan(); txt.setText(""); s = "+"; } else if (btn.getLabel() == "-") { jisuan(); txt.setText(""); s = "-"; } else if (btn.getLabel() == "/") { jisuan(); txt.setText(""); s = "/"; } else if (btn.getLabel() == "*") { jisuan(); txt.setText(""); s = "*"; } else { txt.setText(txt.getText() + btn.getLabel()); if (btn.getLabel() == "清空") txt.setText(""); } } public void jisuan() {// 编写具体计算方法 if (s == "+") a += Double.parseDouble(txt.getText()); else if (s == "-") a -= Double.parseDouble(txt.getText()); else if (s == "*") a *= Double.parseDouble(txt.getText()); else if (s == "/") a /= Double.parseDouble(txt.getText()); else a = Double.parseDouble(txt.getText()); } }}class close implements ActionListener {// 退出 public void actionPerformed(ActionEvent e) { System.exit(0); }}

java制作计算器怎么写首位只能输入一个零

String str = btnNewButton.getText();String TextStr = TextField.getText();//获取文本框中内容if (str.equals(null)){return;}else if(TextStr.equals("00")){//加入文本框内容是00,则直接返回return;}else {textField.setText(textStr + str);}也可以这样写:String str = btnNewButton.getText();String TextStr = TextField.getText();//获取文本框中内容if (str.equals(null)||TextStr.equals("00")){return;}else {textField.setText(textStr + str);}经过切点且垂直于切线的直线必经过圆心切线长

怎样用Java设计一个计算器 要用到subversion、Ant或Maven 说明是什么操作系统 IDE,界面上的需求 非常感谢

不懂你的意思

一个普通的二本非计算机专业的毕业生能学懂java吗?该如何入手

能,最好还是去报培训班比较好!