簡體   English   中英

以數字為單位拆分單位並拆分余數

[英]Splitting units base of a number and separating remainders

我正在嘗試拆分一個基數,然后將兩個數字分開以獲得不同的輸出。 (請記住,我剛剛編輯過,答案是解決方案)。 留在這里,以便有類似問題的人可以找到解決方案。 謝謝你們!

所以這是個主意:

如果數字> = 10 &&以10為底

然后給我10個單位的折扣價

如果數字<= 0 &&不是10

然后為其中有10個單位的數字加上折扣,其余部分沒有折扣(假設數字為簡單起見,假設為100%)

舉一個實際的例子,如果我訂購25個單位的x(每個1美元)和15個單位的(每個1美元)y,價格將是:

x 20單位= $ 0 x 5單位=總計$ 5

y 10個單位= $ 0 y 5個單位=總$ 5

這有點棘手,這是我到目前為止所得到的:

double discountedmNI = (mNI - ((mNI/100)*10)) * mNIC;
double discountedmNIP = mNI - ((mNI/100)*10);

if(mNIC >= 10 && mNIC % 10 == 0){

    System.out.println("mNI " + discountedmNIP + " " + mNIC);
    System.out.println(discountedmNI);

}

else if (!mNIC % 10 == 0){

    System.out.println("mNI " + mNI + mNIC);
    System.out.println(mNI * mNIC);

}

我不認為我要分開定義10個單位

謝謝你們!

希望我對你沒錯。 我得到您想要計算包含兩個元素的總價格:非打折商品的價格和打折商品的價格。

// The following three values are just example assumptions.
float discountInPercent = 100.0f;
float itemsOrdered = 5004.0f;
float itemPriceNormal = 5.0f;

// Here the price for one discounted item gets calculated.
// Please remember that the discount is given in percentage.
float itemPriceDiscounted = itemPriceNormal * ((100.0f - discountInPercent) / 100.0f);

// Calculate the count of items that get discounted and those that 
// will get priced normally.
float itemsDiscounted = Math.floor(itemsOrdered / 10.0f);
float itemsNotDiscounted = itemsOrdered % 10;

// Finally calculate the two elements of the total price and sum it up. 
float priceDiscounted = (itemsDiscounted * itemPriceDiscounted);
float priceNormal = (itemsNotDiscounted * itemPriceNormal);
float totalPrice = priceDiscounted + priceNormal;

System.out.println("Price discounted: %.2f" + priceDiscounted);
System.out.println("Price non-discounted: %.2f" + priceNormal);
System.out.println("Price total: %.2f" + totalPrice);

EUREKA!

    double discountedmNIP = mNI - ((mNI/100)*10);
    int mNIC2 = (mNIC % 10);
    double mNIC2disc = (mNI * mNIC2);
    double discountedmNI = (mNI - ((mNI/100)*10)) * (mNIC - mNIC2);


    if(mNIC >= 10){

        System.out.println(discountedmNIP + " " + (mNIC - mNIC2) + " " + discountedmNI );
        System.out.println(mNI + " " + mNIC2 + " " + mNIC2disc);

    }



    else{

        System.out.print(mNI + " " + mNIC);
        System.out.print(mNI * mNIC);


    }

    double sum = (mNI + discountedmNI + discountedRh + rH);

    System.out.println('\t');
    System.out.println("Total order cost " + sum);

我所要做的就是取%10的單位,它將左邊的整數或兩倍除以右邊(用戶輸入的左邊)

當我將該變量減去原始變量后,會給我余數!

同樣,這一小步驟使我花了一整夜才弄清楚,這確實很簡單。 這是針對一堂課的,如果您在該堂課中並且正在閱讀(即使您可能需要花點時間才能找到這是什么作業),我也想告訴您,這對編程很有趣! 我不是在諷刺,我真的很喜歡這類問題!

簽:

那外國人

EUREKA了!

請享用!

暫無
暫無

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

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