簡體   English   中英

我如何計算輸入后的折扣價或正常價格?

[英]How do I compute a discounted price or the regular price after the inputs?

我仍然是一名學生,仍在學習 java,我最近才知道if...else...是如何工作的,但我不知道如何正確使用它。 如何使用if...else....計算和顯示總價金額

問題:

附近的一家米店正在打折。 如果客戶購買至少 11 公斤,則所有大米均以每公斤 10% (0.1)的價格出售。 對於任何低於此的價格,均適用正常價格。

以下是價格清單:

獅子 - Php48/公斤

Ganador - Php50/公斤

Conchita - Php47/公斤

其他類型的大米 - Php45/公斤

讓用戶輸入購買的大米和公斤數,然后計算並顯示客戶需要支付的總金額

示例:如果客戶購買了 3 公斤的獅子大米,您的程序預計只計算正常價格的 3 公斤。

注意:客戶可以購買任意數量的大米,甚至是半公斤。

我的代碼:

import java.util.Scanner;

public class Main
{
    public static void main (String[] args) 
    {
        System.out.println("List of prices: ");
        System.out.println("Lion - Php48/kilo");
        System.out.println("Ganador - Php50/kilo");
        System.out.println("Conchita - Php47/kilo");
        System.out.println("Other types of rice - Php45/kilo");
        System.out.println(" ");
        
        Scanner input = new Scanner(System.in);
        System.out.println("Type of rice: ");
        String rice = input.nextLine();
        System.out.println(" ");

        System.out.println("Kilos of rice: ");
        int kilo = input.nextInt();
        System.out.println(" ");
        
        System.out.println("Type of rice: " +rice);
        System.out.println("Kilos of rice: " +kilo);
        
        if(kilo>11)
        {
            System.out.println("You have a discount of 10%.");
        }
        else if(kilo<11)
        {
            System.out.println("You have no discount of 10%.");
        }
    }
}

問題陳述說可以購買任何數量的大米,甚至是半公斤。 計算機不能接受像 ½ 這樣的粗俗分數,但它們可以接受小數,因此變量kilo的類型應該是double (而不是int )。

想想你將如何進行計算。 您需要獲取用戶輸入的大米類型的價格。 我會使用switch語句或Map但由於您可能還沒有學習過這些,並且由於您 state 這是使用if...else的練習,下面的代碼使用if...else來獲取價格要求的大米類型。

得到價格后,您只需乘以輸入的金額。 現在您應用折扣。 10% 的折扣意味着您將價格乘以0.9

這是代碼。

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        System.out.println("List of prices: ");
        System.out.println("Lion - Php48/kilo");
        System.out.println("Ganador - Php50/kilo");
        System.out.println("Conchita - Php47/kilo");
        System.out.println("Other types of rice - Php45/kilo");
        System.out.println(" ");

        Scanner input = new Scanner(System.in);
        System.out.println("Type of rice: ");
        String rice = input.nextLine();
        System.out.println(" ");

        System.out.println("Kilos of rice: ");
        double kilo = input.nextDouble();
        System.out.println(" ");

        System.out.println("Type of rice: " + rice);
        System.out.println("Kilos of rice: " + kilo);
        int price;
        if ("Lion".equals(rice)) {
            price = 48;
        }
        else if ("Ganador".equals(rice)) {
            price = 50;
        }
        else if ("Conchita".equals(rice)) {
            price = 47;
        }
        else {
            price = 45;
        }
        double cost = price * kilo;

        if (kilo >= 11) {
            System.out.println("You have a discount of 10%.");
            cost *= 0.9;
        }
        else {
            System.out.println("You have no discount of 10%.");
        }
        System.out.println("Cost: " + cost);
    }
}

請注意,您需要使用方法equals比較字符串。 請參閱如何比較 Java 中的字符串?

這是上述代碼運行示例的 output。

List of prices: 
Lion - Php48/kilo
Ganador - Php50/kilo
Conchita - Php47/kilo
Other types of rice - Php45/kilo
 
Type of rice: 
Ganador
 
Kilos of rice: 
11.5
 
Type of rice: Ganador
Kilos of rice: 11.5
You have a discount of 10%.
Cost: 517.5

這個問題的關鍵是利用條件(例如:if)語句或 HashMap 來確定客戶選擇的大米類型的價值。

  1. 第一步:確定大米的種類。

  2. 第 2 步:從第 1 步確定大米的價格

  3. 第三步:判斷客戶選擇的大米數量是否>=11公斤

  4. 第 4 步:如果第 3 步為真,則對 totalPrice 應用 90% 的折扣

另請注意,如果提供任何其他類型的大米,默認價格為 45。

下面的代碼示例:使用 HashMap。

public class Main {
    public static void main (String[] args) 
    {
        //declaring local variables 
        int priceOfOtherTypesOfRice = 45;
        double discountValue = 0.9;
        HashMap<String,Integer> ricePriceMap  = new HashMap<>();
        ricePriceMap.put("Lion", 48);
        ricePriceMap.put("Ganador", 50);
        ricePriceMap.put("Conchita", 47);
        double totalPrice =0.0;             
        
        System.out.println("List of prices: ");
        System.out.println("Lion - Php48/kilo");
        System.out.println("Ganador - Php50/kilo");
        System.out.println("Conchita - Php47/kilo");
        System.out.println("Other types of rice - Php45/kilo");
        System.out.println(" ");
        
        Scanner input = new Scanner(System.in);
        System.out.println("Type of rice: ");
        String rice = input.nextLine();
        System.out.println(" ");

        System.out.println("Kilos of rice: ");
        int kilo = input.nextInt();
        System.out.println(" ");
        
        System.out.println("Type of rice: " +rice);
        System.out.println("Kilos of rice: " +kilo);
        if(kilo>11)
        {
            System.out.println("You have a discount of 10%.");
            totalPrice  =kilo * ((discountValue)*ricePriceMap.getOrDefault(rice, priceOfOtherTypesOfRice));
        }
        else if(kilo<11)
        {
            System.out.println("You have no discount of 10%.");
            totalPrice  =kilo * ricePriceMap.getOrDefault(rice, priceOfOtherTypesOfRice);
        }
        System.out.println("Final total cost of rice is " + totalPrice);

    }
}

控制台 output: [1]:https://i.stack.imgur.com/0sfa1.png

暫無
暫無

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

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