Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Natalia Młodzik 210057 i Piotr Turoboś 210345
- import javax.swing.JOptionPane;
- public class change
- {
- static int [] ileMonet= {0,0,0,0};
- final static int zakresMin=0;
- final static int zakresMax=100;
- final static int maxPrób=4;
- public static void ZerujTablice()
- {
- for(int i=0; i<=3; i++)
- ileMonet[i]=0;
- }
- public static int KontrolaWejścia ()
- {
- int reszta=0, ilePrób=0;
- boolean odczytOK=false;
- do
- {
- String ileString = JOptionPane.showInputDialog("Podaj wartość reszty (liczbę "
- + "całkowitą z zakresu od " + (zakresMin+1) + " do " + (zakresMax-1)
- +").\nObliczę resztę w monetach:");
- if (ileString != null)
- {
- try
- {
- reszta = Integer.parseInt(ileString);
- if(reszta>zakresMin && reszta<zakresMax)
- odczytOK=true;
- else
- JOptionPane.showMessageDialog(null, "Niestety, podana wartośc "
- + "reszty jest spoza zakresu.");
- }
- catch (NumberFormatException e)
- {
- JOptionPane.showMessageDialog(null, "Zła wartość reszty! ");
- } finally {ilePrób++;}
- }
- else
- System.exit(0);
- } while (!odczytOK && ilePrób<maxPrób);
- if(!odczytOK)
- {
- JOptionPane.showMessageDialog(null, "Zbyt wiele razy podano złą wartość,"
- + " program zakończy swoją pracę.");
- System.exit(0);
- }
- return reszta;
- }
- public static void Change (int reszta)
- {
- while(reszta>=25)
- {
- ileMonet[0]++;
- reszta-=25;
- }
- while(reszta>=10)
- {
- ileMonet[1]++;
- reszta-=10;
- }
- while(reszta>=5)
- {
- ileMonet[2]++;
- reszta-=5;
- }
- while(reszta>=1)
- {
- ileMonet[3]++;
- reszta-=1;
- }
- }
- public static boolean CzyKontynuować()
- {
- int czyKontynuować=JOptionPane.showConfirmDialog(null, "Czy chcesz "
- + "wyznaczyć ilość monet dla innej wartości reszty?",
- "Click Yes or No", JOptionPane.YES_NO_OPTION);
- if (czyKontynuować==JOptionPane.YES_OPTION)
- return true;
- else
- return false;
- }
- public static void main(String[] args)
- {
- do
- {
- int reszta=KontrolaWejścia();
- Change(reszta);
- JOptionPane.showMessageDialog(null, reszta + " centów w monetach:\n"
- + ileMonet[0] + " quarters\n" + ileMonet[1] + " dimes\n" + ileMonet[2]
- + " nickels and\n" + ileMonet[3] + " pennies");
- ZerujTablice();
- }while(CzyKontynuować());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement