2014-01-05 86 views
1

這是我在這裏的第一篇文章,我希望如果我可以得到我有一個學校項目的一些幫助。該項目是用Java編寫一個計算器,我已經完成了它,並且大部分工作正常,唯一的問題是當我得到一個答案 - 例如5+5=10和10被顯示時,當我想輸入另一個時號碼可以說我想進入8 * 10,爲了做到這一點,我寫在8,而不是刪除以前的答案是10和寫8而不是那個數字它會寫10在10之後,所以它將是108 。我想要的是一旦我在給出答案後輸入新的號碼,先前的答案將被刪除。我希望我解釋得很好,這裏是我的代碼,我想在這件事上得到一些幫助,因爲我試過的所有東西都沒有奏效。提前致謝。Java計算器幫助

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 

public class Calculator_UI implements ActionListener { 

    JFrame frame = new JFrame("Calculator"); 
    JPanel panel = new JPanel(); 
    JTextArea text = new JTextArea(1, 20); 
    JButton but1 = new JButton("1"); 
    JButton but2 = new JButton("2"); 
    JButton but3 = new JButton("3"); 
    JButton but4 = new JButton("4"); 
    JButton but5 = new JButton("5"); 
    JButton but6 = new JButton("6"); 
    JButton but7 = new JButton("7"); 
    JButton but8 = new JButton("8"); 
    JButton but9 = new JButton("9"); 
    JButton but0 = new JButton("0"); 
    JButton butadd = new JButton("+"); 
    JButton butsub = new JButton("-"); 
    JButton butmulti = new JButton("*"); 
    JButton butdiv = new JButton("/"); 
    JButton buteq = new JButton("="); 
    JButton butclear = new JButton("C"); 
    Double number1, number2, result; 
    int addc = 0, subc = 0, multic = 0, divc = 0; 

    public void ui() { 
     frame.setVisible(true); 
     frame.setSize(230, 200); 
     frame.setResizable(false); 
     frame.setLocationRelativeTo(null); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     frame.add(panel); 

     panel.add(text); 
     panel.add(but1); 
     panel.add(but2); 
     panel.add(but3); 
     panel.add(but4); 
     panel.add(but5); 
     panel.add(but6); 
     panel.add(but7); 
     panel.add(but8); 
     panel.add(but9); 
     panel.add(but0); 

     panel.add(butadd); 
     panel.add(butsub); 
     panel.add(butmulti); 
     panel.add(butdiv); 
     panel.add(buteq); 
     panel.add(butclear); 

     but1.addActionListener(this); 
     but1.setBackground(Color.cyan); 
     but2.addActionListener(this); 
     but2.setBackground(Color.cyan); 
     but3.addActionListener(this); 
     but3.setBackground(Color.cyan); 
     but4.addActionListener(this); 
     but4.setBackground(Color.cyan); 
     but5.addActionListener(this); 
     but5.setBackground(Color.cyan); 
     but6.addActionListener(this); 
     but6.setBackground(Color.cyan); 
     but7.addActionListener(this); 
     but7.setBackground(Color.cyan); 
     but8.addActionListener(this); 
     but8.setBackground(Color.cyan); 
     but9.addActionListener(this); 
     but9.setBackground(Color.cyan); 
     but0.addActionListener(this); 
     but0.setBackground(Color.cyan); 

     butadd.addActionListener(this); 
     butadd.setBackground(Color.cyan); 
     butsub.addActionListener(this); 
     butsub.setBackground(Color.cyan); 
     butmulti.addActionListener(this); 
     butmulti.setBackground(Color.cyan); 
     butdiv.addActionListener(this); 
     butdiv.setBackground(Color.cyan); 
     buteq.addActionListener(this); 
     buteq.setBackground(Color.cyan); 
     butclear.addActionListener(this); 
     butclear.setBackground(Color.cyan); 
    } 

    @Override 
    public void actionPerformed(ActionEvent e) { 

     Object source = e.getSource(); 

     if (source == butclear) { 
      number1 = 0.0; 
      number2 = 0.0; 
      text.setText(""); 
     } 

     if (source == but1) { 
      text.append("1"); 
     } 
     if (source == but2) { 
      text.append("2"); 
     } 
     if (source == but3) { 
      text.append("3"); 
     } 
     if (source == but4) { 
      text.append("4"); 
     } 
     if (source == but5) { 
      text.append("5"); 
     } 
     if (source == but6) { 
      text.append("6"); 
     } 
     if (source == but7) { 
      text.append("7"); 
     } 
     if (source == but8) { 
      text.append("8"); 
     } 
     if (source == but9) { 
      text.append("9"); 
     } 
     if (source == but0) { 
      text.append("0"); 
     } 
     if (source == butadd) { 
      number1 = number_reader(); 
      text.setText(""); 
      addc = 1; 
      subc = 0; 
      multic = 0; 
      divc = 0; 
     } 
     if (source == butsub) { 
      number1 = number_reader(); 
      text.setText(""); 
      addc = 0; 
      subc = 1; 
      multic = 0; 
      divc = 0; 
     } 
     if (source == butmulti) { 
      number1 = number_reader(); 
      text.setText(""); 
      addc = 0; 
      subc = 0; 
      multic = 1; 
      divc = 0; 
     } 
     if (source == butdiv) { 
      number1 = number_reader(); 
      text.setText(""); 
      addc = 0; 
      subc = 0; 
      multic = 0; 
      divc = 1; 
     } 

     if (source == buteq) { 
      number2 = number_reader(); 
      if (addc == 1) { 
       result = number1 + number2; 
       text.setText(Double.toString(result)); 
      } 
      if (subc == 1) { 
       result = number1 - number2; 
       text.setText(Double.toString(result)); 

      } 
      if (multic == 1) { 
       result = number1 * number2; 
       text.setText(Double.toString(result)); 

      } 
      if (divc == 1) { 
       result = number1/number2; 
       text.setText(Double.toString(result)); 
      } 
     } 
    } 

    public double number_reader() { 
     Double num1; 
     String s; 
     s = text.getText(); 
     num1 = Double.valueOf(s); 

     return num1; 
    } 
} 
+0

不應該在這裏發佈作業。你有沒有嘗試過使用可靠的谷歌? –

+2

Ofc我有,我知道它的功課,但在詢問我的同學,並沒有在谷歌上找到答案後,這是我的最後手段,我不喜歡「欺騙」但我的程序反正,它只是不是100%完美,我可能會被評爲10/10,但我想知道如何解決這個問題,所以我可以從中學到一些東西。 – Rockeer

回答

4

當用戶按下=並打印的結果,則應一個標誌設置爲真,從而使數字按鈕被按下接下來的時間,其結果是第一擦除。

事實上,你甚至應該改變你的整個邏輯成state diagram,因爲如果,例如,用戶按下+兩次,你應該顯示錯誤消息或忽略第二+,而不是讀取當前數目。每個狀態應該定義當按下任何按鈕時會發生什麼,以及下一個狀態取決於按下哪個按鈕。由於GUI可以比真實計算器更智能,因此每個狀態還可以定義哪些按鈕被啓用,哪些被禁用。

+0

非常感謝你,我甚至都沒有編輯=,我經常試圖編輯按鈕,出於某種奇怪的原因,謝謝你指點我在正確的方向:) – Rockeer