我的基本货币转换器程序输出错误

时间:2019-07-13 15:08:49

标签: java

因此,该代码应转换货币,但不能正确执行。我有一个变量k(克罗地亚库纳... 1欧元= 7.5库纳),例如,如果我想将1欧元转换为1美元,程序将金额(1)乘以7.5,那么我得到欧元的KUNA金额,那么行得通。但是,当我将结果(7.5)除以6.3(1 DOLLAR IS 6.3 KUNA)时,我得到的数字相同。

import java.util.Scanner;

public class Conv {
    private double rez;
    private double rez2;
    private double svota;
    Scanner ul = new Scanner(System.in);

    public void PretvorbaInKunu(double y) {
        System.out.print("Insert amomunt: ");
        svota = ul.nextDouble();
        rez2 = svota*y;

    }

    public void PR2(double x) {
        rez = getRez2() / x;
    }

    public double getRez() {
        return rez;
    }
    public double getRez2() {
        return rez2;
    }
    public double getSvota() {
        return svota;
    }

}
import java.util.Scanner;

//Currency Converter

public class Vjezbica {
    public static void main(String[] args) {
        double e = 7.5;
        double d = 6.3;
        double p = 9.5;
        double k = 1.0;
        Conv more = new Conv();
        Scanner in = new Scanner(System.in);

        System.out.print("\t\tCurrency converter\nIz (e,p,d,k) - ");
        String iz = in.next();
        switch(iz) {
        case "e":
            more.PretvorbaInKunu(e);
            break;
        case "d":
            more.PretvorbaInKunu(d);
            break;
        case "p":
            more.PretvorbaInKunu(p);
            break;
        case "k":
            more.PretvorbaInKunu(k);
            break;
        }
        System.out.println(more.getRez2());

        System.out.print(" To  ");
        String u = in.next();
        switch(u) {
        case "e":
            more.PR2(e);
        case "d":
            more.PR2(d);
        case "p":
            more.PR2(p);
        case "k":
            more.PR2(k);
        }
        System.out.println(more.getSvota() + " " + iz + " is " + more.getRez() + " " + u);
    }
}

2 个答案:

答案 0 :(得分:1)

问题出在第二个switch-case语句中:您需要在每种情况的结尾处添加一个中断。

如果您像这样更改Vjezbica类,它应该可以工作:

import java.util.Scanner;

//Currency Converter

public class Vjezbica {

    public static void main(String[] args) {
        double e = 7.5;
        double d = 6.3;
        double p = 9.5;
        double k = 1.0;
        Conv more = new Conv();
        Scanner in = new Scanner(System.in);

        System.out.print("\t\tCurrency converter\nIz (e,p,d,k) - ");
        String iz = in.next();
        switch (iz) {
            case "e":
                more.PretvorbaInKunu(e);
                break;
            case "d":
                more.PretvorbaInKunu(d);
                break;
            case "p":
                more.PretvorbaInKunu(p);
                break;
            case "k":
                more.PretvorbaInKunu(k);
                break;
        }
        System.out.println(more.getRez2());

        System.out.print(" To  ");
        String u = in.next();
        switch (u) {
            case "e":
                more.PR2(e);
                break;//added break here
            case "d":
                more.PR2(d);
                break;//added break here
            case "p":
                more.PR2(p);
                break;//added break here
            case "k":
                more.PR2(k);
                break;//added break here
        }
        System.out.println(more.getSvota() + " " + iz + " is " + more.getRez() + " " + u);

        //you should also close the scanner at the end...
        in.close();
    }
}

答案 1 :(得分:0)

首先-正如Tobias所指出的那样,错误是缺少的“中断”; 但是代码中还有其他错误,例如扫描仪未关闭。

我也建议提高代码质量。您可以编写一个只执行计算的枚举类型(无输入-输入应分开)。然后,您可以使用不同的枚举构造函数轻松定义货币。

上面还讨论了应使用double还是BigDecimal。 BigDecimal是一种“昂贵”类型。我想知道浮点数是否还不够,因为货币值通常包含2位数的精度-如果您处理的金额不是很高,那么32位浮点数类型应该已经受到影响。

我将其重写如下:

import java.util.Scanner;

//Currency Converter

public class Vjezbica {

    private static enum Currency {
        EURO(7.5), KUNA(1), DOLLAR(6.3), P(9.3);

        private double cr;

        Currency(double conversionRate) {
            this.cr = conversionRate;
        }

        // precision: 2digits
        public float fromKuna(double kuna) {
            return (int) (((kuna) / cr) * 100) / 100f;
        }

        public float toKuna(double foreignCurrency) {
            return (int) (((foreignCurrency) * cr) * 100) / 100f;
        }
    }

    private static Currency readCurrency() {
        Scanner in = new Scanner(System.in);
        System.out.print("\t\tCurrency converter\nIz (e,p,d,k) - ");
        String iz = in.next();
        in.close();

        switch (iz) { // no break required when returning directly
        case "e":
            return Currency.EURO;
        case "d":
            return Currency.DOLLAR;
        case "p":
            return Currency.P;
        case "k":
            return Currency.KUNA;
        default:
            throw new IllegalArgumentException("invalid input: " + iz);
        }
    }

    private static float readAmount() {
        Scanner ul = new Scanner(System.in);
        float svota = ul.nextFloat();
        ul.close();
        return svota;
    }

    public static void main(String[] args) {

        System.out.print(" From  ");
        Currency c1 = readCurrency();

        System.out.print("Insert amomunt: ");
        float svota = readAmount();

        float amountInKuna = c1.toKuna(svota);

        System.out.print(" To  ");
        Currency c2 = readCurrency();

        float amountInC2 = c2.fromKuna(amountInKuna);
        System.out.println(svota + " " + c1.toString() + " is " + amountInC2 + " " + c2.toString());

    }


}

不考虑:转换确实会先从X转换为Kuna,然后再从Kuna转换为Y。在这两次转换中,我们都执行了不精确的舍入(用四舍五入代替正确的四舍五入,并且将两次取整而不是一次取整)。

根据此建议,如果您计算出精确的转换率(最终允许从X到Y的直接转换,然后对最终结果进行舍入运算),那就更好了。