2013-06-27 158 views
0

所以在我的基本Java計算器代碼中,它似乎代碼只運行第一次。我無法找到問題的根源,但它可能與清除功能有關。我真的是Java的noob,並會感謝來自更有經驗的編程人員的任何幫助!計算器只運行第一次

package Calculator; 

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

public class Calculator extends JFrame implements ActionListener { 

    /** 
    * 
    */ 
    private static final long serialVersionUID = 1L; 
    JButton num1, num2, num3, num4, num5, num6, num7, num8, num9, num0, add, sub, div, times, equals, exit, point, reset; 
    JTextField txtfld; 
    static String s = "", func = ""; 
    int flag = 0; 
    static double total1; 
    double first, second; 

    Double toDouble(String a) { 
     double i = Double.parseDouble(a); 
     return i; 
    } 

    void total(double first, double second, String func) { 
     String total; 



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

      total1 = first + second; 
      // System.out.println(first+"\n"+second+"\n"+sum); 
      total = Double.toString(total1); 
      txtfld.setText(total); 
     } else if ("-".equals(func)) { 
      total1 = first - second; 
      total = Double.toString(total1); 
      txtfld.setText(total); 
     } else if ("*".equals(func)) { 
      total1 = first * second; 
      total = Double.toString(total1); 
      txtfld.setText(total); 
     } else if ("/".equals(func)) { 
      total1 = first/second; 
      total = Double.toString(total1); 
      txtfld.setText(total); 
     } 
    } 

    public Calculator() { 
     Container content = getContentPane(); 
     content.setLayout(new FlowLayout()); 
     JLabel jl = new JLabel("    Java GUI Calculator    "); 
     txtfld = new JTextField(15); 
     num1 = new JButton(" 1 "); 
     num2 = new JButton(" 2 "); 
     num3 = new JButton(" 3 "); 
     num4 = new JButton(" 4 "); 
     num5 = new JButton(" 5 "); 
     num6 = new JButton(" 6 "); 
     num7 = new JButton(" 7 "); 
     num8 = new JButton(" 8 "); 
     num9 = new JButton(" 9 "); 
     num0 = new JButton(" 0 "); 
     add = new JButton(" + "); 
     sub = new JButton(" - "); 
     div = new JButton("/"); 
     times = new JButton(" * "); 
     equals = new JButton(" = "); 
     exit = new JButton(" Exit  "); 
     point = new JButton(" . "); 
     reset = new JButton("C"); 
     reset.setBackground(Color.RED); 
     num1.addActionListener(this); 
     num2.addActionListener(this); 
     num3.addActionListener(this); 
     num4.addActionListener(this); 
     num5.addActionListener(this); 
     num6.addActionListener(this); 
     num7.addActionListener(this); 
     num8.addActionListener(this); 
     num9.addActionListener(this); 
     num0.addActionListener(this); 
     add.addActionListener(this); 
     sub.addActionListener(this); 
     times.addActionListener(this); 
     div.addActionListener(this); 
     equals.addActionListener(this); 
     exit.addActionListener(this); 
     point.addActionListener(this); 
     reset.addActionListener(this); 
     content.add(jl); 
     content.add(txtfld); 
     content.add(num1); 
     content.add(num2); 
     content.add(num3); 
     content.add(add); 
     content.add(num4); 
     content.add(num5); 
     content.add(num6); 
     content.add(sub); 
     content.add(num7); 
     content.add(num8); 
     content.add(num9); 
     content.add(div); 
     content.add(num0); 
     content.add(point); 


     content.add(times); 
     content.add(equals); 
     content.add(reset); 
     content.add(exit); 
    } 

    public static void main(String arg[]) { 
     Calculator jf = new Calculator(); 
     jf.setSize(230, 250); 
     jf.setVisible(true); 


    } 

    public void actionPerformed(ActionEvent e) { 
     Object o = e.getSource(); 

     if (o == num1) { 
      txtfld.setText(s.concat("1")); 
      s = txtfld.getText(); 
     } else if (o == num2) { 
      txtfld.setText(s.concat("2")); 
      s = txtfld.getText(); 
     } else if (o == num3) { 
      txtfld.setText(s.concat("3")); 
      s = txtfld.getText(); 
     } else if (o == num4) { 
      txtfld.setText(s.concat("4")); 
      s = txtfld.getText(); 
     } else if (o == num5) { 
      txtfld.setText(s.concat("5")); 
      s = txtfld.getText(); 
     } else if (o == num6) { 
      txtfld.setText(s.concat("6")); 
      s = txtfld.getText(); 
     } else if (o == num7) { 
      txtfld.setText(s.concat("7")); 
      s = txtfld.getText(); 
     } else if (o == num8) { 
      txtfld.setText(s.concat("8")); 
      s = txtfld.getText(); 
     } else if (o == num9) { 
      txtfld.setText(s.concat("9")); 
      s = txtfld.getText(); 
     } else if (o == num0) { 
      txtfld.setText(s.concat("0")); 
      s = txtfld.getText(); 
     } else if (o == add) { 

      txtfld.setText(""); 
      first = toDouble(s); 
      System.out.println(first); 
      s = ""; 
      func = "+"; 
     } else if (o == sub) { 
      txtfld.setText(""); 
      first = toDouble(s); 
      s = ""; 
      func = "-"; 
     } else if (o == times) { 

      txtfld.setText(""); 
      first = toDouble(s); 
      s = ""; 
      func = "*"; 
     } else if (o == div) { 
      txtfld.setText(""); 
      first = toDouble(s); 
      s = ""; 
      func = "/"; 
     } else if (o == equals) { 
      if (flag == 0) { 
       second = toDouble(s); 
       total(first, second, func); 
       flag = 1; 
      } else if (flag == 1) { 
       second = toDouble(s); 
       total(total1, second, func); 
      } 
      System.out.println(first); 
     } else if (o == exit) { 
      System.exit(0); 
     } else if (o == point) { 
      txtfld.setText(s.concat(".")); 
      s = txtfld.getText(); 
     } 
     if (o == reset) { 
      total1 = 0; 
      txtfld.setText(""); 
      s = txtfld.getText(); 
     } 
    } 
} 
+0

