Kalkulačka, nefunguji dvojciferná čísla – Java – Fórum – Programujte.com
 x   TIP: Přetáhni ikonu na hlavní panel pro připnutí webu

Kalkulačka, nefunguji dvojciferná čísla – Java – Fórum – Programujte.comKalkulačka, nefunguji dvojciferná čísla – Java – Fórum – Programujte.com

 

Dodkar
~ Anonymní uživatel
6 příspěvků
19. 1. 2013   #1
-
0
-

Zdravim programátoři, vyskytl se malý a problém a to: nefunguje mi zadávání dvojciferných čísel v kalkulačce, funguji jen jednociferná! 

package kalkulačka;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class HLclass {

		private int MeziVypocet;
		private double KonecneCislo=0;
		private char operace='0';
		private boolean prvniVstup=true;
	
		private final JFrame frame=new JFrame();
		private final JLabel display=new JLabel("prříklad: ");
		private final JPanel panel1=new JPanel(); 
		private final JPanel panel2=new JPanel(); 
		private final JPanel panel3=new JPanel(); 
	
		private JButton jedna=new JButton("1");
		private JButton dva=new JButton("2");
		private JButton tri=new JButton("3");
		private JButton ctyri=new JButton("4");
		private JButton pet=new JButton("5");
		private JButton sest=new JButton("6");
		private JButton sedum=new JButton("7");
		private JButton osum=new JButton("8");
		private JButton devet=new JButton("9");
		private JButton nula=new JButton("0");
	
		private JButton plus=new JButton("+");
		private JButton minus=new JButton("-");
		private JButton krat=new JButton("*");
		private JButton deleno=new JButton("/");
		private JButton rovnase=new JButton("=");
		private JButton clear=new JButton("C");
	
	public HLclass(){
	
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setResizable(false);
		frame.getContentPane().setLayout(new BorderLayout());
		frame.setLayout(new GridLayout(4, 2));
	
		panel1.add(jedna);panel1.add(dva);panel1.add(tri);panel1.add(ctyri);panel1.add(pet);
		panel2.add(sest);panel2.add(sedum);panel2.add(osum);panel2.add(devet);panel2.add(nula);
		panel3.add(plus);panel3.add(minus);panel3.add(krat);panel3.add(deleno);panel3.add(rovnase);panel3.add(clear);
		frame.add(display);
		frame.add(panel1);
		frame.add(panel2);
		frame.add(panel3);
		frame.pack();
		frame.setVisible(true);
	
		jedna.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {vypocet(1);}});
		dva.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {vypocet(2);}});
		tri.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {vypocet(3);}});
		ctyri.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {vypocet(3);}});
		pet.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {vypocet(5);}});
		sest.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {vypocet(6);}});
		sedum.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {vypocet(7);}});
		osum.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {vypocet(8);}});
		devet.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {vypocet(9);}});
		nula.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {vypocet(0);}});
	
	plus.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			operace='+';
			display.setText(display.getText()+"+");
		}});
	
	minus.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			operace='-';
			display.setText(display.getText()+"-");
		}});
	
	krat.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			operace='*';
			display.setText(display.getText()+"*");
		}});
	
	deleno.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			operace='/';
			display.setText(display.getText()+"/");
		}});
	
	rovnase.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			operace='0';
			display.setText(display.getText()+"="+KonecneCislo);
		}});
	
	clear.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			KonecneCislo=0;
			MeziVypocet=0;
			operace=0;
			display.setText("příklad: ");
			prvniVstup=true;
		}});
	}
	
public void vypocet(int Button){
	MeziVypocet*=10;
	MeziVypocet+=Button;
			
		if(prvniVstup==true)KonecneCislo=MeziVypocet;
			prvniVstup=false;
		if(operace=='+')KonecneCislo+=MeziVypocet;
		if(operace=='-')KonecneCislo-=MeziVypocet;
		if(operace=='*')KonecneCislo*=MeziVypocet;
		if(operace=='/')KonecneCislo/=MeziVypocet;
	
		display.setText(display.getText()+MeziVypocet);
		MeziVypocet=0;
	}

}

Ještě jedna drobnost Jak to, že nemůžu nahradit toto:

