계산기 질문좀 드릴게요
난슬
이 소스를 실행하였을 경우
123456789*123456789
이런식으로 계산을 하려하면 결과값이 이상하더라구요
왜 그런가요?
가능하시다면 수정도 해주시면 감사하겠습니다import java.awt.*;
import java.awt.event.*;//이벤트 패키지 포함
import javax.swing.*;
class Calculator
{
public static void main(String args[])
{
CalculatorFrame frame = new CalculatorFrame();//자신의 클래스로 부터 객체 생성
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//프레임 종료버튼을 누르면 프레임을 닫고 프로그램을 종료
frame.setVisible(true);// 프레임을 나타낸다
frame.setSize(500,560);// 프레임 사이즈 설정
}
}
class CalculatorFrame extends JFrame//JFrame 상속
{
public CalculatorFrame()
{
setTitle(200631569 하헌영); //프레임의 타이틀을 설정
CalculatorPanel panel = new CalculatorPanel();
add(panel);
pack();
}
}
class CalculatorPanel extends JPanel
{
public CalculatorPanel()
{
setLayout(new BorderLayout());//new 컴포넌트를 프레임에 추가
result = 0;
lastCommand = =;
start = true;
display = new JLabel(0); //초기에 보여질 값 설정
display.setEnabled(false);
add(North,display);// 상단으로 초기 위치 설정
ActionListener insert = new InsertAction();
ActionListener command = new CommandAction();
panel = new JPanel();// 판넬 생성
panel.setLayout(new GridLayout(4,4));// 4*4 배열로 된 GridLayout 객체 생성
//버튼 추가
addButton(7, insert);
addButton(8, insert);
addButton(9, insert);
addButton(/, command);
addButton(4, insert);
addButton(5, insert);
addButton(6, insert);
addButton(*, command);
addButton(1, insert);
addButton(2, insert);
addButton(3, insert);
addButton(-, command);
addButton(0, insert);
addButton(., insert);
addButton(=, command);
addButton(+, command);
add(Center,panel);
}
private void addButton(String label, ActionListener listener)
{
JButton button = new JButton(label);
button.addActionListener(listener);
panel.add(button);
}
private class InsertAction implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
String input = event.getActionCommand();
if(start){
display.setText();
start = false;
}
display.setText(display.getText()+input);
}
}
private class CommandAction implements ActionListener//버튼의 이벤트 처리위하여 액션리스너 포함
{
public void actionPerformed(ActionEvent event)
{
String command = event.getActionCommand();
if(start)
{
if(command.equals())
{
display.setText(command);
start = false;
}else{
lastCommand = command;
}
}else{
calculator(Double.parseDouble(display.getText()));
lastCommand = command;
start = true;
}
}
}
public void calculator(double x){
if(lastCommand.equals(+)) result += x;
else if(lastCommand.equals(-)) result -= x;
else if(lastCommand.equals(*)) result *= x;
else if(lastCommand.equals(/)) result /= x;
else if(lastCommand.equals(=)) result = x;
display.setText( + result);
}
private JLabel display;
private JPanel panel;
private double result;
private String lastCommand;
private boolean start;
}
-
티나
강좌에 보면..있습니다 ~_~
-
푸른들
그 부분을 수정해야 하는데 어떻게 해야 하는지를 몰라서요..
-
김애교
계산값이 자료형이 표현 할 수 있는 한계치를 벗어난듯 보입니다
번호 | 제 목 | 글쓴이 | 날짜 |
---|---|---|---|
2700422 | 버전에 관해 질문 | 라온제나 | 2025-07-04 |
2700393 | mysql이 갑자기!!!!!!!!!!!!!!!에러가;; (2) | 소미 | 2025-07-04 |
2700359 | 3.0 ) SimpleButton 상태 강제 변경 (3) | 희선 | 2025-07-04 |
2700304 | ie8 전용핵 문의 (3) | 여자 | 2025-07-03 |
2700281 | 이런경우 어떻게 코딩해야 표준에 따르는건가요? (6) | 늘솜 | 2025-07-03 |
2700230 | 질문이여 ! | 뿡뿡몬 | 2025-07-03 |
2700205 | 액션스크립트책 좀 추천해주세요. (10) | 화이트캣 | 2025-07-02 |
2700173 | 자바 소스인데 어떤게 에러인지..? (1) | 호빵녀 | 2025-07-02 |
2700142 | 하단이 붙어있는 가변 레이아웃구조 질문드립니다. | 이플 | 2025-07-02 |
2700089 | 이미지를 사다리꼴로 비틀게 하는 액션코드가 있나요? (4) | 여름 | 2025-07-01 |
2700033 | 배경에 그라데이션을 넣으려고 하는데요.. (4) | 화이티 | 2025-07-01 |
2700005 | [질문] TextField 객체의 실제 높이 알아오는 방법 ? | 천사의눈물 | 2025-07-01 |
2699978 | FileReferenceList를 이용하여 업로드시 자꾸 실행속도가 느리다는 팝업이... (10) | 데이비드 | 2025-06-30 |
2699944 | 자바스크립트가 많은 사이트는... (6) | 희나리 | 2025-06-30 |
2699918 | 브라우저마다 다른 input과 텍스트 정렬 (3) | 늘봄 | 2025-06-30 |
2699887 | 동적텍스트를 그래픽으로?? (2) | 족장 | 2025-06-30 |
2699862 | scope넣기 (1) | 아인 | 2025-06-29 |
2699835 | exe로 만드는 방법을....알려주세요.. (5) | 방방 | 2025-06-29 |
2699809 | 롤오버할때 백그라운드 이미지로할때 alt설명은 어떻게해야하죠?..ㅠ (4) | 반혈 | 2025-06-29 |
2699757 | 리스트뷰에 있는 내용을 랜덤으로 추출 | 세실 | 2025-06-28 |