有一個在你的代碼中沒有明確的功能。 – JHS

+0

它在代碼 – Ted

回答

2

試試這個它的事業,完善

/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package com.java.util.test; 

import java.text.DateFormat; 
import java.text.SimpleDateFormat; 
import java.util.HashMap; 
import javax.swing.JFrame; 

/** 
* 
* @author shreyansh.jogi 
*/ 
import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.*; 

public class JavaApplication2 extends JFrame implements ActionListener { 

    /** 
    * 
    */ 
    private static final long serialVersionUID = 1L; 
    JButton num1, num2, num3, num4, num5, num6, num7, num8, num9, num0, add, sub, div, mult, equalto, exit, point, reset; 
    JTextField txtfld; 
    String s = "", ope = ""; 
    int flag = 0; 
    double total1; 
    double first, second; 

    Double toDouble(String a) { 
     double i = Double.parseDouble(a); 
     return i; 
    } 

    void total(double first, double second, String ope) { 
     String total; 

     if (ope.equalsIgnoreCase("+")) { 

      total1 = first + second; 
      // System.out.println(first+"\n"+second+"\n"+sum); 
      total = Double.toString(total1); 
      txtfld.setText(total); 
     } else if (ope.equalsIgnoreCase("-")) { 
      total1 = first - second; 
      total = Double.toString(total1); 
      txtfld.setText(total); 
     } else if (ope.equalsIgnoreCase("*")) { 
      total1 = first * second; 
      total = Double.toString(total1); 
      txtfld.setText(total); 
     } else if (ope.equalsIgnoreCase("/")) { 

      total1 = first/second; 
      total = Double.toString(total1); 
      txtfld.setText(total); 
     } 
     clearfields(); 
    } 