if(prvniVstup==true)KonecneCislo=MeziVypocet;
			prvniVstup=false;

tím to, v zásadě je to stejné ne?

if(operace==0)KonecneCislo=MeziVypocet;
		

Předem díky moc za radu :).

Nahlásit jako SPAM
IP: 194.12.32.–
Flowy0
Věrný člen
19. 1. 2013   #2
-
0
-

tvoj kod je neskutocne neprehladne napisany a verim ze je to tvoj prvy projekt ... nauc sa pracovat s while/for loopmi a k tomu aspon else ale switch tiez nieje zbytocny

ako programator by si mal pochopit ze ak mas problem tak zakladnym predpokladom pre jeho riesenie je aby si ho dokazal definovat ...

int medzivypocet ti vzdy prichadza ako 0 a tuto nasobis desiatimi ... potom k tejto nule pricitas zadane cislo a int zase nulujes ... proste to cislo nenuluj pred skutocnym pouzitim vo vypocte a pouzi ho ako buffer vstupu

... a operacia je typu string preto ju nemozes porovnavat s int 0

btw tie operacie v listenery na cisla nedavaju zmysel ... ak si chces robit medzivypocty (robit operacie sposobom FIFO) tak ich rob pri udavani dalsej operacie a nie dalsieho cisla ale kvalitnejsie by bolo vyhodnocovat ich az na konci a pocitat s urcitou prednostou nasobenia a delenia

osobne by som to asi riesil ulozenim prikladu ako string a potom vyhodnocovat cez regularne vyrazy (regex) tak ako nam to ukazuje ms excel

Nahlásit jako SPAM
IP: 91.127.64.–
https://github.com/Flowy
Leeesy
~ Anonymní uživatel
6 příspěvků
22. 1. 2013   #3
-
0
-

Udělal jsem druhou verzi všechno mi funguje až na to když zavolám "rovnáse", tak mi String operace neobsahuje žádný znak jak to? Trochu jsem pořešil i obsluhu JButtnů, zdá se mi lepší.

