簡體   English   中英

不確定是否要使用兩個變量執行計算

[英]Unsure about performing a calculation with two variables

這就是我到目前為止所得到的。 在計算完交換值后,我希望它向用戶收取是否是否為帳戶持有人的佣金(開始時要求)。 就像如果他們不是帳戶持有人一樣,那么他們將收取2%和1%的佣金。 我不確定如何使用AccHolder變量和ExcAmount將其合並到計算佣金中。 有幫助嗎?

if (CurChoice.equals("EUR")) {
            result = ExcAmount / Euro;
            DecimalFormat df = new DecimalFormat("#.##");
            System.out.println(ExcAmount + " in " + CurChoice + "\t=£" + df.format(result));
        } else if (CurChoice.equals("USD")) {
            result = ExcAmount / USAD;
            DecimalFormat df = new DecimalFormat("#.##");
            System.out.println(ExcAmount + " in " + CurChoice + "\t=£" + df.format(result));
        } else if (CurChoice.equals("JPY")) {
            result = ExcAmount / JapYen;
            DecimalFormat df = new DecimalFormat("#.##");
            System.out.println(ExcAmount + " in " + CurChoice + "\t=£" + df.format(result));
        } else if (CurChoice.equals("PLN")) {
            result = ExcAmount / Zloty;
            DecimalFormat df = new DecimalFormat("#.##");
            System.out.println(ExcAmount + " in " + CurChoice + "\t=£" + df.format(result));
        } else {
            System.out.println("");
        }

        double commission = (yn ? 0.01 : 0.02)*result;
        DecimalFormat df = new DecimalFormat("#.##");
        System.out.println("Commission\t=£" + df.format(commission));

        double netPayment = result - commission;
        System.out.println("Total:\t=£" +netPayment);

聲明一次結果。

double result = 0.0;
if (CurChoice.equals("EUR")) {
    result = ExcAmount / Euro;
//...
 } else {
    System.out.println("");
}

double commission = (yn ? 0.01 : 0.02)*result;
System.out.println( "commision: " + ... );
double netPayment = result - commission;

yn是一個糟糕的名字,isAccountHolder可能更好。

同樣,與if相比,打開CurChoice更為可取。

定義yn的位置如下:

boolean yn;

初始化為true(如下所示),因為將不允許用戶輸入y / n以外的其他字符串。

boolean yn = true;

閱讀金額后:

int ExcAmount = keybStr.nextInt();

添加以下行以計算得出的佣金,例如:

double result = 0.;//default value of result

//do the calculation 

if (yn) {
   result *= 0.01d;
} else { 
   result *= 0.02d;
}

現在,刪除所有對結果的重新聲明,例如double result到just result ,因此我們只將轉換后的值添加到佣金中。

在......的最后

if (MenuChoice == 1) {

您應該設置以下條件:

if (yn) ExcAmount += 0.01 * ExcAmount;
else ExcAmount += 0.02 * ExcAmount;

這是您要尋找的條件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM