即使使用正确的数据类型,也会抛出异常

时间:2017-09-04 17:07:54

标签: java exception exception-handling

我已经构建了一个货币转换程序,它需要用户输入。在程序中的某个点,我要求他们想要转换的货币。然而,没有什么能阻止他们将“java”作为货币或任何其他词语。

这是我目前的代码,我会再解释一下我正在寻找的内容:

import java.util.Scanner;

public class Exchange {
    public static void main(String[] args) {
        int i = 1;
        Scanner inn = new Scanner(System.in);
        try{
            while ( i > 0){
                double USDrate = 0.12808;
                double EURrate = 0.107643821;
                double GBPrate = 0.098872935;
                System.out.println("Hello, what currency would you like to convert to?");
                System.out.println("Types: USD  /  EUR  / GBP");
                String type = inn.nextLine();
                System.out.println("Hello, how much would you like to convert?");
                double NOK = inn.nextDouble();
                if( type.equalsIgnoreCase("USD")){
                        System.out.printf(NOK + "kr equals $%.2f \n", (NOK*USDrate));
                }else if( type.equalsIgnoreCase("EUR")){
                        System.out.printf(NOK + "kr equals €%.2f \n", (NOK*EURrate));
                }else if( type.equalsIgnoreCase("GBP")){
                        System.out.printf(NOK + "kr equals £%.2f \n", (NOK*GBPrate));
                }else {
                    // This is where I would like to throw an exception if what they put in doesn't make sense.
                }
                inn.nextLine();
                System.out.println("Would you like to convert more?");
                System.out.println(" YES / NO ");
                String ans = inn.nextLine();
                if( ans.equalsIgnoreCase("NO")){
                        i = 0;
                }
            }
        }
        catch(Exception e){
            System.out.println("You've entered an incorrect value! Restart.");
        }
    }
}   

我希望异常发生在else循环中的最后一个while - 语句下,因为你可以看到我在那里有一个评论正是如此。我也希望在代码块中有相同的异常,我会问他们是否想转换更多。

任何人都可以提供帮助吗?如果您需要的任何细节遗失,我会根据您的要求给予他们。

提前致谢。

我想出了如何按照我想要的方式制作它。没有例外,但这是我的解决方案:

import java.util.Scanner;

public class Exchange {
    public static void main(String[] args) {
        int i = 1;
        Scanner inn = new Scanner(System.in);
        try{
            while ( i > 0){
                double USDrate = 0.12808;
                double EURrate = 0.107643821;
                double GBPrate = 0.098872935;
                System.out.println("Hello, what currency would you like to convert to?");
                System.out.println("Types: USD  /  EUR  / GBP");
                String type = inn.nextLine();
                if(!type.equalsIgnoreCase("USD") && !type.equalsIgnoreCase("EUR") && !type.equalsIgnoreCase("GBP")){
                    System.out.println("Not available currency! Try again.");
                    break;
                }
                System.out.println("How much would you like to convert?");
                double NOK = inn.nextDouble();
                if( type.equalsIgnoreCase("USD")){
                        System.out.printf(NOK + "kr equals $%.2f \n", (NOK*USDrate));
                }else if( type.equalsIgnoreCase("EUR")){
                        System.out.printf(NOK + "kr equals €%.2f \n", (NOK*EURrate));
                }else if( type.equalsIgnoreCase("GBP")){
                        System.out.printf(NOK + "kr equals £%.2f \n", (NOK*GBPrate));
                }
                inn.nextLine();
                System.out.println("Would you like to convert more?");
                System.out.println(" YES / NO ");
                String ans = inn.nextLine();
                if( ans.equalsIgnoreCase("NO")){
                        i = 0;
                }
            }
        }
        catch(Exception e){
            System.out.println("You've entered an incorrect value! Restart.");
        }
    }
}   

使用了另一种方法:)无论如何,感谢您的答案,他们帮助了很多。

3 个答案:

答案 0 :(得分:0)

    import java.util.Scanner;

public class Exchange {
    public static void main(String[] args) throws Exception {
        int i = 1;
        Scanner inn = new Scanner(System.in);
        while (i > 0) {
            double USDrate = 0.12808;
            double EURrate = 0.107643821;
            double GBPrate = 0.098872935;
            System.out.println("Hello, what currency would you like to convert to?");
            System.out.println("Types: USD  /  EUR  / GBP");
            String type = inn.nextLine();
            System.out.println("Hello, how much would you like to convert?");
            double NOK = inn.nextDouble();
            if (type.equalsIgnoreCase("USD")) {
                System.out.printf(NOK + "kr equals $%.2f \n", (NOK * USDrate));
            } else if (type.equalsIgnoreCase("EUR")) {
                System.out.printf(NOK + "kr equals €%.2f \n", (NOK * EURrate));
            } else if (type.equalsIgnoreCase("GBP")) {
                System.out.printf(NOK + "kr equals £%.2f \n", (NOK * GBPrate));
            } else {
                throw new Exception();
            }
            inn.nextLine();
            System.out.println("Would you like to convert more?");
            System.out.println(" YES / NO ");
            String ans = inn.nextLine();
            if (ans.equalsIgnoreCase("NO")) {
                i = 0;
            }
        }
    }
        /*(Exception e) {
            System.out.println("You've entered an incorrect value! Restart.");*/
}

它将在else部分抛出一个异常。在你的代码中,其他部分将无法正常工作,因为有一个例外,它会直接捕获块。

答案 1 :(得分:-1)

而不是使用if/else if... block我建议使用switch语句。在默认情况下,您可以抛出异常。因为您正在抓住Exception,只需制作throw new Exception();

           type = type.toUpperCase();
            switch(type) {
                case "USD":
                    System.out.printf(NOK + "kr equals $%.2f \n", (NOK*USDrate));
                break;

                case "EUR":
                    System.out.printf(NOK + "kr equals €%.2f \n", (NOK*EURrate));
                break;

                case "GBP":
                    System.out.printf(NOK + "kr equals £%.2f \n", (NOK*GBPrate));
                break;

                default:
                    throw new Exception();
            }

不要忘记关闭扫描仪

        finally {
            inn.close();
        }

答案 2 :(得分:-1)

为什么你想在这里抛出异常!

只需在else块中打印一些含义消息

System.out.println("You've entered an incorrect value! Please provide correct value"); // something similar.

如果你真的想抛出异常,那么在else块中写下面的代码。

throw new Exception();