Convert Euro to dollar, need to be able to enter decimal!

Convert Euro to dollar, need to be able to enter decimal!

Hi, this is what my program is supposed to do: Write a program that repeatedly performs currency conversion from euros to dollars: it prompts the user for the conversion factor (i.e. the number that you use to multiply the number of euros to get the number of dollars). The program then repeatedly asks the user for a quantity to convert, considers each quantity to be an amount in euros and displays the equivalent number of dollars. When the user enters 0 or a negative amount, the program quits.

My program does all that. The only problem is, that if you enter a decimal for the conversion factor or quantity the program will give me an error. Can someone give me an example of how to fix my program so that a decimal would be accepted as well as a whole number? Thanks! Here is my code:

import java.util.*;

public class Euro {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Please enter a conversion factor. ");
        double num1 = input.nextInt();
        System.out.print("Please enter a quantity or enter a 0 or less to stop the program.");
        double num2 = input.nextInt();
        double total = num1 * num2;

        while (num2 > 0) 
        {
            System.out.println("The quantity of dollars is:" + total);
            System.out.print("Please enter a quantity or enter a 0 or less to stop the program.");
            num2 = input.nextInt();
            total = num1 * num2;
        }
        }

        }
View Answers









Related Tutorials/Questions & Answers:

Ads