<del id="d4fwx"><form id="d4fwx"></form></del>
      <del id="d4fwx"><form id="d4fwx"></form></del><del id="d4fwx"><form id="d4fwx"></form></del>

            <code id="d4fwx"><abbr id="d4fwx"></abbr></code>
          • java計算器簡易代碼,java做計算器代碼

            編寫java程序簡單計算器

            主要涉及的知識點: 類的寫法, 以及方法的調用 .建議多做練習. 如果有看不懂的地方. 可以繼續(xù)追問,一起討論.

            創(chuàng)新互聯(lián)建站2013年開創(chuàng)至今,先為興寧等服務建站,興寧等地企業(yè),進行企業(yè)商務咨詢服務。為興寧企業(yè)網(wǎng)站制作PC+手機+微官網(wǎng)三網(wǎng)同步一站式服務解決您的所有建站問題。

            參考代碼如下

            //Number類

            class?Number?{

            private?int?n1;//私有的整型數(shù)據(jù)成員n1

            private?int?n2;//私有的整型數(shù)據(jù)成員n2

            //?通過構造函數(shù)給n1和n2賦值

            public?Number(int?n1,?int?n2)?{

            this.n1?=?n1;

            this.n2?=?n2;

            }

            //?加法

            public?int?addition()?{

            return?n1?+?n2;

            }

            //?減法

            public?int?subtration()?{

            return?n1?-?n2;

            }

            //?乘法

            public?int?multiplication()?{

            return?n1?*?n2;

            }

            //?除法?(可能除不盡,所以使用double作為返回類型)

            public?double?division()?{

            return?n1?*?1.0?/?n2;?//?通過n1*1.0?把計算結果轉換成double類型.

            }

            }

            //Exam4?類

            public?class?Exam4{

            public?static?void?main(String[]?args)?{

            Number?number=new?Number(15,?6);//創(chuàng)建Number類的對象

            //下面的是調用方法得到返回值進行輸出顯示

            System.out.println("加法"+number.addition());

            System.out.println("減法"+number.subtration());

            System.out.println("乘法"+number.multiplication());

            System.out.println("除法"+number.division());

            }

            }

            求大神發(fā)個完整簡單的java計算器代碼,

            public static void main(String[] args) {

            System.out.println("簡單計算器");

            boolean flag=true;//while循環(huán)是否繼續(xù),true繼續(xù)循環(huán),false停止循環(huán)

            System.out.println("請輸入第一個數(shù)字");

            while(flag){

            Scanner scan = new Scanner(System.in);

            String bh=scan.next();

            try {

            double num = Double.parseDouble(bh);

            System.out.println("請輸入符號(+、-、*、/)");

            while(1==1){

            String fh=scan.next();

            if("+".equals(fh) || "-".equals(fh) || "*".equals(fh) || "/".equals(fh)){

            System.out.println("請輸入第二個數(shù)字");

            while(1==1){

            String bh2=scan.next();

            try {

            double num2 = Double.parseDouble(bh2);

            double num3=0;

            if("+".equals(fh)){

            num3=num+num2;

            }else if("-".equals(fh)){

            num3=num-num2;

            }else if("*".equals(fh)){

            num3=num*num2;

            }else if("/".equals(fh)){

            num3=num/num2;

            }

            System.out.println(num3);

            break;

            } catch (Exception e) {

            System.out.println("請輸入第二個正確的數(shù)字");

            continue;

            }

            }

            break;

            }else{

            System.out.println("請輸入正確的符號(+、-、*、/)");

            continue;

            }

            }

            System.out.println("是否繼續(xù)運算:輸入Y或者y繼續(xù),輸入其它任意字符退出");

            String yn=scan.next();

            if("Y".equals(yn) || "y".equals(yn)){

            continue;

            }else{

            System.out.println("運算結束");

            break;

            }

            }catch (NumberFormatException e) {//輸入非數(shù)字類型時

            System.out.println("請輸入第一個正確的數(shù)字");

            continue;

            }

            }

            }

            急求java簡易計算器代碼

            試試下面的代碼 絕對沒有錯誤。

            package main;

            import java.awt.Button;

            import java.awt.Color;

            import java.awt.GridLayout;

            import java.awt.Panel;

            import java.awt.event.ActionEvent;

            import java.awt.event.ActionListener;

            import java.math.BigDecimal;

            import javax.swing.JFrame;

            import javax.swing.JTextField;

            public class app7 extends JFrame implements ActionListener {

            static Panel pan = new Panel();

            static JTextField textField = new JTextField("0");

            static Button b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, bp, ba, bs, bm, bd,

            be, bc, bt, bf, bh;

            private StringBuffer temp = new StringBuffer("");

            private String optValue = "0";

            private String optType="";

            private boolean isChoiseOptType=true;

            public void init() {

            b0 = new Button("0");

            b0.addActionListener(this);

            b1 = new Button("1");

            b1.addActionListener(this);

            b2 = new Button("2");

            b2.addActionListener(this);

            b3 = new Button("3");

            b3.addActionListener(this);

            b4 = new Button("4");

            b4.addActionListener(this);

            b5 = new Button("5");

            b5.addActionListener(this);

            b6 = new Button("6");

            b6.addActionListener(this);

            b7 = new Button("7");

            b7.addActionListener(this);

            b8 = new Button("8");

            b8.addActionListener(this);

            b9 = new Button("9");

            b9.addActionListener(this);

            bp = new Button(".");

            bp.addActionListener(this);

            ba = new Button("+");

            ba.addActionListener(this);

            bs = new Button("-");

            bs.addActionListener(this);

            bm = new Button("*");

            bm.addActionListener(this);

            bd = new Button("/");

            bd.addActionListener(this);

            be = new Button("=");

            be.addActionListener(this);

            bc = new Button("c");

            bc.addActionListener(this);

            bt = new Button("退格");

            bt.addActionListener(this);

            bf = new Button("1/x");

            bf.addActionListener(this);

            bh = new Button("+/-");

            bh.addActionListener(this);

            this.setTitle("計算機");

            this.setLayout(null);

            this.setSize(260, 300);

            this.setResizable(false);

            GridLayout grid = new GridLayout(4, 5);

            pan.setLayout(grid);

            pan.setBounds(20, 60, 150, 120);

            textField.setBounds(20, 35, 150, 20);

            textField.setBackground(Color.cyan);

            textField.setHorizontalAlignment(textField.RIGHT);

            textField.setEditable(false);

            pan.add(b1);

            pan.add(b2);

            pan.add(b3);

            pan.add(ba);

            pan.add(bc);

            pan.add(b4);

            pan.add(b5);

            pan.add(b6);

            pan.add(bs);

            pan.add(bt);

            pan.add(b7);

            pan.add(b8);

            pan.add(b9);

            pan.add(bm);

            pan.add(bf);

            pan.add(b0);

            pan.add(bh);

            pan.add(bp);

            pan.add(bd);

            pan.add(be);

            this.add(textField);

            this.add(pan);

            }

            public static void main(String[] args) {

            app7 frm = new app7();

            frm.init();

            frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            frm.setVisible(true);

            }

            @SuppressWarnings("static-access")

            @Override

            public void actionPerformed(ActionEvent e) {

            String value="0";

            if (e.getSource().equals(b0)) {

            this.temp.append(b0.getLabel());

            this.textField.setText(this.temp.toString());

            isChoiseOptType=false;

            } else if (e.getSource().equals(b1)) {

            this.temp.append(b1.getLabel());

            this.textField.setText(this.temp.toString());

            isChoiseOptType=false;

            } else if (e.getSource().equals(b2)) {

            this.temp.append(b2.getLabel());

            this.textField.setText(this.temp.toString());

            isChoiseOptType=false;

            } else if (e.getSource().equals(b3)) {

            this.temp.append(b3.getLabel());

            this.textField.setText(this.temp.toString());

            isChoiseOptType=false;

            } else if (e.getSource().equals(b4)) {

            this.temp.append(b4.getLabel());

            this.textField.setText(this.temp.toString());

            isChoiseOptType=false;

            } else if (e.getSource().equals(b5)) {

            this.temp.append(b5.getLabel());

            this.textField.setText(this.temp.toString());

            isChoiseOptType=false;

            } else if (e.getSource().equals(b6)) {

            this.temp.append(b6.getLabel());

            this.textField.setText(this.temp.toString());

            isChoiseOptType=false;

            } else if (e.getSource().equals(b7)) {

            this.temp.append(b7.getLabel());

            this.textField.setText(this.temp.toString());

            isChoiseOptType=false;

            } else if (e.getSource().equals(b8)) {

            this.temp.append(b8.getLabel());

            this.textField.setText(this.temp.toString());

            isChoiseOptType=false;

            } else if (e.getSource().equals(b9)) {

            this.temp.append(b9.getLabel());

            this.textField.setText(this.temp.toString());

            isChoiseOptType=false;

            } else if (e.getSource().equals(bp)) {

            if(this.temp.length()=0)

            this.temp.append("0");

            this.temp.append(bp.getLabel());

            this.textField.setText(this.temp.toString());

            isChoiseOptType=false;

            } else if (e.getSource().equals(ba)) {

            if(!isChoiseOptType){

            value=this.textField.getText();

            if(value.lastIndexOf(".")==value.length()-1){

            value=value.substring(0,value.length()-1);

            }

            this.optValue=value;

            this.temp=new StringBuffer("");

            }

            this.optType=ba.getLabel();

            isChoiseOptType=true;

            } else if (e.getSource().equals(bs)) {

            if(!isChoiseOptType){

            value=this.textField.getText();

            if(value.lastIndexOf(".")==value.length()-1){

            value=value.substring(0,value.length()-1);

            }

            this.optValue=value;

            this.temp=new StringBuffer("");

            }

            this.optType=bs.getLabel();

            isChoiseOptType=true;

            } else if (e.getSource().equals(bm)) {

            if(!isChoiseOptType){

            value=this.textField.getText();

            if(value.lastIndexOf(".")==value.length()-1){

            value=value.substring(0,value.length()-1);

            }

            this.optValue=value;

            this.temp=new StringBuffer("");

            }

            this.optType=bm.getLabel();

            isChoiseOptType=true;

            } else if (e.getSource().equals(bd)) {

            if(!isChoiseOptType){

            value=this.textField.getText();

            if(value.lastIndexOf(".")==value.length()-1){

            value=value.substring(0,value.length()-1);

            }

            this.optValue=value;

            this.temp=new StringBuffer("");

            }

            this.optType=bd.getLabel();

            isChoiseOptType=true;

            }else if (e.getSource().equals(be)) {

            if(!this.optType.equals("")){

            BigDecimal opt1=new BigDecimal(this.optValue);

            value=this.textField.getText();

            if(value.lastIndexOf(".")==value.length()-1){

            value=value.substring(0,value.length()-1);

            }

            BigDecimal opt2=new BigDecimal(value);

            BigDecimal result=new BigDecimal(0);

            if(this.optType.equals("+")){

            result=opt1.add(opt2);

            }else if(this.optType.equals("-")){

            result=opt1.subtract(opt2);

            }else if(this.optType.equals("*")){

            result=opt1.multiply(opt2);

            }else if(this.optType.equals("/")){

            result=opt1.divide(opt2);

            }else if(this.optType.equals("%")){

            result=opt1.remainder(opt2);

            }

            this.textField.setText(result.toString());

            this.temp=new StringBuffer("");

            isChoiseOptType=false;

            this.optValue="0";

            }

            } else if (e.getSource().equals(bc)) {

            this.temp=new StringBuffer();

            this.textField.setText("0");

            } else if (e.getSource().equals(bt)) {

            value=this.textField.getText();

            value=value.substring(0,value.length()-1);

            if(value.indexOf("-")=0 value.length()=1){

            value="0";

            this.temp=new StringBuffer("");

            }else{

            this.temp=new StringBuffer(value);

            }

            this.textField.setText(value);

            }else if (e.getSource().equals(bh)) {

            value=this.textField.getText();

            if(value.indexOf("-")==0){

            value=value.substring(1,value.length());

            }else{

            value="-"+value;

            }

            this.temp=new StringBuffer(value);

            this.textField.setText(value);

            } else if (e.getSource().equals(bf)) {

            this.optValue=this.textField.getText();

            if(value.lastIndexOf(".")==value.length()-1){

            this.optValue=this.optValue.substring(0,this.optValue.length()-1);

            }

            Integer opt1=new Integer(this.optValue);

            if(!opt1.toString().equals("0")){

            this.textField.setText(1.0/opt1.intValue()+"");

            System.out.println(1/opt1.intValue()+"");

            }else{

            this.textField.setText("0");

            }

            this.temp=new StringBuffer("");

            this.optType="";

            this.optValue="0";

            }

            }

            }

            記住類名是app7.java 包名是main. 如果有不對的地方 到時候在找我 我在線。

            分享文章:java計算器簡易代碼,java做計算器代碼
            當前網(wǎng)址:http://www.jbt999.com/article6/hedsig.html

            成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設計公司面包屑導航、外貿網(wǎng)站建設、App設計、電子商務、網(wǎng)站維護

            廣告

            聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:[email protected]。內容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

            成都網(wǎng)站建設

              <del id="d4fwx"><form id="d4fwx"></form></del>
              <del id="d4fwx"><form id="d4fwx"></form></del><del id="d4fwx"><form id="d4fwx"></form></del>

                    <code id="d4fwx"><abbr id="d4fwx"></abbr></code>
                  • 丰满老妇高潮一级A片 | 日本黄色视频网址电影 | 精品少妇无码视频 | 性一乱一交一交一视频 | 婷婷五月天丁香在线 |