av手机免费在线观看,国产女人在线视频,国产xxxx免费,捆绑调教一二三区,97影院最新理论片,色之久久综合,国产精品日韩欧美一区二区三区

java語(yǔ)言

java計(jì)算器綜合實(shí)例學(xué)習(xí)教程

時(shí)間:2025-01-21 17:48:26 java語(yǔ)言 我要投稿
  • 相關(guān)推薦

java計(jì)算器綜合實(shí)例學(xué)習(xí)教程

  下面是百分網(wǎng)小編帶來(lái)的Java計(jì)算器綜合實(shí)例學(xué)習(xí)教程,歡迎學(xué)習(xí)!

java計(jì)算器綜合實(shí)例學(xué)習(xí)教程

  本節(jié)給出了一個(gè)計(jì)算器的綜合實(shí)例,該實(shí)例主要利用了本章及上一章所涉及的SWING組件及組件事件的處理來(lái)實(shí)現(xiàn)的,在實(shí)現(xiàn)的過(guò)程中還使用到了異常處理的知識(shí)。

  [例8-14]

  import java.awt.*;

  import java.awt.event.*;

  import javax.swing.*;

  class Calculator extends JFrame{

  String[] buttons =

  new String[]{"7","8","9","/","4","5","6","*","1","2","3","-","0","+/-",".","+"};

  JPanel pWest = new JPanel();

  JPanel pMain = new JPanel();

  JPanel pNorth = new JPanel();

  JPanel pMain_North = new JPanel();

  JPanel pMain_Center = new JPanel();

  JPanel pMain_East = new JPanel();

  JButton buttonMain[];//包括數(shù)字0-9以及'+','-','*','+'/'.','+/-'

  JButton buttonNorth[];//包括Back,CE,C

  JButton buttonEast[];//包括sqrt,%,1/x,=

  JTextField fieldResult = new JTextField();

  //

  static private String value1="";//第一個(gè)操作數(shù)

  static private String value2="";//第二個(gè)操作數(shù)

  static private String oper="";//操作符

  static private String result="";//運(yùn)算結(jié)果

  static private String lastChar="";//相比當(dāng)前,其上一個(gè)按下按鈕的字符

  static private boolean value1Input=true;//處于第一個(gè)操作數(shù)輸入情況

  static private boolean value2Input=false;//出于第二個(gè)操作數(shù)輸入情況

  static private boolean start = false;//是否開(kāi)始計(jì)算,主要是用于檢測(cè)剛開(kāi)始時(shí)按下非數(shù)字鍵的情況。

  public Calculator(String title){

  super(title);

  Container contentPane = this.getContentPane();

  fieldResult.setHorizontalAlignment(JTextField.RIGHT);//右對(duì)齊

  pMain_Center.setLayout(new GridLayout(4,4,5,5));

  buttonMain= new JButton[buttons.length];

  for(int i=0;i

  buttonMain[i] = new JButton(buttons[i]);

  pMain_Center.add(buttonMain[i]);

  }

  buttonNorth = new JButton[3];

  buttonNorth[0] = new JButton("Back");

  buttonNorth[1] = new JButton("CE");

  buttonNorth[2] = new JButton("C");

  pMain_North.setLayout(new GridLayout(1,3,5,5));

  pMain_North.add(buttonNorth[0]);

  pMain_North.add(buttonNorth[1]);

  pMain_North.add(buttonNorth[2]);

  buttonEast = new JButton[4];

  buttonEast[0] = new JButton("sqrt");

  buttonEast[1] = new JButton("%");

  buttonEast[2] = new JButton("1/x");

  buttonEast[3] = new JButton("=");

  pMain_East.setLayout(new GridLayout(4,1,5,5));

  pMain_East.add(buttonEast[0]);

  pMain_East.add(buttonEast[1]);

  pMain_East.add(buttonEast[2]);

  pMain_East.add(buttonEast[3]);

  contentPane.add(fieldResult,BorderLayout.NORTH);

  pMain.setLayout(new BorderLayout(5,5));

  pMain.add(pMain_Center,BorderLayout.WEST);

  pMain.add(pMain_East,BorderLayout.EAST);

  pMain.add(pMain_North,BorderLayout.NORTH);

  contentPane.add(pMain,BorderLayout.CENTER);

  pack();

  setVisible(true);

  //注冊(cè)監(jiān)x器

  MyActionListener listener = new MyActionListener();

  for(int i=0;i

  buttonMain[i].addActionListener(listener);

  }

  buttonNorth[0].addActionListener(listener);//Back

  buttonEast[3].addActionListener(listener);//"="

  buttonNorth[2].addActionListener(listener);//"C"

  buttonMain[13].setEnabled(false);//設(shè)置+/-不可用

  buttonMain[14].setEnabled(false);//設(shè)置.不可用

  buttonNorth[1].setEnabled(false);//設(shè)置CE不可用

  buttonEast[0].setEnabled(false);//設(shè)置sqrt不可用

  buttonEast[1].setEnabled(false);//設(shè)置%不可用

  buttonEast[2].setEnabled(false);//設(shè)置1/x不可用

  this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  }

  private class MyActionListener implements ActionListener{

  public void actionPerformed(ActionEvent e) {

  String str = e.getActionCommand();

  //數(shù)字鍵

  if(Character.isDigit(str.charAt(0))){

  handalDigit(str);

  }

  if(start == true){

  if(str.equals("=")){//"="鍵

  handalEqual(str);

  return;

  }

  //Back、C鍵處理,完善本程序時(shí)可在下面的語(yǔ)句中添加對(duì)其他按鈕的處理

  if(str.equals("Back")||str.equals("C")){

  handalNonOper(str);

  }

  //'+','-','*','+'/'的處理,完善本程序時(shí)可添加sqrt,%等操作符的處理

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

  handalOper(str);

  }

  }

  }

  //處理數(shù)字,str表示按鈕上的文字

  private void handalDigit(String str){

  char currentChar=str.charAt(0);//獲得按下的字符;

  if(!lastChar.equals("=")){

  if(value2Input == false){

  value1+=String.valueOf(currentChar);

  if(value1.charAt(0)=='0'){//第一個(gè)數(shù)字為0

  value1="0";

  }

  fieldResult.setText(value1);

  }

  else{

  value2+=String.valueOf(currentChar);

  if(value2.charAt(0)=='0'){//第一個(gè)數(shù)字為0

  value2="0";

  }

  fieldResult.setText(value2);

  }

  start = true;

  lastChar = str;

  }

  else{//是=

  resetAllValue();

  value1+=str;

  fieldResult.setText(value1);

  }

  printAllValue();

  }

  //處理"="鍵

  private void handalEqual(String str){

  if(Character.isDigit(lastChar.charAt(0)) && value2.length()>0){

  handalOper(str);

  }

  }

  //處理運(yùn)算符,暫時(shí)只處理+,-,*,/str表示按鈕上的文字

  private void handalOper(String str){

  if(start == true){

  if(value1.length()>0 && value2.length()>0){

  result=operationResult(value1,value2,oper.charAt(0));

  fieldResult.setText(result);

  if(!result.equals("除數(shù)不能為零")){

  value1=result;

  value2="";

  }

  }

  else{//說(shuō)明第一個(gè)操作數(shù)輸入結(jié)束,操作符結(jié)束,開(kāi)始輸入第二個(gè)操作數(shù)

  value1Input=false;

  value2Input=true;

  }

  oper = str;//記錄操作符

  lastChar = str;//記錄最后一個(gè)字符

  printAllValue();

  }

  }

  //處理非運(yùn)算符,但不是數(shù)字,如Back,C等鍵,str表示按鈕上的文字

  private void handalNonOper(String str){

  if(str.equals("Back")){//Back鍵處理

  if(Character.isDigit(lastChar.charAt(0)) == true){//必須是前一個(gè)字符是數(shù)字

  if(value2Input == true){

  if(value2.length()>0){

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

  }

  fieldResult.setText(value2);

  }

  else{

  if(value1.length()>0){

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

  }

  else{

  resetAllValue();

  }

  fieldResult.setText(value1);

  }

  }

  printAllValue();

  }

  if(str.equals("C")){//C鍵處理

  resetAllValue();

  fieldResult.setText("");

  printAllValue();

  }

  }

  //對(duì)兩個(gè)字符串value1和value2表示的整數(shù)做運(yùn)算,oper表示運(yùn)算符

  private String operationResult(String value1,String value2,char oper){

  String result;

  try{

  switch(oper){

  case '+':

  result = String.valueOf(Integer.parseInt(value1)+Integer.parseInt(value2));

  break;

  case '-':

  result = String.valueOf(Integer.parseInt(value1)-Integer.parseInt(value2));

  break;

  case '*':

  result = String.valueOf(Integer.parseInt(value1)*Integer.parseInt(value2));

  break;

  case '/':

  if(value2.equals("0")){

  result="除數(shù)不能為零";

  resetAllValue();//所有值置初值

  }

  else{

  result = String.valueOf(Integer.parseInt(value1)/Integer.parseInt(value2));

  }

  break;

  default:

  result="error";

  }

  }

  catch(Exception ex){

  result = "操作數(shù)或結(jié)果超過(guò)整數(shù)范圍";

  resetAllValue();

  }

  return result;

  }

  //重置變量值

  private void resetAllValue(){

  value1="";//第一個(gè)操作數(shù)

  value2="";//第二個(gè)操作數(shù)

  oper="";//操作符

  result="";//運(yùn)算結(jié)果

  lastChar="";//相比當(dāng)前,其上一個(gè)按下按鈕的字符

  value1Input=true;//處于第一個(gè)操作數(shù)輸入情況

  value2Input=false;//出于第二個(gè)操作數(shù)輸入情況

  start = false;

  }

  //打印變量值

  private void printAllValue(){

  System.out.println("| --------Values display----------------");

  System.out.println("| value1:" + value1);

  System.out.println("| value2:" + value2);

  System.out.println("| oper:" + oper);

  System.out.println("| result:" + result);

  System.out.println("| lastChar:" + lastChar);

  System.out.println("| value1Input:" + value1Input);

  System.out.println("| value2Input:" + value2Input);

  System.out.println("| start:" + start);

  System.out.println("| --------------------------------------");

  }

  }

  }

  public class Calculator{

  public static void main(String[] args) {

  new Calculator("計(jì)算器");

  }

  }

  程序運(yùn)行主界面如下:

  圖8-18 計(jì)算器程序運(yùn)行主界面

  該程序?qū)崿F(xiàn)了加減乘除運(yùn)算,且充分考慮了各種異常情況,一般不會(huì)出現(xiàn)在按下某個(gè)按鍵時(shí)出現(xiàn)程序報(bào)異常的錯(cuò)誤,對(duì)于程序中未實(shí)現(xiàn)的按鈕功能,運(yùn)行界面中都以灰色按鈕的形式出現(xiàn),讀者可在研究本程序的基礎(chǔ)上完成這些按鍵的功能。

【java計(jì)算器綜合實(shí)例學(xué)習(xí)教程】相關(guān)文章:

Java的特點(diǎn)學(xué)習(xí)教程08-27

Java中的== 和equals()方法詳解與實(shí)例教程08-03

Java數(shù)組的基礎(chǔ)學(xué)習(xí)教程08-12

Java數(shù)組的基本學(xué)習(xí)教程08-22

Java對(duì)話框?qū)W習(xí)教程05-31

java編譯計(jì)算器08-11

python概率計(jì)算器實(shí)例分析11-11

Axure交互實(shí)例教程07-19

java教程之Java編程基礎(chǔ)09-12