Hellow guys..
Jeg har lavet en low quality lommeregner, med min low quality hjerne. Jeg har bare stille og rolig lavet den, startet med plus, minus, divideret og gange, gået op til at kunne slette, clear, kvadratrod.. Så besluttede jeg mig for at lave den om til at kunne bruge decimal tal.. Det var ikke så svært, istedet for int skulle det da bare være double eller float.. Jaaa-nej, det var ikke bare lige sådan, den kan da godt regne med double, lige på nær kvadratrods funktionen.. I made something wrong, men efter at have rettet alle de fejl jeg kunne finde, er mit problem altså stadig ikke helt løst..
Nogen som kan hjælpe mig ud her?
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.JButton;
- import javax.swing.JOptionPane;
- import javax.swing.JLabel;
-
- import java.awt.Color;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
-
- public class Main extends JFrame
- {
- double first, second, result;
- String number = "";
- boolean bplus, bminus, btimes, bdivided;
-
- private JPanel panel;
- private JLabel label;
- private JButton b1;
- private JButton b2;
- private JButton b3;
- private JButton b4;
- private JButton b5;
- private JButton b6;
- private JButton b7;
- private JButton b8;
- private JButton b9;
- private JButton b0;
- private JButton equals;
- private JButton plus;
- private JButton minus;
- private JButton times;
- private JButton divided;
- private JButton squareRoot;
- private JButton c;
- private JButton delete;
- private JButton period;
-
-
- public Main()
- {
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- this.setSize(432, 880);
- this.setVisible(true);
-
- panel = new JPanel();
- panel.setLayout(null);
-
- label = new JLabel(getNumber(), null, JLabel.CENTER);
- label.setBounds(0, 0, 160, 40);
- panel.add(label);
-
- b1 = new JButton("1");
- b1.setBounds(0, 40, 50, 40);
- panel.add(b1);
-
- b1.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent arg0)
- {
- number += "1";
- label.setText(getNumber());
- }
- });
-
- b2 = new JButton("2");
- b2.setBounds(50, 40, 50, 40);
- panel.add(b2);
-
- b2.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent arg0)
- {
- number += "2";
- label.setText(getNumber());
- }
- });
-
- b3 = new JButton("3");
- b3.setBounds(100, 40, 50, 40);
- panel.add(b3);
-
- b3.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent arg0)
- {
- number += "3";
- label.setText(getNumber());
- }
- });
-
- b4 = new JButton("4");
- b4.setBounds(0, 80, 50, 40);
- panel.add(b4);
-
- b4.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent arg0)
- {
- number += "4";
- label.setText(getNumber());
- }
- });
-
- b5 = new JButton("5");
- b5.setBounds(50, 80, 50, 40);
- panel.add(b5);
-
- b5.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent arg0)
- {
- number += "5";
- label.setText(getNumber());
- }
- });
-
- b6 = new JButton("6");
- b6.setBounds(100, 80, 50, 40);
- panel.add(b6);
-
- b6.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent arg0)
- {
- number += "6";
- label.setText(getNumber());
- }
- });
-
- b7 = new JButton("7");
- b7.setBounds(0, 120, 50, 40);
- panel.add(b7);
-
- b7.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent arg0)
- {
- number += "7";
- label.setText(getNumber());
- }
- });
-
- b8 = new JButton("8");
- b8.setBounds(50, 120, 50, 40);
- panel.add(b8);
-
- b8.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent arg0)
- {
- number += "8";
- label.setText(getNumber());
- }
- });
-
- b9 = new JButton("9");
- b9.setBounds(100, 120, 50, 40);
- panel.add(b9);
-
- b9.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent arg0)
- {
- number += "9";
- label.setText(getNumber());
- }
- });
-
- b0 = new JButton("0");
- b0.setBounds(50, 160, 50, 40);
- panel.add(b0);
-
- b0.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent arg0)
- {
- number += "0";
- label.setText(getNumber());
- }
- });
-
- plus = new JButton("+");
- plus.setBounds(150, 160, 50, 40);
- panel.add(plus);
-
- plus.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent e)
- {
- label.setText("+");
- first = Double.parseDouble(number);
- number = "";
- bplus = true;
- bminus = false;
- bdivided = false;
- btimes = false;
- }
- });
-
- minus = new JButton("-");
- minus.setBounds(150, 120, 50, 40);
- panel.add(minus);
-
- minus.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent e)
- {
- label.setText("-");
- first = Double.parseDouble(number);
- number = "";
- bplus = false;
- bminus = true;
- bdivided = false;
- btimes = false;
- }
- });
-
- times = new JButton("*");
- times.setBounds(150, 80, 50, 40);
- panel.add(times);
-
- times.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent e)
- {
- label.setText("*");
- first = Double.parseDouble(number);
- number = "";
- bplus = false;
- bminus = false;
- bdivided = false;
- btimes = true;
- }
- });
-
- divided = new JButton("/");
- divided.setBounds(150, 40, 50, 40);
- panel.add(divided);
-
- divided.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent e)
- {
- label.setText("/");
- first = Double.parseDouble(number);
- number = "";
- bplus = false;
- bminus = false;
- bdivided = true;
- btimes = false;
- }
- });
-
- equals = new JButton("=");
- equals.setBounds(100, 160, 50, 40);
- panel.add(equals);
-
- equals.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent e)
- {
- second = Double.parseDouble(number);
-
- if(bplus)
- result = first + second;
-
- else if(bminus)
- result = first - second;
-
- else if(bdivided)
- result = first / second;
-
- else if(btimes)
- result = first * second;
-
- number = Double.toString(result);
- label.setText(number);
- }
- });
-
- squareRoot = new JButton("v");
- squareRoot.setBounds(200, 160, 50, 40);
- panel.add(squareRoot);
-
- squareRoot.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent e)
- {
- double temp = Double.parseDouble(number);
-
- for(double i = 0.0; i < temp; i += 0.1)
- {
- if(i*i == temp)
- {
- result = i;
- break;
- }
- else
- {
- System.out.println("forloop has run " + i + " times");
- }
- }
-
- number = Double.toString(result);
- label.setText(number);
- }
-
- });
-
- c = new JButton("C");
- c.setBounds(200, 40, 60, 40);
- panel.add(c);
-
- c.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent e)
- {
- setDefault();
- label.setText(number);
- }
-
- });
-
- delete = new JButton("<-");
- delete.setBounds(180, 0, 80, 40);
- panel.add(delete);
-
- delete.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent e)
- {
- number = number.substring(0, number.length() -1);
- label.setText(number);
- }
-
- });
-
- period = new JButton(",");
- period.setBounds(0, 160, 50, 40);
- panel.add(period);
-
- period.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent e)
- {
- number += ".";
- label.setText(getNumber());
- }
-
- });
-
-
- this.add(panel);
- }
-
- public static void main(String args[])
- {
- Main main = new Main();
- }
-
- public String getNumber()
- {
- return number;
- }
-
- public void setDefault()
- {
- first = 0; second = 0; result = 0;
- number = "";
- bplus = false; bminus = false; btimes = false; bdivided = false;
- }
- }