2013-07-09 2 views
1

이 프로그램의 기능을 향상시킬 수있는 더 나은 방법이 있음을 알고 있습니다.하지만 할 수 없었습니다. 이 프로그램이하는 정확히 어떻게, 어떤 제안이 도움이됩니다 않는 더 나은 알고리즘을 마련자바를 사용하여 GUI 계산기를 만들었습니다. 변수가 최신 상태로 유지되지 않고 0으로 유지됩니다.

package calc; 

import java.awt.FlowLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JTextField; 

/** 
* 
* @author Ben 
*/ 
public class GUI extends JFrame { 

int response, count=0;  
double num1, num2, num3, num5, num6, num7, num8, total=0; 
String operation, answer, num, testnum; 
private JButton one, two, three, four, five, six, seven, eight, 
     nine, zero, multiply, divide, subtract, add, equals, clear; 
private JTextField display, fakedisplay; 

public GUI(){ 
    super("Calculator"); 
    setLayout (new FlowLayout()); 

    fakedisplay = new JTextField(10); 
    display = new JTextField(10); 
    display.setEditable(false);   
    add(display);     
    one = new JButton("1");  
    add(one); 
    two = new JButton("2"); 
    add(two); 
    three = new JButton("3"); 
    add(three); 
    four = new JButton("4"); 
    add(four); 
    five = new JButton("5"); 
    add(five); 
    six = new JButton("6"); 
    add(six); 
    seven = new JButton("7"); 
    add(seven); 
    eight = new JButton("8"); 
    add(eight); 
    nine = new JButton("9"); 
    add(nine); 
    zero = new JButton("0"); 
    add(zero);    
    multiply = new JButton("*"); 
    add(multiply);   
    divide = new JButton("/"); 
    add(divide); 
    subtract = new JButton("-"); 
    add(subtract); 
    add = new JButton("+"); 
    add(add); 
    equals = new JButton("="); 
    add(equals); 
    clear = new JButton("Clear"); 
    add(clear); 


    handler handle = new handler(); 

    one.addActionListener(handle); 
    two.addActionListener(handle); 
    three.addActionListener(handle); 
    four.addActionListener(handle); 
    five.addActionListener(handle); 
    six.addActionListener(handle); 
    seven.addActionListener(handle); 
    eight.addActionListener(handle); 
    nine.addActionListener(handle); 
    zero.addActionListener(handle); 
    multiply.addActionListener(handle); 
    divide.addActionListener(handle); 
    subtract.addActionListener(handle); 
    add.addActionListener(handle); 
    equals.addActionListener(handle); 
    clear.addActionListener(handle); 

} 
private class handler implements ActionListener{ 

    @Override 
    public void actionPerformed(ActionEvent e){ 

     if(e.getSource()==one){ 
      response = 1;    
      display.setText(display.getText() + response); 
      fakedisplay.setText(fakedisplay.getText() + response); 
     } 
     else if (e.getSource()==two){ 
      response = 2; 
      display.setText(display.getText() + response);  
      fakedisplay.setText(fakedisplay.getText() + response); 
     } 
     else if (e.getSource()==three){ 
       response = 3; 
       display.setText(display.getText() + response); 
       fakedisplay.setText(fakedisplay.getText() + response); 
     } 
     else if (e.getSource()==four){ 
       response = 4; 
       display.setText(display.getText() + response); 
       fakedisplay.setText(fakedisplay.getText() + response); 
     } 
     else if (e.getSource()==five){ 
       response = 5; 
       display.setText(display.getText() + response); 
       fakedisplay.setText(fakedisplay.getText() + response); 
     } 
     else if (e.getSource()==six){ 
       response = 6; 
       display.setText(display.getText() + response); 
       fakedisplay.setText(fakedisplay.getText() + response); 
     } 
     else if (e.getSource()==seven){ 
       response = 7; 
       display.setText(display.getText() + response); 
       fakedisplay.setText(fakedisplay.getText() + response); 
     } 
     else if (e.getSource()==eight){ 
       response = 8; 
       display.setText(display.getText() + response); 
       fakedisplay.setText(fakedisplay.getText() + response); 
     } 
     else if (e.getSource()==nine){ 
       response = 9; 
       display.setText(display.getText() + response); 
       fakedisplay.setText(fakedisplay.getText() + response); 
     } 
     else if(e.getSource()==zero){ 
      response = 0; 
      display.setText(display.getText() + response); 
      fakedisplay.setText(fakedisplay.getText() + response); 
     } 
     else if (e.getSource()==multiply){ 
      if(count == 0){ 
       num1 = Double.parseDouble(display.getText()); 
       count++; 
      }     
      else if(count == 1){ 
       num2 = Double.parseDouble(fakedisplay.getText()); 
       total = num1*num2; 
       count++; 
      } 
      else if(count == 2){ 
       num3 = Double.parseDouble(fakedisplay.getText()); 
       total = total*num3; 
       count++; 
      } 
      operation = "*"; 
      display.setText(display.getText() + operation); 
      fakedisplay.setText(""); 

     } 
     else if (e.getSource()==divide){ 
      if(count == 0){ 
       num1 = Double.parseDouble(display.getText()); 
       count++; 
      } 
      else if(count == 1){ 
       num2 = Double.parseDouble(fakedisplay.getText()); 
       total = num1/num2+num1%num2; 
       count++; 
      } 
      else if(count == 2){ 
       num3 = Double.parseDouble(fakedisplay.getText()); 
       total = total/num3+total%num3; 
       count++; 
      } 
       operation = "/";      
       display.setText(display.getText() + operation); 
       fakedisplay.setText(""); 
     } 
     else if (e.getSource()==add){ 
      if(count == 0){ 
       num1 = Double.parseDouble(display.getText()); 
       count++; 
      } 
      else if(count == 1){ 
       num2 = Double.parseDouble(fakedisplay.getText()); 
       total = num1+num2; 
       count++; 
      } 
      else if(count == 2){ 
       num3 = Double.parseDouble(fakedisplay.getText()); 
       total = total+num3; 
       count++; 
      } 
       operation = "+"; 
       display.setText(display.getText() + operation); 
       fakedisplay.setText(""); 
     } 
     else if (e.getSource()==subtract){ 
      if(count == 0){ 
       num1 = Double.parseDouble(display.getText()); 
       count++; 
      } 
      else if (count == 1) { 
       num2 = Double.parseDouble(fakedisplay.getText()); 
       total = num1-num2; 
       count++; 
      } 
      else if(count == 2){ 
       num3 = Double.parseDouble(fakedisplay.getText()); 
       total = total-num3; 
       count++; 
      } 
       operation = "-"; 
       display.setText(display.getText() + operation); 
       fakedisplay.setText(""); 
     } 
     else if (e.getSource()==equals){     
      operation = "="; 
      display.setText(display.getText() + operation + total);       
     } 
     else if (e.getSource()==clear){ 
      display.setText(""); 
     } 


} 



} 
} 
+1

