java抛出异常,即使数据类型正确

ve7v8dk2  于 2021-07-03  发布在  Java
关注(0)|答案(2)|浏览(407)

这个问题在这里已经有了答案

如何使用java.util.scanner从system.in正确读取用户输入并对其执行操作(1个答案)
三年前关门了。
我已经建立了一个货币转换程序,它需要用户输入。在程序中的某个点上,我要求他们转换成什么货币。然而,没有什么能阻止他们将“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.");
        }
    }
}

用另一种方法:)谢谢你的回答,他们帮了很多忙。

qcbq4gxm

qcbq4gxm1#

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部分抛出一个异常。在您的代码中,else部分将不起作用,因为有一个异常它将直接转到catch块。

uqdfh47h

uqdfh47h2#

而不是使用 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();
}

相关问题