    public JavaApplication2() { 
     Container content = getContentPane(); 
     content.setLayout(new FlowLayout()); 
     JLabel jl = new JLabel("   Calculator    "); 
     txtfld = new JTextField(15); 
     num1 = new JButton(" 1 "); 
     num2 = new JButton(" 2 "); 
     num3 = new JButton(" 3 "); 
     num4 = new JButton(" 4 "); 
     num5 = new JButton(" 5 "); 
     num6 = new JButton(" 6 "); 
     num7 = new JButton(" 7 "); 
     num8 = new JButton(" 8 "); 
     num9 = new JButton(" 9 "); 
     num0 = new JButton(" 0 "); 
     add = new JButton(" + "); 
     sub = new JButton(" - "); 
     div = new JButton("/"); 
     mult = new JButton(" * "); 
     equalto = new JButton(" = "); 
     exit = new JButton(" Exit  "); 
     point = new JButton(" . "); 
     reset = new JButton("C"); 
     reset.setBackground(Color.RED); 
     num1.addActionListener(this); 
     num2.addActionListener(this); 
     num3.addActionListener(this); 
     num4.addActionListener(this); 
     num5.addActionListener(this); 
     num6.addActionListener(this); 
     num7.addActionListener(this); 
     num8.addActionListener(this); 
     num9.addActionListener(this); 
     num0.addActionListener(this); 
     add.addActionListener(this); 
     sub.addActionListener(this); 
     mult.addActionListener(this); 
     div.addActionListener(this); 
     equalto.addActionListener(this); 
     exit.addActionListener(this); 
     point.addActionListener(this); 
     reset.addActionListener(this); 
     content.add(jl); 
     content.add(txtfld); 
     content.add(num1); 
     content.add(num2); 
     content.add(num3); 
     content.add(add); 
     content.add(num4); 
     content.add(num5); 
     content.add(num6); 
     content.add(sub); 
     content.add(num7); 
     content.add(num8); 
     content.add(num9); 
     content.add(div); 
     content.add(num0); 
     content.add(point); 


     content.add(mult); 
     content.add(equalto); 
     content.add(reset); 
     content.add(exit); 
    } 

    public static void main(String arg[]) { 
     JavaApplication2 jf = new JavaApplication2(); 
     jf.setSize(230, 250); 
     jf.setVisible(true); 


    } 

    public void actionPerformed(ActionEvent e) { 
     Object o = e.getSource(); 

     if (o == num1) { 
      txtfld.setText(s.concat("1")); 
      s = txtfld.getText(); 
     } else if (o == num2) { 
      txtfld.setText(s.concat("2")); 
      s = txtfld.getText(); 
     } else if (o == num3) { 
      txtfld.setText(s.concat("3")); 
      s = txtfld.getText(); 
     } else if (o == num4) { 
      txtfld.setText(s.concat("4")); 
      s = txtfld.getText(); 
     } else if (o == num5) { 
      txtfld.setText(s.concat("5")); 
      s = txtfld.getText(); 
     } else if (o == num6) { 
      txtfld.setText(s.concat("6")); 
      s = txtfld.getText(); 
     } else if (o == num7) { 
      txtfld.setText(s.concat("7")); 
      s = txtfld.getText(); 
     } else if (o == num8) { 
      txtfld.setText(s.concat("8")); 
      s = txtfld.getText(); 
     } else if (o == num9) { 
      txtfld.setText(s.concat("9")); 
      s = txtfld.getText(); 
     } else if (o == num0) { 
      txtfld.setText(s.concat("0")); 
      s = txtfld.getText(); 
     } else if (o == add) { 

      txtfld.setText(""); 
      first = toDouble(s); 
      System.out.println(first); 
      s = ""; 
      ope = "+"; 
     } else if (o == sub) { 
      txtfld.setText(""); 
      first = toDouble(s); 
      s = ""; 
      ope = "-"; 
     } else if (o == mult) { 

      txtfld.setText(""); 
      first = toDouble(s); 
      s = ""; 
      ope = "*"; 
     } else if (o == div) { 
      txtfld.setText(""); 
      first = toDouble(s); 
      s = ""; 
      ope = "/"; 
     } else if (o == equalto) { 
      if (flag == 0) { 
       second = toDouble(s); 
       total(first, second, ope); 
       flag = 1; 
      } else if (flag == 1) { 
       second = toDouble(s); 
       total(first, second, ope); 
      } 
      System.out.println(first); 
     } else if (o == exit) { 
      System.exit(0); 
     } else if (o == point) { 
      txtfld.setText(s.concat(".")); 
      s = txtfld.getText(); 
     } 
     if (o == reset) { 
      txtfld.setText(""); 
      s = txtfld.getText(); 
      total1 = 0; 
     } 
    } 

    private void clearfields() { 
     first=0; 
     second=0; 
     ope=""; 
     flag = 0; 
     total1 = 0; 
     s=""; 
    } 
} 


//System.out.println(stem[0]); 
+0

中被稱爲「重置」,我不確定你的意思? – Ted

+0

你已經定義了變量第一次,但第二次當你試圖運行該變量不保留任何值,因此您的操作不會執行 –

+0

感謝您的編輯,但我想出了簡單地重置第一個,第二個標誌和total1 values to = 0.一個問題的人,我將如何使它,所以數字顯示的文本框是可編輯的,並且能夠計算函數(例如+, - ,*,/)? – Ted