package kalkulačka;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class kalkulacka implements ActionListener {
	
	JFrame frame=new JFrame();
	JPanel panel=new JPanel();
	JPanel panel1=new JPanel();
	JPanel panel2=new JPanel();
	JTextField text=new JTextField("příklad: ");
	String operace="+";
	int meziVypocet=0;
	int KonecneCislo;
	
	
	JButton soucet=new JButton("+");
	JButton rozdil=new JButton("-");
	JButton podil=new JButton("/");
	JButton soucin=new JButton("*");
	JButton rovnase=new JButton("=");
	JButton clear=new JButton("C");
	
	JButton nula=new JButton("0");
	JButton jedna=new JButton("1");
	JButton dva=new JButton("2");
	JButton tri=new JButton("3");
	JButton ctyri=new JButton("4");
	JButton pet=new JButton("5");
	JButton sest=new JButton("6");
	JButton sedum=new JButton("7");
	JButton osum=new JButton("8");
	JButton devet=new JButton("9");
	
	
	public kalkulacka(){
		
		
		text.setEditable(false);

		soucet.addActionListener(this);
		rozdil.addActionListener(this);
		podil.addActionListener(this);
		soucin.addActionListener(this);
		clear.addActionListener(this);
		rovnase.addActionListener(this);
		
		nula.addActionListener(this);
		jedna.addActionListener(this);
		dva.addActionListener(this);
		tri.addActionListener(this);
		ctyri.addActionListener(this);
		pet.addActionListener(this);
		sest.addActionListener(this);
		sedum.addActionListener(this);
		osum.addActionListener(this);
		devet.addActionListener(this);
	
		panel.add(soucet);panel.add(rozdil);panel.add(podil);panel.add(soucin);panel.add(clear);panel.add(rovnase);
		panel1.add(nula);panel1.add(jedna);panel1.add(dva);panel1.add(tri);panel1.add(ctyri);
		panel2.add(pet);panel2.add(sest);panel2.add(sedum);panel2.add(osum);panel2.add(devet);
		
		frame.setLayout(new GridLayout(4, 2));
		
		frame.add(text, BorderLayout.NORTH);
		frame.add(panel);
		frame.add(panel1);
		frame.add(panel2);
		frame.pack();
		
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
		System.out.println("soucet");
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		
		 operace = e.getActionCommand();
		
		switch(operace){
			case "+":
				vypocet();
				operace="+";
				break;
			case "-":
				vypocet();
				operace="-";
				break;        	
			case "/":
				vypocet();	
				operace="/";
				break;
			case"*":
				vypocet();
				operace="*";
				break;
			case"C":
				meziVypocet=0;
				KonecneCislo=0;
				text.setText("příklad: ");
				
				break;
			case"=":
				System.out.println("konecen cislo =  "+KonecneCislo);
				System.out.println("operace: "+operace);
				vypocet();
				text.setText(text.getText()+"="+KonecneCislo);
				break;
			
			case"0":
				vypocetMeziCisla(0);
				break;
			case"1":
				vypocetMeziCisla(1);
				break;
			case"2":
				vypocetMeziCisla(2);
				break;
			case"3":
				vypocetMeziCisla(3);
				break;
			case"4":
				vypocetMeziCisla(4);
				break;
			case"5":
				vypocetMeziCisla(5);
				break;
			case"6":
				vypocetMeziCisla(6);
				break;
			case"7":
				vypocetMeziCisla(7);
				break;
			case"8":
				vypocetMeziCisla(8);
				break;
			case"9":
				vypocetMeziCisla(9);
				break;
		}	 
	}
	public void vypocet(){
		switch(operace){
		case"+":
			System.out.println("volam vypocet");
			KonecneCislo+=meziVypocet;
			text.setText(text.getText()+"+");
			System.out.println("meziVypocet, KonecneCislo: "+meziVypocet+"   "+KonecneCislo);
			meziVypocet=0;
			System.out.println("meziVypocet, KonecneCislo: "+meziVypocet+"   "+KonecneCislo);
			
			operace="+";
			break;
		case"-":
			KonecneCislo-=meziVypocet;
			text.setText(text.getText()+"-");
			meziVypocet=0;
			operace="-";
			break;
		case"*":
			KonecneCislo*=meziVypocet;
			text.setText(text.getText()+"*");
			meziVypocet=0;
			operace="*";
			break;
		case"/":
			KonecneCislo/=meziVypocet;
			text.setText(text.getText()+"/");
			meziVypocet=0;
			operace="/";
			break;
		default:
			System.out.println("nic");
			break;
			
		
		}
		
	}
	public void vypocetMeziCisla(int Button){
		meziVypocet*=10;
		meziVypocet+=Button;
		text.setText(text.getText()+Button);
		System.out.println("meziVypocet: "+meziVypocet);
	}
}
Nahlásit jako SPAM
IP: 194.12.32.–
Zjistit počet nových příspěvků

Přidej příspěvek

Toto téma je starší jak čtvrt roku – přidej svůj příspěvek jen tehdy, máš-li k tématu opravdu co říct!

Ano, opravdu chci reagovat → zobrazí formulář pro přidání příspěvku

×Vložení zdrojáku

×Vložení obrázku

Vložit URL obrázku Vybrat obrázek na disku
Vlož URL adresu obrázku:
Klikni a vyber obrázek z počítače:

×Vložení videa

Aktuálně jsou podporována videa ze serverů YouTube, Vimeo a Dailymotion.
×
 
Podporujeme Gravatara.
Zadej URL adresu Avatara (40 x 40 px) nebo emailovou adresu pro použití Gravatara.
Email nikam neukládáme, po získání Gravatara je zahozen.
-
Pravidla pro psaní příspěvků, používej diakritiku. ENTER pro nový odstavec, SHIFT + ENTER pro nový řádek.
Sledovat nové příspěvky (pouze pro přihlášené)
Sleduj vlákno a v případě přidání nového příspěvku o tom budeš vědět mezi prvními.
Reaguješ na příspěvek:

Uživatelé prohlížející si toto vlákno

Uživatelé on-line: 0 registrovaných, 8 hostů

Moderátoři diskuze

 

Hostujeme u Českého hostingu       ISSN 1801-1586       ⇡ Nahoru Webtea.cz logo © 20032024 Programujte.com
Zasadilo a pěstuje Webtea.cz, šéfredaktor Lukáš Churý