需要一种方法来确保用户输入正确的金额,以及确保用户不输入字符串

时间:2015-12-03 14:56:26

标签: java exception-handling

此处的代码一直有效,直到我需要通过输入字符串而不是整数或双精度来确保用户不会导致异常。我基本上需要确保用户输入的金额大于或等于价格,以便程序可以返回正确的更改量。

public static double findChange()
{
    System.out.println("\nPlease insert: " + price + " (enter payment amount)");

        while (payment < price)
        {
            try {
                payment = kb.nextDouble();
                //takes input until user has entered the needed amount

            } catch (Exception e) 
            {
                System.out.println("Error: Please enter valid currency");
            }
            price = price - payment;
            price = (price * 100) / 100;
            System.out.println("Please insert:" + price);
            if (payment <= price)
                stringError = false;
        }
    } 

    change = payment - price;
    change = Math.round(change * 100);
    change = change / 100;
    System.out.println("\nChange Given: $" + change);
    //determines amount of change to give user and formats to cents

    return change;

}

3 个答案:

答案 0 :(得分:1)

更改

catch (Exception e) 
{
    System.out.println("Error: Please enter valid currency");
}

catch (Exception e)
{
    System.out.println("Error: Please enter valid currency");
    continue;
}

这样,如果用户输入非double值,将显示错误消息,并且将要求他重新输入一个值(continue;指令跳过当前迭代&amp;转到下一个)。

答案 1 :(得分:0)

 try {
                payment = kb.nextDouble();
                //takes input until user has entered the needed amount

            } catch (Exception e) 
            {
                System.out.println("Error: Please enter valid currency");
            }
            price = price - payment;

当发生异常时,这将导致麻烦,因为付款没有价值(或没有正确的价值)。

稍后在while块中放入你的catch语句

答案 2 :(得分:0)

尝试以下方法:

using System;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            findChanges();
        }

        private static double findChanges()
        {
            string xPrice;
            string xPayment;
            double price = 0d;
            double payment = 0d;
            double change = 0d;
            Console.WriteLine("Please insert price and payment amout");
            Console.WriteLine("Price ?");
            xPrice = Console.ReadLine();
            bool PriceIsNumber = double.TryParse(xPrice, out price);
            Console.WriteLine("Payment ?");
            xPayment = Console.ReadLine();
            bool PaymentIsNumber = double.TryParse(xPayment, out payment);

            if (PriceIsNumber == true && PaymentIsNumber == true)
            {
                if (payment > price)
                {
                    try
                    {
                        // price = price - payment;
                        // price = (price * 100) / 100;
                        change = payment - price;
                        // change = Math.Round(change * 100);
                        // change = change / 100;
                        Console.WriteLine("Change = " + change.ToString());
                    }
                    catch (Exception e)
                    {
                        // supress or process e
                    }
                }
            }
            else
            {
                Console.WriteLine("Please enter valid currency");
            }
            Console.Read();
            return change;
        }
    }
}