I am attempting to create a program thtat does some processing and exits when a given letter is typed.

//1.00usd = .727751euro
int reset = 0;
while(reset == 0)
{
    double euro;
    double ems;

    String input = JOptionPane.showInputDialog(null,"Enter Amount of US Dollar: ");

    ems = Double.parseDouble(input);

    if (ems < 0)
    {
        JOptionPane.showMessageDialog(null, "Please enter a real amount of money");
        reset = 0;
    }

    if (ems >= 0)
    {
        euro = .727751;
        ems = ems*euro;
        ems = ems*100;
        ems = Math.round(ems);      
        ems = ems/100;

        JOptionPane.showMessageDialog(null,"Amount in euros: € " + ems);
    }
}

This program is to convert usd to euro and I wanted to know how I can make the program exit when entering the letter "Q".

This is for a an object class so I'm still learning.

有帮助吗?

解决方案

Something like

String input = JOptionPane.showInputDialog(null,"Enter Amount of US Dollar: ");

if( input.equals("Q") ) // but the case is important here
{
    System.out.println("Bye bye");
    System.exit(0);
}
ems = Double.parseDouble(input);

其他提示

If your question is "how to exit the program", you can call

System.exit(0);

when the user presses a key. If you just want to "quit" the loop you're in, manage to get the condition true, or use "break" (but you should not need it in your case).

Add this if statement in the while loop.

if(inputString.equalsIgnoreCase("q")) {
    System.exit(0);
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top