บทความนี้สรุปวิธีการทั่วไปสำหรับการเขียนเครื่องคิดเลขใน Java แบ่งปันสำหรับการอ้างอิงของคุณดังนี้:
วิธีที่ 1:
แพ็คเกจ wanwa; นำเข้า java.awt.*; นำเข้า Java.awt.event.*; นำเข้า Javax.swing.*; เครื่องคิดเลขคลาสสาธารณะขยาย jframe {คอนเทนเนอร์ส่วนตัวคอนเทนเนอร์, layout gridbaglayout ส่วนตัว; ผลลัพธ์; // บันทึกการคำนวณผลลัพธ์บูลีนส่วนตัวเริ่มต้น; // พิจารณาว่าเป็นจุดเริ่มต้นของเครื่องคิดเลขจำนวนมาก () {super ("เครื่องคิดเลข") คอนเทนเนอร์ = getContentPane (); layout = new gridbaglayout () container.setLayout Jtextfield (20); DisplayField.Sethorizontalalignment (jtextfield.right); constraints.gridx = 0; ข้อ จำกัด gridy = 0; ข้อ จำกัด . gridwidth = 4; ข้อ จำกัด . ข้อ จำกัด ); container.add (displayfield); actionListener insert = new insertaction (); ActionListener Command = New Commandaction (); // AddButton ("backspace", 0, 1, 2, 1, insert); // addbutton ("ce", 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, แทรก); AddButton ("7", 0, 2, 1, 1, Insert); AddButton ("8", 1, 2, 1, 1, 1, Insert); AddButton ("9", 2, 2, 1, 1, 1, addButton ("/", 3, 2, 1, 1, 1, 1, 1, 1, 3, 1, AddButton ("6", 2, 3, 1, 1, Insert); AddButton ("*", 3, 3, 1, 1, 1, คำสั่ง); AddButton ("1", 0, 4, 1, 1, INSERT); AddButton ("2", 1, 1, 1, 1, 1, 1, แทรก); AddButton ("-", 3, 4, 1, 1, คำสั่ง); AddButton ("0", 0, 5, 1, 1, 1, แทรก); // AddButton ("+/-", 1, 5, 1, 1, 1, 1, 1, " แทรก); addButton ("+", 3, 5, 1, 1, 1, คำสั่ง); addButton ("=", 0, 6, 4, 1, 1, คำสั่ง); this.setResizable (false); setSize (180, 200); setVisible (true);} โมฆะส่วนตัว AddButton (สตริงฉลาก, แถว int, คอลัมน์ int, int ด้วย, ความสูง int, actionListener Listener) {JButton ปุ่ม = new JButton (ฉลาก); ข้อ จำกัด ความสูง; ข้อ จำกัด fill = gridbagConstraints.both; button.addactionListener (ผู้ฟัง) layout.setConstraints (ปุ่ม, ข้อ จำกัด ); container.add (ปุ่ม);} การแทรกคลาสส่วนตัวดำเนินการ {displayfield.settext ("); start = false; if (input.equals (" +/- ")) displayfield.settext (displayfield.getText ()+"-");} ถ้า (! input.equals (" +/- ") {ถ้า (input.equals (" backspace ") displayfield.settext (str.substring (0, str.length () - 1));} else (input.equals ("ce") || input.equals ("c")) {displayfield.settext ("0"); start = true; ActionPerformed (ActionEvent EVT) {String Command = EVT.GetActionCommand (); if (เริ่มต้น) {lastCommand = คำสั่ง; } else {คำนวณ (double.parsedouble (displayfield.getText ())); LastCommand = คำสั่ง; เริ่ม = จริง; }}} โมฆะสาธารณะคำนวณ (double x) {ถ้า (lastCommand.equals (" +")) ผลลัพธ์ += x; อื่นถ้า (lastCommand.equals (" -")) ผลลัพธ์ -= x; อื่น ๆ ถ้า (lastCommand.equals (" *") ผลลัพธ์ *= x; x; displayfield.settext ("" + ผลลัพธ์);} โมฆะคงที่สาธารณะหลัก (สตริง [] args) {คำนวณเครื่องคิดเลข = เครื่องคิดเลขใหม่ (); calculator.setDefaultCloseoperation (jFrame.Exit_on_close);}}}}วิธีที่ 2:
นำเข้า Java.awt.*; นำเข้า Java.awt.event.*; Mycalculator คลาสสาธารณะ {เฟรมส่วนตัว f; TextField ส่วนตัว TF = New TextField (30); ผลลัพธ์ส่วนตัวยาว ภาคผนวกบูลีนส่วนตัว = FALSE; ตัวดำเนินการถ่านหินส่วนตัว = '='; ปุ่มส่วนตัว [] btn = ปุ่มใหม่ [15]; mycalculator สาธารณะ () {initComponent (); } โมฆะส่วนตัว initComponent () {f = เฟรมใหม่ ("เครื่องคิดเลขของฉัน v1.0"); F.SetLayout (New BorderLayout ()); // เฟรมใช้ BorderLayout F.AddWindowListener (WindowAdapter ใหม่ () {โมฆะสาธารณะ Windowclosing (WindowEvent EVT) {System.Exit (0);}}); Panel CenterPanel = แผงใหม่ (); centerpanel.setLayout (ใหม่ gridlayout (5, 3)); // พาเนลใช้ gridlayout numberListener nl = numberListener (); OperatorListener OL = New OperatorListener (); btn [10] = ปุ่มใหม่ ("+"); btn [11] = ปุ่มใหม่ ("-"); btn [12] = ปุ่มใหม่ ("*"); btn [13] = ปุ่มใหม่ ("/"); btn [14] = ปุ่มใหม่ ("="); สำหรับ (int i = 0; i <= 9; i ++) {btn [i] = ปุ่มใหม่ (string.valueof (i)); centerpanel.add (btn [i]); btn [i] .addactionListener (NL); if (i%2 == 1) {centerpanel.add (btn [(i+19)/2]); btn [(i+19)/2] .addactionListener (OL); }} f.add (centerpanel, borderlayout.center); Panel NorthPanel = แผงใหม่ (); tf.seteditable (เท็จ); Northpanel.add (TF); F.Add (Northpanel, Borderlayout.north); } โมฆะสาธารณะไป () {f.pack (); f.setVisible (จริง); } โมฆะคงที่สาธารณะหลัก (สตริง [] args) {mycalculator ใหม่ (). go (); } /***ใช้วิธีการคลาสภายในเพื่อใช้อินเทอร์เฟซผู้ฟังเพื่ออำนวยความสะดวกในการเข้าถึงสมาชิกภายในของคลาสหลักภายในชั้นเรียน *คลาสนี้มีหน้าที่รับผิดชอบในการฟังและการจัดการเหตุการณ์การกระทำของปุ่มตัวเลข*/ คลาส NumberListener ใช้ ActionListener {โมฆะสาธารณะ ActionPerformed (ActionEvent E) {ถ้า (! ภาคผนวก) {tf.settext (""); ผนวก = จริง; } สตริง s = tf.getText (); s+= e.getActionCommand (); tf.settext (s); if (! btn [10] .isenabled ()) {สำหรับ (int i = 10; i <= 14; i ++) btn [i] .setenabled (จริง); }}} / *** คลาสสมาชิกภายใน, รับผิดชอบในการจัดการเหตุการณ์ของปุ่มผู้ปฏิบัติงาน* / ผู้ดำเนินการในชั้นเรียนใช้ ActionListener {โมฆะสาธารณะ ActionPerformed (ActionEvent E) {ถ้า (! ภาคผนวก) กลับ; สำหรับ (int i = 10; i <= 14; i ++) btn [i] .setenabled (เท็จ); สตริง s = tf.getText (); long num = long.parselong (s); // รับจำนวนของ textfield append = false; // ตั้งค่าสวิตช์ภาคผนวก (ตัวดำเนินการ) {case '+': ผลลัพธ์+= num; break; กรณี '-': ผลลัพธ์-= num; break; กรณี '*': ผลลัพธ์*= num; break; กรณี '/': {ถ้า (num == 0) result = 0; ผลลัพธ์อื่น/= num; หยุดพัก; } case '=': result = num; break; } tf.settext (string.valueof (ผลลัพธ์)); // ตั้งค่าผลลัพธ์ของผลลัพธ์เป็นสตริง textField op = e.getActionCommand (); Operator = op.charat (0); // set operator}}}วิธีที่ 3:
แพ็คเกจ wanwa; นำเข้า java.util.*; คลาสสาธารณะ calc {โมฆะคงที่สาธารณะหลัก (สตริง [] args) {สแกนเนอร์อินพุต = สแกนเนอร์ใหม่ (System.in); System.out.println ("************************ คำแนะนำสำหรับการใช้งาน: 1. เพิ่มเติม 2. การลบ 3. การคูณ 4. การแบ่ง*"); system.out.println ("*/t/t/t/t/t/t*"); System.out.println (" ********************************************************* i = 0; i <100; i ++) {system.out.print ("/nplease เลือกกฎการดำเนินการ:"); int num = input.nextint (); สวิตช์ (num) {กรณีที่ 1: system.out.println ("/n ****** คุณได้เลือกเพิ่มเติม *******/n"); system.out.print ("โปรดป้อน adder แรก:"); int jiashu1 = input.nextint (); คือ: " + Jiashu1 +" + " + Jiashu1 +" = " + (jiashu1 + jiashu2)); break; กรณีที่ 2: system.out.println ("/n ****** คุณเลือกการลบ *******/n "); input.nextint (); system.out.print ("โปรดป้อนการลบ:"); int jianshu2 = input.nextint (); system.out.println ("ผลการดำเนินการคือ:" + jianshu1 + " -" + jianshu2 + "=" + (jianshu1 - jianshu2)); การคูณ *******/n "); system.out.print (" โปรดป้อนปัจจัยแรก: "); int chengfa1 = input.nextint (); system.out.print (" โปรดป้อนปัจจัยที่สอง: "); int chengfa2 = input.nextint (); *chengfa2)); break; กรณีที่ 4: system.out.println ("/n ****** คุณเลือกแผนก *******/n"); system.out.print ("โปรดป้อนตัวหาร:"); double chufa1 = input.nextint (); input.nextint (); system.out.println ("ผลการดำเนินการคือ:" + chufa1 + " /" + chufa2 + "=" + (chufa1 / chufa2) + "yu" + (Chufa1 % Chufa2); เลือกอีกครั้ง! "); break;}}}}วิธีที่ 4:
แพ็คเกจ wanwa; นำเข้า java.awt.*; นำเข้า Java.awt.event.*; นำเข้า Javax.swing.*; เครื่องคิดเลขคลาสสาธารณะขยาย jframe {คอนเทนเนอร์ส่วนตัวคอนเทนเนอร์, layout gridbaglayout ส่วนตัว; ผลลัพธ์; // บันทึกการคำนวณผลลัพธ์บูลีนส่วนตัวเริ่มต้น; // พิจารณาว่าเป็นจุดเริ่มต้นของเครื่องคิดเลขจำนวนมาก () {super ("เครื่องคิดเลข") คอนเทนเนอร์ = getContentPane (); layout = new gridbaglayout () container.setLayout Jtextfield (20); DisplayField.Sethorizontalalignment (jtextfield.right); constraints.gridx = 0; ข้อ จำกัด gridy = 0; ข้อ จำกัด . gridwidth = 4; ข้อ จำกัด . ข้อ จำกัด ); container.add (displayfield); actionListener insert = new insertaction (); ActionListener Command = New Commandaction (); // AddButton ("backspace", 0, 1, 2, 1, insert); // addbutton ("ce", 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, แทรก); AddButton ("7", 0, 2, 1, 1, Insert); AddButton ("8", 1, 2, 1, 1, 1, Insert); AddButton ("9", 2, 2, 1, 1, 1, addButton ("/", 3, 2, 1, 1, 1, 1, 1, 1, 3, 1, AddButton ("6", 2, 3, 1, 1, Insert); AddButton ("*", 3, 3, 1, 1, 1, คำสั่ง); AddButton ("1", 0, 4, 1, 1, INSERT); AddButton ("2", 1, 1, 1, 1, 1, 1, แทรก); AddButton ("-", 3, 4, 1, 1, คำสั่ง); AddButton ("0", 0, 5, 1, 1, 1, แทรก); // AddButton ("+/-", 1, 5, 1, 1, 1, 1, 1, " แทรก); addButton ("+", 3, 5, 1, 1, 1, คำสั่ง); addButton ("=", 0, 6, 4, 1, 1, คำสั่ง); this.setResizable (false); setSize (180, 200); setVisible (true);} โมฆะส่วนตัว AddButton (สตริงฉลาก, แถว int, คอลัมน์ int, int ด้วย, ความสูง int, actionListener Listener) {JButton ปุ่ม = new JButton (ฉลาก); ข้อ จำกัด ความสูง; ข้อ จำกัด fill = gridbagConstraints.both; button.addactionListener (ผู้ฟัง) layout.setConstraints (ปุ่ม, ข้อ จำกัด ); container.add (ปุ่ม);} การแทรกคลาสส่วนตัวดำเนินการ {displayfield.settext ("); start = false; if (input.equals (" +/- ")) displayfield.settext (displayfield.getText ()+"-");} ถ้า (! input.equals (" +/- ") {ถ้า (input.equals (" backspace ") displayfield.settext (str.substring (0, str.length () - 1));} else (input.equals ("ce") || input.equals ("c")) {displayfield.settext ("0"); start = true; ActionPerformed (ActionEvent EVT) {String Command = EVT.GetActionCommand (); if (เริ่มต้น) {lastCommand = คำสั่ง; } else {คำนวณ (double.parsedouble (displayfield.getText ())); LastCommand = คำสั่ง; เริ่ม = จริง; }}} โมฆะสาธารณะคำนวณ (double x) {ถ้า (lastCommand.equals (" +")) ผลลัพธ์ += x; อื่นถ้า (lastCommand.equals (" -")) ผลลัพธ์ -= x; อื่น ๆ ถ้า (lastCommand.equals (" *") ผลลัพธ์ *= x; x; displayfield.settext ("" + ผลลัพธ์);} โมฆะคงที่สาธารณะหลัก (สตริง [] args) {คำนวณเครื่องคิดเลข = เครื่องคิดเลขใหม่ (); calculator.setDefaultCloseoperation (jFrame.Exit_on_close);}}}}ฉันหวังว่าบทความนี้จะเป็นประโยชน์กับการเขียนโปรแกรม Java ของทุกคน