Currency issue issue in Java

I try to teach myself Java and run into two little hiccups in only two chapters in the book I received: P This is one of one of the exercises:

"Write a class that calculates and displays the conversion of the entered number of dollars into banknotes --- 20 s, 10 s, 5 s and 1 s."

Iโ€™m going to read from 0 knowledge of coding for four hours so far, so hopefully this doesnโ€™t seem like a simple question to answer. I'm sure there is a much more efficient way to write all of this, but my question is how can I finish everything if the user answers โ€œyesโ€ or continues to work with the revised version if they answer โ€œnoโ€?

Also, any advice or recommendations that you guys can give me while studying Java will be very grateful! Thanks for taking the time to read this.

import javax.swing.JOptionPane; public class Dollars { public static void main(String[] args) { String totalDollarsString; int totalDollars; totalDollarsString = JOptionPane.showInputDialog(null, "Enter amount to be converted", "Denomination Conversion", JOptionPane.INFORMATION_MESSAGE); totalDollars = Integer.parseInt(totalDollarsString); int twenties = totalDollars / 20; int remainderTwenty = (totalDollars % 20); int tens = remainderTwenty / 10; int remainderTen = (totalDollars % 10); int fives = remainderTen / 5; int remainderFive = (totalDollars % 5); int ones = remainderFive / 1; JOptionPane.showMessageDialog(null, "Total Entered is $" + totalDollarsString + "\n" + "\nTwenty Dollar Bills: " + twenties + "\nTen Dollar Bills: " + tens + "\nFive Dollar Bills: " + fives + "\nOne Dollar Bills: " + ones); int selection; boolean isYes, isNo; selection = JOptionPane.showConfirmDialog(null, "Is this how you wanted the total broken down?", "Select an Option", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE); isYes = (selection == JOptionPane.YES_OPTION); JOptionPane.showMessageDialog(null, "You responded " + isYes + "\nThanks for your response!"); isNo = (selection == JOptionPane.NO_OPTION); int twenties2 = totalDollars / 20; int tens2 = totalDollars / 10; int fives2 = totalDollars / 5; int ones2 = totalDollars / 1; JOptionPane.showMessageDialog(null, "Total Entered is $" + totalDollarsString + "\n" + "\nTwenty Dollar Bills: " + twenties2 + "\nTen Dollar Bills: " + tens2 + "\nFive Dollar Bills: " + fives2 + "\nOne Dollar Bills: " + ones2); } } 
+4
source share
1 answer

First, you really don't need two booleans for isYes and isNo. Basically, you ask the user if he wants a different solution, that is, one true / false value (or rather: isNo is the same as! IsYes, since the parameter bar returns only one of the values โ€‹โ€‹YES_OPTION and NO_OPTION).

What you want to do next is switch to your โ€œrefinedโ€ version if the user indicates that the first result was not what he wanted:

 int selection = JOptionPane.showConfirmDialog(null, "Is this how you wanted the total broken down?", "Select an Option", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE); if (selection == JOptionPane.NO_OPTION) { int twenties2 = totalDollars / 20; int tens2 = totalDollars / 10; int fives2 = totalDollars / 5; int ones2 = totalDollars / 1; JOptionPane.showMessageDialog(null, "Total Entered is $" + totalDollarsString + "\n" + "\nTwenty Dollar Bills: " + twenties2 + "\nTen Dollar Bills: " + tens2 + "\nFive Dollar Bills: " + fives2 + "\nOne Dollar Bills: " + ones2); } 

If the user selects "Yes", your main method is executed in any case, so in this case you do not need to do anything.

0
source

Source: https://habr.com/ru/post/1440991/


All Articles