은 수 있습니다 (http://stackoverflow.com/a/17259571/1057230)이 [답]를 참조하시기 바랍니다 이 도움이 될 수 있습니다 :-) –

+1

나는 [이 답변] (http://stackoverflow.com/a/7441804/418556) 달성 비록 소스 코드의 200 + 라인을 (적어도, 조심스럽게) 보지 않았다. 약 140 LOC의 작동 계산기. –

답변

3

이 내가 볼 문제의 몇 가지가 있지만, 가장 먼저 필요한 것은 함께 당신을 도울 :

등호 연산을 수행하는 위치는 어디입니까? 간단한 산술 함수를 하 :

A op B = ? 

(A 및 B 시험 번호 및 영업 + 중 하나이고 -, *, /)

num1에 슬롯 번호를되지만 op 수행되지 작업 count 동일 자신을 돕기 위해 1

, 마지막 else if 블록 뒤에 다음을 추가하지 않습니다 때문에 :

System.out.println("num1: " + num1 + " num2: " + num2 + " num3: " + num3 
    + "\ndisplay: " + display.getText() + " fakedisplay: " + fakedisplay.getText() 
    + "\nresponse: " + response + "\ncount: " + count + "\ntotal: " + total); 

이것은 각 키/버튼을 누른 후에 방금 일어난 일에 대한 힌트를 제공합니다. 당신이해야 할 당신이 논리의 단계에 대해 생각하는 경우

은 다음과 같습니다

 
Create fields for leftValue, operator, rightValue. 
Handle the input just entered 
    if input is a number and leftValue is null 
     leftValue = input 
    if input is a number and leftValue (and operator) are not null 
     leftValue = leftValue OPERATION input 
    if input is an operator and leftValue is not null 
     operator = input 
+0

도움을 주셔서 감사합니다, 그 코드는 내가 정말로 실제로 백그라운드에서 무슨 일이 일어나고 있는지 보여주고 추가했습니다 감사합니다! 메신저 아직도 조금 카운트가 정확히 어떻게 될지 혼란스러워, 왜냐하면 count == 0은 디폴트가 0이고 if 문장의 끝에서 카운트가 ++이기 때문에 1까지 보내야하기 때문이다. – user2555459

+0

네,하지만 한번 치면 루프의 (do some operation) 부분으로 되돌아 가지 않습니다. '2'+ ''4 '=' '를 수행하면'+ '다음에 count = 1을 치게되지만 작업을 수행하기 위해 해당 루프를 다시 입력하지 않습니다 (count = 1 동안). 나는 당신이 '='을 치면 어떤 일이 일어나는지 생각할 필요가 있다고 생각합니다. – mcalex

+0

미안하지만, GUI와 액션 리스너를 사용하기 시작했기 때문에 미안하지만, actionListener를 사용하면 위에서 설명한 것처럼 사이클링을 유지하기 위해 프로그램을 작동시켜야합니다. 숫자가 단지 equasls 일 때 과거의 동작을 계속 유지하도록합니다. – user2555459

관련 문제