Złe wyniki kalkulatora

0

Witajcie napisałem prosty kalkulator w javie,ale jest problem wyniki wyświetlają się w postaci np.3.0 jak to naprawić? Prosiłbym o poprawienie tego co jest źle.

public class Kalkulator extends JFrame implements ActionListener {

	private JTextArea text = new JTextArea(1, 20);
	private JFrame frame = new JFrame("Kalkulator");
	private JPanel panel = new JPanel();

	private JMenuBar menuBar = new JMenuBar();
	private JMenu Program = new JMenu("Program");
	private JMenu Edycja = new JMenu("Edycja");
	private JMenuItem Zamknij = new JMenuItem("Zamknij");
	private JMenuItem Kopiuj = new JMenuItem("Kopiuj");
	private JMenuItem Wklej = new JMenuItem("Wklej");

	private JButton but1 = new JButton("1");
	private JButton but2 = new JButton("2");
	private JButton but3 = new JButton("3");
	private JButton but4 = new JButton("4");
	private JButton but5 = new JButton("5");
	private JButton but6 = new JButton("6");
	private JButton but7 = new JButton("7");
	private JButton but8 = new JButton("8");
	private JButton but9 = new JButton("9");
	private JButton but0 = new JButton("0");

	private JButton dodaj = new JButton("+");
	private JButton odejmij = new JButton("-");
	private JButton pomnoz = new JButton("*");
	private JButton podziel = new JButton("/");
	private JButton wynik = new JButton("=");
	private JButton czysc = new JButton("C");

	Double number1, number2, result;
	int dodajc = 0, odejmijc = 0, pomnozc = 0, podzielc = 0;

	public void Zamknij() {

		System.exit(0);
	}

	public void but1() {

		text.append("1");
	}

	public void but2() {

		text.append("2");
	}

	public void but3() {

		text.append("3");
	}

	public void but4() {

		text.append("4");
	}

	public void but5() {

		text.append("5");
	}

	public void but6() {

		text.append("6");
	}

	public void but7() {

		text.append("7");
	}

	public void but8() {

		text.append("8");
	}

	public void but9() {

		text.append("9");
	}

	public void but0() {

		text.append("0");
	}

	public void dodaj() {

		number1 = number_reader();
		text.setText("");
		dodajc = 1;
		odejmijc = 0;
		pomnozc = 0;
		podzielc = 0;
	}

	public void odejmij() {

		number1 = number_reader();
		text.setText("");
		dodajc = 0;
		odejmijc = 1;
		pomnozc = 0;
		podzielc = 0;
	}

	public void pomnoz() {

		number1 = number_reader();
		text.setText("");
		dodajc = 1;
		odejmijc = 0;
		pomnozc = 1;
		podzielc = 0;
	}

	public void wynik() {

		number2 = number_reader();
		if (dodajc > 0) {
			result = number1 + number2;
			text.setText(Double.toString(result));
		}

		if (odejmijc > 0) {
			result = number1 - number2;
			text.setText(Double.toString(result));
		}
		if (pomnozc > 0) {
			result = number1 * number2;
			text.setText(Double.toString(result));
		}
	}

	public double number_reader() {

		Double num1;
		String s;
		s = text.getText();
		num1 = Double.valueOf(s);
		return num1;

	}

	public Kalkulator() {

		this.setSize(new Dimension(240, 230));
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
		this.setTitle("Kalkulator");
		this.add(panel, BorderLayout.CENTER);

		// this.setIconImage(Toolkit.getDefaultToolkit().getImage(
		// getClass().getResource("pobrane.png")))

		Zamknij.addActionListener(this);
		Kopiuj.addActionListener(this);
		Wklej.addActionListener(this);

		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);

		dodaj.addActionListener(this);
		odejmij.addActionListener(this);
		pomnoz.addActionListener(this);
		podziel.addActionListener(this);
		wynik.addActionListener(this);
		czysc.addActionListener(this);

		this.setJMenuBar(menuBar);
		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(dodaj);
		panel.add(odejmij);
		panel.add(pomnoz);
		panel.add(podziel);
		panel.add(wynik);
		panel.add(czysc);

		menuBar.add(Program);
		menuBar.add(Edycja);
		Program.add(Zamknij);
		Edycja.add(Kopiuj);
		Edycja.add(Wklej);

		// skroty klawiszowe
		Zamknij.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,
				ActionEvent.CTRL_MASK));
		Kopiuj.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,
				ActionEvent.CTRL_MASK));
		Wklej.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,
				ActionEvent.CTRL_MASK));
	}

	public static void main(String[] args) {
		new Kalkulator();
	}

	@Override
	public void actionPerformed(ActionEvent a) {
		// TODO Auto-generated method stub

		if (a.getSource() == Zamknij)

			Zamknij();

		if (a.getSource() == but1)

			but1();

		if (a.getSource() == but2)

			but2();

		if (a.getSource() == but3)

			but3();

		if (a.getSource() == but4)

			but4();

		if (a.getSource() == but5)

			but5();

		if (a.getSource() == but6)

			but6();

		if (a.getSource() == but7)

			but7();

		if (a.getSource() == but8)

			but8();

		if (a.getSource() == but9)

			but9();

		if (a.getSource() == but0)

			but0();

		if (a.getSource() == dodaj)

			dodaj();

		if (a.getSource() == odejmij)

			odejmij();

		if (a.getSource() == pomnoz)

			pomnoz();

		if (a.getSource() == wynik)

			wynik();
	}
} 
0

gdy robisz

Double.toString(result)

to powinienes np. sprawdzic czy koncowka jest ".0" i jesli tak to ja usunac, mozesz te dwie rzeczy zrobic za pomoca metod klasy String takich jak endsWith i replace http://docs.oracle.com/javase/7/docs/api/java/lang/String.html

1

Przecież ten kod nadaje się tylko na podpałkę. Usuń z dysku, zapomnij o nim i napisz nowy.

1 użytkowników online, w tym zalogowanych: 0, gości: 1