英文:
I cant find the solution to my code 0*2=2 or 0/2=2 on a calculator
问题
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Toolkit;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.SwingConstants;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JLabel;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;
public class Calculator {
private JFrame frame;
private JButton btnX;
private JButton button_1, button;
private JButton button_2;
private JLabel label;
private JLabel label2;
private JButton button_3;
private JButton button_4;
private JButton button_5;
private JButton button_6;
private JButton button_7;
private JButton button_8;
private JButton button_9, button_15;
private JButton button_11, button_14;
private JButton button_12, button_16;
private JButton button_13, button_17;
private JButton button_22;
private Double result = 0.0;
private Double inverse;
private Double squareRoot;
private Double square;
private Double percent;
private boolean newin = true;
private String arg = "";
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Calculator window = new Calculator();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Calculator() {
initialize();
}
private void initialize() {
ImageIcon image = new ImageIcon("D:/Programming/MyApps/src/1440353685_MB__calculator.png");
frame = new JFrame();
KeyListener l = new KeyListener() {
// KeyListener code here...
};
frame.setResizable(false);
frame.setBounds(100, 100, 289, 452);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Calculator");
frame.addKeyListener(l);
frame.setIconImage(image.getImage());
frame.getContentPane().setLayout(null);
Border border = LineBorder.createGrayLineBorder();
button = new JButton("1");
// ActionListener for button "1"
frame.getContentPane().add(button);
// More buttons and their ActionListeners...
button_22 = new JButton("1/x");
// ActionListener for button "1/x"
frame.getContentPane().add(button_22);
// Rest of the buttons...
}
private void calculate() {
// Calculation logic...
}
private String removeZeros(Double n) {
// Removing zeros logic...
}
private String reverseString(String input) {
// Reversing a string logic...
}
}
Note: The above code includes only the translated Java code from your original input. It doesn't include explanations or comments.
英文:
Im having a problem in the calculate(). When i open up the calculator when i press 0 - 2 or 0*2 or 0/2 it always results to the number what i have entered before the zero.
If i dont do that if else code the first number when i open up my calculator doesnt count. Please help me im a newbie when it comes to programming.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Toolkit;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.SwingConstants;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JLabel;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;
public class Calculator{
private JFrame frame;
private JButton btnX;
private JButton button_1, button;
private JButton button_2;
private JLabel label;
private JLabel label2;
private JButton button_3;
private JButton button_4;
private JButton button_5;
private JButton button_6;
private JButton button_7;
private JButton button_8;
private JButton button_9, button_15;
private JButton button_11, button_14;
private JButton button_12, button_16;
private JButton button_13, button_17;
private JButton button_22;
private Double result = 0.0;
private Double inverse;
private Double squareRoot;
private Double square;
private Double percent;
private boolean newin = true;
private String arg = "";
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Calculator window = new Calculator();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Calculator() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
ImageIcon image = new ImageIcon("D:/Programming/MyApps/src/1440353685_MB__calculator.png");
frame = new JFrame();
KeyListener l = new KeyListener() {
@Override
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_0 :
case KeyEvent.VK_NUMPAD0 : button_9.doClick();
break;
case KeyEvent.VK_NUMPAD1 :
case KeyEvent.VK_1 : button.doClick();
break;
case KeyEvent.VK_2 :
case KeyEvent.VK_NUMPAD2 : button_1.doClick();
break;
case KeyEvent.VK_3 :
case KeyEvent.VK_NUMPAD3 : button_2.doClick();
break;
case KeyEvent.VK_4 :
case KeyEvent.VK_NUMPAD4 : button_3.doClick();
break;
case KeyEvent.VK_5 :
case KeyEvent.VK_NUMPAD5 : button_4.doClick();
break;
case KeyEvent.VK_6 :
case KeyEvent.VK_NUMPAD6 : button_5.doClick();
break;
case KeyEvent.VK_7 :
case KeyEvent.VK_NUMPAD7 : button_6.doClick();
break;
case KeyEvent.VK_8 : if (e.isShiftDown()) button_14.doClick();
else button_7.doClick();
break;
case KeyEvent.VK_NUMPAD8 : button_7.doClick();
break;
case KeyEvent.VK_9 :
case KeyEvent.VK_NUMPAD9 : button_8.doClick();
break;
case KeyEvent.VK_PERIOD :
case KeyEvent.VK_DECIMAL : button_13.doClick();
break;
case KeyEvent.VK_EQUALS : if(e.isShiftDown()) button_12.doClick();
else button_17.doClick();
break;
case KeyEvent.VK_ENTER : button_17.doClick();
break;
case KeyEvent.VK_MINUS :
case KeyEvent.VK_SUBTRACT : button_11.doClick();
break;
case KeyEvent.VK_C : button_16.doClick();
break;
case KeyEvent.VK_ADD : button_12.doClick();
break;
case KeyEvent.VK_MULTIPLY : button_14.doClick();
break;
case KeyEvent.VK_DIVIDE :
case KeyEvent.VK_SLASH : button_15.doClick();
break;
case KeyEvent.VK_BACK_SPACE : btnX.doClick();
break;
case KeyEvent.VK_AMPERSAND : button_22.doClick();
}
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
};
frame.setResizable(false);
frame.setBounds(100, 100, 289, 452);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Calculator");
frame.addKeyListener(l);
frame.setIconImage(image.getImage());
frame.getContentPane().setLayout(null);
Border border = LineBorder.createGrayLineBorder();
button = new JButton("1");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if (label.getText().length() == 22) {
Toolkit.getDefaultToolkit().beep();
return;
}
if (label.getText().equals("0") || newin) label.setText("");
label.setText(label.getText() + "1");
newin = false;
}
});
button.setBounds(10, 68, 47, 34);
button.setFocusable(false);
button.setContentAreaFilled(false);
frame.getContentPane().add(button);
btnX = new JButton("<X");
btnX.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (label.getText().length() > 0 && !label.getText().equals("0")) {
label.setText(label.getText().substring(0, label.getText().length()-1));
}
if (label.getText().length() == 0) label.setText("0");
}
});
btnX.setBounds(224, 68, 49, 34);
btnX.setFocusable(false);
btnX.setToolTipText("Press \"Backspace\" instead");
btnX.setContentAreaFilled(false);
frame.getContentPane().add(btnX);
button_1 = new JButton("2");
button_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (label.getText().length() == 22) {
Toolkit.getDefaultToolkit().beep();
return;
}
if (label.getText().equals("0") || newin) label.setText("");
label.setText(label.getText() + "2");
newin = false;
}
});
button_1.setBounds(59, 68, 47, 34);
button_1.setFocusable(false);
button_1.setContentAreaFilled(false);
frame.getContentPane().add(button_1);
button_2 = new JButton("3");
button_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (label.getText().length() == 22) {
Toolkit.getDefaultToolkit().beep();
return;
}
if (label.getText().equals("0") || newin) label.setText("");
label.setText(label.getText() + "3");
newin = false;
}
});
button_2.setBounds(108, 68, 47, 34);
button_2.setFocusable(false);
button_2.setContentAreaFilled(false);
frame.getContentPane().add(button_2);
label2 = new JLabel("");
label2.setBounds(10, 15, 260, 14);
label2.setOpaque(true);
label2.setBackground(Color.WHITE);
label2.setFont(new Font("Arial", Font.BOLD, 10));
label2.setHorizontalAlignment(SwingConstants.RIGHT);
label = new JLabel("0");
label.setHorizontalAlignment(SwingConstants.RIGHT);
label.setVerticalAlignment(SwingConstants.BOTTOM);
label.setOpaque(true);
label.setBackground(Color.white);
label.setBorder(border);
label.setLayout(new BorderLayout());
label.setFont(new Font("Arial", Font.BOLD, 20));
label.add(label2, BorderLayout.PAGE_START);
label.setBounds(10, 11, 265, 42);
frame.getContentPane().add(label);
button_3 = new JButton("4");
button_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if (label.getText().length() == 22) {
Toolkit.getDefaultToolkit().beep();
return;
}
if (label.getText().equals("0")) label.setText("");
label.setText(label.getText() + "4");
newin = false;
}
});
button_3.setBounds(10, 113, 47, 34);
button_3.setFocusable(false);
button_3.setContentAreaFilled(false);
frame.getContentPane().add(button_3);
button_4 = new JButton("5");
button_4.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (label.getText().length() == 22) {
Toolkit.getDefaultToolkit().beep();
return;
}
if (label.getText().equals("0") || newin) label.setText("");
label.setText(label.getText() + "5");
newin = false;
}
});
button_4.setBounds(59, 113, 47, 34);
button_4.setFocusable(false);
button_4.setContentAreaFilled(false);
frame.getContentPane().add(button_4);
button_5 = new JButton("6");
button_5.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (label.getText().length() == 22) {
Toolkit.getDefaultToolkit().beep();
return;
}
if (label.getText().equals("0") || newin) label.setText("");
label.setText(label.getText() + "6");
newin = false;
}
});
button_5.setBounds(108, 113, 47, 34);
button_5.setFocusable(false);
button_5.setContentAreaFilled(false);
frame.getContentPane().add(button_5);
button_6 = new JButton("7");
button_6.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (label.getText().length() == 22) {
Toolkit.getDefaultToolkit().beep();
return;
}
if (label.getText().equals("0") || newin) label.setText("");
label.setText(label.getText() + "7");
newin = false;
}
});
button_6.setBounds(10, 160, 47, 34);
button_6.setFocusable(false);
button_6.setContentAreaFilled(false);
frame.getContentPane().add(button_6);
button_7 = new JButton("8");
button_7.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (label.getText().length() == 22) {
Toolkit.getDefaultToolkit().beep();
return;
}
if (label.getText().equals("0") || newin) label.setText("");
label.setText(label.getText() + "8");
newin = false;
}
});
button_7.setBounds(59, 160, 47, 34);
button_7.setFocusable(false);
button_7.setContentAreaFilled(false);
frame.getContentPane().add(button_7);
button_8 = new JButton("9");
button_8.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (label.getText().length() == 22) {
Toolkit.getDefaultToolkit().beep();
return;
}
if (label.getText().equals("0") || newin) label.setText("");
label.setText(label.getText() + "9");
newin = false;
}
});
button_8.setBounds(108, 160, 47, 34);
button_8.setFocusable(false);
button_8.setContentAreaFilled(false);
frame.getContentPane().add(button_8);
button_9 = new JButton("0");
button_9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (label.getText().length() == 22) {
Toolkit.getDefaultToolkit().beep();
return;
}
if (label.getText().equals("0") || newin) label.setText("");
label.setText(label.getText() + "0");
newin = false;
}
});
button_9.setFocusable(false);
button_9.setContentAreaFilled(false);
button_9.setBounds(59, 205, 47, 34);
frame.getContentPane().add(button_9);
JButton button_10 = new JButton("±");
button_10.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if (label.getText().equals("0")) return;
if (label.getText().substring(0, 1).equals("-")) label.setText(label.getText().substring(1));
else label.setText("-" + label.getText());
}
});
button_10.setFocusable(false);
button_10.setContentAreaFilled(false);
button_10.setBounds(165, 68, 49, 34);
frame.getContentPane().add(button_10);
button_11 = new JButton("-");
button_11.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (newin && !label2.getText().equals("")) {
String in = reverseString(label2.getText());
in = in.substring(1);
in = "-" + in;
label2.setText("");
label2.setText(reverseString(in));
arg = "-";
} else {
label2.setText(label2.getText() + " " + label.getText() + " -");
calculate();
arg = "-";
label.setText(removeZeros(result));
newin = true;
}
}
});
button_11.setFocusable(false);
button_11.setContentAreaFilled(false);
button_11.setBounds(165, 113, 49, 34);
frame.getContentPane().add(button_11);
button_12 = new JButton("+");
button_12.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (newin && !label2.getText().equals("")) {
String in = reverseString(label2.getText());
in = in.substring(1);
in = "+" + in;
label2.setText("");
label2.setText(reverseString(in));
arg = "+";
} else {
label2.setText(label2.getText() + " " + label.getText() + " +");
calculate();
arg = "+";
label.setText(removeZeros(result));
newin = true;
}
}
});
button_12.setFocusable(false);
button_12.setContentAreaFilled(false);
button_12.setBounds(165, 157, 49, 82);
frame.getContentPane().add(button_12);
button_13 = new JButton(".");
button_13.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
for (int i = 0; i < label.getText().length(); i++) {
char a = label.getText().charAt(i);
if (a == '.') return;
}
label.setText(label.getText() + ".");
}
});
button_13.setFocusable(false);
button_13.setContentAreaFilled(false);
button_13.setBounds(108, 205, 47, 34);
frame.getContentPane().add(button_13);
button_14 = new JButton("x");
button_14.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if (newin && !label2.getText().equals("")) {
String in = reverseString(label2.getText());
in = in.substring(1);
in = "x" + in;
label2.setText("");
label2.setText(reverseString(in));
arg = "*";
} else {
label2.setText(label2.getText() + " " + label.getText() + " x");
calculate();
arg = "*";
label.setText(removeZeros(result));
newin = true;
}
}
});
button_14.setFocusable(false);
button_14.setContentAreaFilled(false);
button_14.setBounds(224, 160, 49, 34);
frame.getContentPane().add(button_14);
button_15 = new JButton("/");
button_15.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if (newin && !label2.getText().equals("")) {
String in = reverseString(label2.getText());
in = in.substring(1);
in = "/" + in;
label2.setText("");
label2.setText(reverseString(in));
arg = "/";
} else {
label2.setText(label2.getText() + " " +
removeZeros(Double.valueOf(label.getText())) + " /");
calculate();
arg = "/";
label.setText(removeZeros(result));
newin = true;
}
}
});
button_15.setFocusable(false);
button_15.setContentAreaFilled(false);
button_15.setBounds(224, 205, 49, 34);
frame.getContentPane().add(button_15);
button_16 = new JButton("C");
button_16.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
label.setText("0");
label2.setText("");
result = 0.0;
arg = "";
newin = false;
}
});
button_16.setFocusable(false);
button_16.setToolTipText("Press \"C\" instead");
button_16.setContentAreaFilled(false);
button_16.setBounds(224, 113, 49, 34);
frame.getContentPane().add(button_16);
button_17 = new JButton("=");
button_17.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
calculate();
label.setText(removeZeros(result));
label2.setText("");
arg = "";
newin = true;
result = 0.0;
}
});
button_17.setFocusable(false);
button_17.setContentAreaFilled(false);
button_17.setBounds(10, 205, 47, 34);
frame.getContentPane().add(button_17);
button_22 = new JButton("1/x");
button_22.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
label2.setText(("reciproc"+ "("+label.getText()+")"));
inverse = 1/Double.parseDouble(label.getText());
label.setText(removeZeros(inverse));
}
});
button_22.setFocusable(false);
button_22.setContentAreaFilled(false);
button_22.setBounds(76, 270, 63, 60);
frame.getContentPane().add(button_22);
}
private void calculate() {
if (result == 0) {
result = Double.valueOf(label.getText());
return;
}
switch (arg) {
case "+" : result += Double.parseDouble(label.getText());
break;
case "-" : result -= Double.parseDouble(label.getText());
break;
case "*" : result *= Double.valueOf(label.getText());
break;
case "/" : result /= Double.valueOf(label.getText());
break;
}
}
private String removeZeros(Double n) {
String in = String.valueOf(n);
while (in.indexOf(".") != -1) {
if (in.substring(in.length() - 1).equals("0") ||
in.substring(in.length() - 1).equals("."))
in = in.substring(0, in.length() - 1);
else break;
}
return in;
}
private String reverseString(String input) {
String output = "";
for (int i = input.length() - 1; i > -1; i--) {
output += input.charAt(i);
}
return output;
}
}
答案1
得分: 0
问题是,当您输入一个零,后面跟着任何操作,然后输入一个操作数时,您的 'result' 变量为零,因此 'calculate()' 将这种情况视为您尚未输入任何内容,只是将 'result' 设置为新操作数,而不是执行 '0 (operation) (operand)'。
您可以通过添加一个新的布尔变量 'isClear' 来解决这个问题,最初将其设置为 true。每次按下带有某个操作的按钮时,将此变量设置为 false。在您的 calculate() 方法中,将
if (result == 0) {
result = Double.valueOf(label.getText());
return;
}
替换为
if (isClear) {
result = Double.valueOf(label.getText());
return;
}
当您按下 'C' 按钮以清除输入时,还应将 'isClear' 设置为 true。
英文:
The problem is that when you enter a zero followed by any operation and then enter an operand, then your 'result' variable is zero, so 'calculate()' treat this situation like you haven't entered anything yet, and just sets 'result' to the new operand instead of performing '0 (operation) (operand)'.
You can solve it if you add a new boolean variable 'isClear', which you initially set to true. Each time you press a button with some operation you set this variable to false. In your calculate() method replace
if (result == 0) {
result = Double.valueOf(label.getText());
return;
}
with
if (isClear) {
result = Double.valueOf(label.getText());
return;
}
You should also set 'isClear' to true when you press 'C' button to clear the input.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论