台灣最大程式設計社群網站
線上人數
566
 
會員總數:231128
接案會員:6781
文章總數:2319
討論主題:175376
歡迎您免費加入會員
討論區列表 >> Java >> 用JAVA寫小算盤有加減乘除
[變換順序]
[我要回覆]
1


回應主題 加入我的關注話題 檢舉此篇討論 將提問者加入個人黑名單
用JAVA寫小算盤有加減乘除
價值 : 10 QP  點閱數:3782 回應數:2

樓主

mastime
門外漢
0 3
28 2
發送站內信



本篇文章發表於2008-12-04 23:22
別忘捐VP感謝幫助你的人 新手會員瞧一瞧
1樓
作者回應

mastime
檢舉此回應
主要是if的問題在計算的問題

本篇文章回覆於2008-12-04 23:24
--未登入的會員無法查看對方簽名檔--
2樓
回應

joe
檢舉此回應
<textarea name="code" class="java">
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MAIN extends WindowAdapter implements ActionListener{
private static TextField TFOutput;
public int number1=0,number2=0;
public static void main(String[] args) {

MAIN myMAIN = new MAIN();

JFrame frm = new JFrame("Frame with Controls");
frm.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

GridBagLayout gb = new GridBagLayout();
GridBagConstraints c;
frm.setLayout(gb);
// Variable
int gridx, gridy, gridwidth, gridheight, anchor, fill, ipadx, ipady;
double weightx, weighty;
Insets insets;

// TextField
TFOutput = new TextField("0", 100);
gridx = 0; gridy = 0;
gridwidth = 4; gridheight = 1;
weightx = 1; weighty = 1;
anchor = GridBagConstraints.CENTER;
fill = GridBagConstraints.BOTH;
insets = new Insets(10, 10, 0, 10);
ipadx = 0; ipady = 0;
c = new GridBagConstraints(gridx, gridy, gridwidth, gridheight,
weightx, weighty, anchor, fill, insets, ipadx, ipady);
gb.setConstraints(TFOutput, c);
frm.add(TFOutput);
TFOutput.setForeground(Color.BLACK);

// frm.add(a);
// frm.setLayout(new GridLayout(4, 3));
Button[] btn = new Button[17];
for (int i=1; i<10; i++) {
btn[i] = new Button(""+i);
gridx = (i-1)%3; gridy = (i-1)/3+1;
gridwidth = 1;
// gridwidth = 1; gridheight = 1;
// weightx = 1; weighty = 1;
// anchor = GridBagConstraints.CENTER;
// fill = GridBagConstraints.BOTH;
// insets = new Insets(10, 10, 10, 10);
// ipadx = 0; ipady = 0;
c = new GridBagConstraints(gridx, gridy, gridwidth, gridheight,
weightx, weighty, anchor, fill, insets, ipadx, ipady);

gb.setConstraints(btn[i], c);
frm.add(btn[i]);
}

// +
btn[10] = new Button("+");
gridx = 3; gridy = 1;
c = new GridBagConstraints(gridx, gridy, gridwidth, gridheight,
weightx, weighty, anchor, fill, insets, ipadx, ipady);
gb.setConstraints(btn[10], c);
frm.add(btn[10]);
btn[10].setForeground(Color.red);

// 0
btn[0] = new Button("0");
gridx = 0; gridy = 4;
c = new GridBagConstraints(gridx, gridy, gridwidth, gridheight,
weightx, weighty, anchor, fill, insets, ipadx, ipady);
gb.setConstraints(btn[0], c);
frm.add(btn[0]);

// =
btn[11] = new Button("=");
gridx = 2; gridy = 4;
c = new GridBagConstraints(gridx, gridy, gridwidth, gridheight,
weightx, weighty, anchor, fill, insets, ipadx, ipady);
gb.setConstraints(btn[11], c);
frm.add(btn[11]);
btn[11].setForeground(Color.red);
//*
btn[12] = new Button("*");
gridx = 3; gridy = 2;
c = new GridBagConstraints(gridx, gridy, gridwidth, gridheight,
weightx, weighty, anchor, fill, insets, ipadx, ipady);
gb.setConstraints(btn[12], c);
frm.add(btn[12]);
btn[12].setForeground(Color.red);
///
btn[13] = new Button("/");
gridx = 3; gridy = 3;
c = new GridBagConstraints(gridx, gridy, gridwidth, gridheight,
weightx, weighty, anchor, fill, insets, ipadx, ipady);
gb.setConstraints(btn[13], c);
frm.add(btn[13]);
btn[13].setForeground(Color.red);
//-
btn[14] = new Button("-");
gridx = 3; gridy = 4;
c = new GridBagConstraints(gridx, gridy, gridwidth, gridheight,
weightx, weighty, anchor, fill, insets, ipadx, ipady);
gb.setConstraints(btn[14], c);
frm.add(btn[14]);
btn[14].setForeground(Color.red);
//c

btn[15] = new Button("c");
gridx = 1; gridy = 4;
c = new GridBagConstraints(gridx, gridy, gridwidth, gridheight,
weightx, weighty, anchor, fill, insets, ipadx, ipady);
gb.setConstraints(btn[15], c);
frm.add(btn[15]);
btn[15].setForeground(Color.red);


frm.setBounds(100, 100, 300, 300);
frm.setVisible(true);

// 攔event
frm.addWindowListener(myMAIN);
for (int j=0; j<17;j++)
btn[j].addActionListener(myMAIN);

}

public void windowClosing(WindowEvent e)
{
System.exit(0);
}
public void actionPerformed(ActionEvent e)
{



String buttonString = e.getActionCommand();

if(buttonString=="+")
{
number1=Integer.parseInt(TFOutput.getText());
TFOutput.setText("0");
}
else if(buttonString=="="){
int number2=Integer.parseInt(TFOutput.getText());
TFOutput.setText(Integer.toString(number1+number2));
}
if(buttonString=="c"){
number1=Integer.parseInt(TFOutput.getText());
TFOutput.setText("0");
}

if(buttonString=="-"){
number1=Integer.parseInt(TFOutput.getText());
TFOutput.setText("0");
}
else if(buttonString=="="){
int number2=Integer.parseInt(TFOutput.getText());
TFOutput.setText(Integer.toString(number1-number2));
}

if(buttonString=="*"){
number1=Integer.parseInt(TFOutput.getText());
TFOutput.setText("0");
}
else if(buttonString=="="){
int number2=Integer.parseInt(TFOutput.getText());
TFOutput.setText(Integer.toString(number1*number2));
}
if(buttonString=="/"){
number1=Integer.parseInt(TFOutput.getText());
TFOutput.setText("0");
}
else if(buttonString=="="){
int number2=Integer.parseInt(TFOutput.getText());
TFOutput.setText(Integer.toString(number1/number2));
}
else
TFOutput.setText(Integer.toString(Integer.parseInt(TFOutput.getText()+buttonString)));
}
}

</textarea>
我只能幫你把IF問題解決,我執行後是編譯可以但你的運算還是有問題


本篇文章回覆於2009-02-26 00:08
--未登入的會員無法查看對方簽名檔--
[變換順序]
1
 

回覆
如要回應,請先登入.