2010-11-19 5 views
2

자이 계산기에 문제가 있습니다. 나는 프로젝트에 배정되었으므로 더하기와 빼기 만 사용할 수 있습니다. 과제의 첫 번째 부분은 스캐너 클래스를 사용하여 과제를 작성하는 것이 었습니다. 그래서 루프를 만들고 계산기는 작동하지만 ... 곱셈과 나눗셈은 꺼져 있습니다. 기본적으로 숫자를 계속 추가합니다. 3 * 6처럼 답은 24가 될 것입니다. 그렇지만 2 * 1을하면 26이나 뭔가가 될 것입니다. 그래, 클래스를 다른 파일로 저장했습니다. ..can 누군가는 당신이 다시 0으로 값을 복원 결코 때문에 문을 실행할 경우에는 그것의 각이 때문에 각 문이 아래로 계속 실행하는 경우 때문에 생각계산기 프로젝트 문제

public class Calculator_part2 extends calculator_gui{ 

public static void main(String[]args) 
{ 
calculator_gui n = new calculator_gui(); 
n.gui(); 
} 
} 

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





public class calculator_gui implements ActionListener 
{ 
JFrame frame = new JFrame(" Calculator "); 
JPanel panel = new JPanel(new FlowLayout()); 

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 add = new JButton(" + "); 
JButton sub = new JButton(" - "); 
JButton multi = new JButton (" * "); 
JButton div = new JButton("/"); 
JButton buteq = new JButton (" = "); 

JButton butclear = new JButton("C"); 

Double number1,number2,result = 0.0,temp = 0.0; 
int addc = 0, subc=0,multic=0,divc=0,i = 0; 

public void gui() 
{ 
    frame.setVisible(true); 
    frame.setSize(250,200); 
    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(add); 
    panel.add(sub); 
    panel.add(multi); 
    panel.add(div); 
    panel.add(buteq); 
    panel.add(butclear); 

    but1.addActionListener(this); 
    but2.addActionListener(this); 
    but3.addActionListener(this); 
    but4.addActionListener(this); 
    but5.addActionListener(this); 
    but6.addActionListener(this); 
    but7.addActionListener(this); 
    but8.addActionListener(this); 
    but9.addActionListener(this); 
    but0.addActionListener(this); 
    add.addActionListener(this); 
    sub.addActionListener(this); 
    multi.addActionListener(this); 
    div.addActionListener(this); 
    buteq.addActionListener(this); 
    butclear.addActionListener(this); 


} 

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 == add) 
{ 
number1 = number_reader(); 
text.setText(""); 
addc=1; 
subc=0; 
multic=0; 
divc=0; 
} 
if(source == sub) 
{ 
number1 = number_reader(); 
text.setText(""); 
addc=0; 
subc=1; 
multic=0; 
divc=0; 
} 
if(source == multi) 
{ 
number1 = number_reader(); 
text.setText(""); 
addc=0; 
subc=0; 
multic=1; 
divc=0; 
} 
if(source == div) 
{ 
number1 = number_reader(); 
text.setText(""); 
addc=0; 
subc=0; 
multic=0; 
divc=1; 
} 

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

    for( double i = 0;i <number2;i++) 
    { 
     result += number1; 
    } 


    text.setText(Double.toString(result)); 
} 
if(divc>0) 
    { 
    for (i = 0; temp < number1; i++) 
    { 
    temp += number2; 
    } 

    text.setText(Double.toString(i)); 
} 
} 
} 

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

return num1; 

} 
} 
+0

10 개의 개별 필드 대신 Button 객체에 대해 배열을 사용하는 것은 어떻습니까? –

답변

1

performAction 하단의 결과를 0.0으로 재설정해야합니다. 또한 부서를 구현하면 i이 텍스트로 설정됩니다. 그 말이 맞지 않습니다.

+0

질문을 올바르게 읽으면 곱셈이나 나눗셈을 사용할 수 없습니다. 그렇지 않으면 +1 –

+0

@Matt Ball, 감사합니다. 그 요구 사항을 알지 못했습니다. 다른 연산자를 사용하라는 제안을 삭제합니다. –