Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: How can I make a loop repeat?

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How can I make a loop repeat?

     
    import javax.swing.JOptionPane;
    public class program7 {
     
        public static void main(String[] args) {
     
    //ask user how many euros a dollar buys
    String euroExchangeRateString = JOptionPane.showInputDialog
            (null, "Enter how many Euros one Dollar will buy");
    double euroExchangeRate = Double.parseDouble(euroExchangeRateString);
     
    //ask user how many pounds a dollar will buy
    String poundExchangeRateString = JOptionPane.showInputDialog
            (null, "Enter how many Pounds one Dollar will buy");
    double poundExchangeRate =  Double.parseDouble(poundExchangeRateString);
     
    //ask user how many yen a dollar will buy
    String yenExchangeRateString = JOptionPane.showInputDialog
            (null, "Enter how many Yen one Dollar will buy");
    double yenExchangeRate = Double.parseDouble(yenExchangeRateString);
     
    //ask user to enter the number of dollars they have
     
        String amountDollarsString = JOptionPane.showInputDialog
    (null, "Enter the amount of foreign currency you want to buy in U.S. dollars");
    double amountDollars = Double.parseDouble(amountDollarsString);
    if (amountDollars <= 100){
    amountDollars -= amountDollars * 0.10;
    JOptionPane.showMessageDialog
            (null,"You can buy $" + amountDollars + " in foreign currency.");
    }
    else if (amountDollars > 100) {
    amountDollars -= amountDollars * 0.05;
    JOptionPane.showMessageDialog
            (null,"You can buy $" + amountDollars + " in foreign currency.");
        }
     
     
    String reply = JOptionPane.showInputDialog 
        (null, "Enter 'E' to buy Euros, 'P' to buy Punds or 'Y' to buy Yen");
    char firstChar = reply.charAt(0);
     
    while (firstChar == 'E' || firstChar == 'e'){
    double amountEuros = (amountDollars * euroExchangeRate);
    JOptionPane.showMessageDialog
            (null, "You have purchased " + amountEuros + " Euros.");
    }
    if (firstChar == 'P' || firstChar == 'p') {
    double amountPounds = (amountDollars * poundExchangeRate);
    JOptionPane.showMessageDialog
            (null, "You have purchased " + amountPounds + " Pounds.");
    }
    else if (firstChar == 'Y' || firstChar == 'y') {
    double amountYen = (amountDollars * yenExchangeRate);
    JOptionPane.showMessageDialog
            (null, "You have purchased " + amountYen + " Yen.");
    }
    JOptionPane.showConfirmDialog
            (null, "Are there any more conversions you would like to perform?");
            }
        }

    On the last part, the program is supposed to ask the user if there are anymore conversions to perform. I put in a confirm dialog box, but when the user clicks yes it does not go back to repeat the previous loop. How can I make it do that?


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: How can I make a loop repeat?

    One way to make things repeat is to ask the same question several times, but we ask that you not do this. Closed as duplicate.

Similar Threads

  1. Simple Repeat.
    By Tecness2 in forum Java Theory & Questions
    Replies: 3
    Last Post: May 30th, 2012, 01:35 PM
  2. One last question: How to get menu to repeat!
    By CrimsonFlash in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 24th, 2011, 05:52 AM
  3. How to repeat a while loop?
    By BITmixit in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 12th, 2011, 12:51 PM
  4. Wanting to repeat my program...
    By Shaybay92 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: August 8th, 2011, 08:29 AM
  5. How to make for loop work?
    By Dragonkndr712 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: March 8th, 2011, 02:14 PM

Tags for this Thread