无法理解计算方法

时间:2018-05-27 22:37:50

标签: android calculated-field

我在计算方法中有问题

它应该像给我那样的总价格* 5 它应该像任何数字* 5一样,如1 * 5 = 5 2 * 5 = 10

但问题是,如果我将数量更改为2,它将给我20美元的价格

数量3它会给我45的价格,而它的价格应该是15我试图搜索问题找不到任何东西

/**
 * IMPORTANT: Make sure you are using the correct package name.
 * This example uses the package name:
 * package com.example.android.justjava
 * If you get an error when copying this code into Android studio, update it to match teh package name found
 * in the project's AndroidManifest.xml file.
 **/

package justjava.andriod.example.com.justjava;


import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.text.NumberFormat;

import justjava.andriod.example.com.justjava.R;

/**
 * This app displays an order form to order coffee.
 */
public class MainActivity extends AppCompatActivity {
    int quantity = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    /**
     * This method is called when the plus button is clicked.
     */
    public void increment (View view) {
        // this method to block the user to use increamnt method if the number is 100
        if (quantity==100){
           //show an error message as a toast
            Toast.makeText(this, "you cannot have more than 100 coffes", Toast.LENGTH_SHORT).show();
            // Exit this method early because there's nothing left to do
            return;
        }
        quantity = quantity+1;
        display(quantity);

    }
    /**
     * This method is called when the minus button is clicked.
     */
    public void decrement (View view) {
// this if statment block user to make decremnt method below number 1 so it wont go to 0
        if (quantity==1) {
            // show an error message as a toast
            Toast.makeText(this, "you cannot have more less than 1 coffee", Toast.LENGTH_SHORT).show();
            // Exit this method early because there's nothing left to do
            return;
        }
        quantity =  quantity - 1;
        display(quantity);
    }



    /**
     * Calculates the price of the order.
     *@return total price

     */
    private String creatOrderSummary(String name, int basePrice, boolean addWhippedCream , boolean addCocolate) {
        String priceMessage = "Name : " + name;
        priceMessage += "\nAdd Whipped Cream? " + addWhippedCream;
        priceMessage += "\nadd chocolate? " + addCocolate;
        priceMessage = priceMessage + "\nQuantity" + quantity;
        priceMessage = priceMessage + "\nTotall: $"+ basePrice;
        priceMessage = priceMessage + "\nthank you!";
        return priceMessage;


    }


    /**
     * Calculates the price of the order.
     *@return total price

     */
    private int calculatePrice(boolean addWhippedCream, boolean addChocolate ) {
        // pice of 1 cup of coffe
        int basePrice = quantity *5;
        // add $1 if the user wants whipped cream
        if (addChocolate) {
            basePrice = basePrice + 1;
        }
        // add $2 if the user wants chocolate
        if (addChocolate) {
            basePrice = basePrice + 2;
        }

        // calcuate the tottal order price by multiplying by quantity
        return quantity * basePrice;

    }



    /**
     * This method is called when the order button is clicked.
     */
    public void submitOrder(View view) {
        // find the user's name
        EditText text = (EditText) findViewById(R.id.name_field);
        String name = text.getText().toString();
        // figure it out if user wants whipped cream topping
        CheckBox whippedCreamCheckBox = (CheckBox) findViewById(R.id.whipped_cream_checkbox);
        boolean hasWippedCream = whippedCreamCheckBox.isChecked();
        Log.v("MainActivity", "Has whipped cream: " + hasWippedCream);
        // figure it out if user want chocolate
        CheckBox chocolate = (CheckBox) findViewById(R.id.add_chocolate);
        boolean hasCocolate = chocolate.isChecked();

        int price = calculatePrice(hasWippedCream, hasCocolate);
        String priceMessage = creatOrderSummary(name,price, hasWippedCream, hasCocolate);
        displayMessage(priceMessage);

    }

    /**
     * This method displays the given quantity value on the screen.
     */
    private void display(int number) {
        TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view);
        quantityTextView.setText("" + number);

    }





    /**
     * This method displays the given text on the screen.
     */
    private void displayMessage(String message) {
        TextView orderSummaryTextView = (TextView) findViewById(R.id.order_summary_text_view);
        orderSummaryTextView.setText(message);
    }
}

1 个答案:

答案 0 :(得分:0)

您将数量乘以两次。改变它:

 */
private int calculatePrice(boolean addWhippedCream, boolean addChocolate ) {
    // pice of 1 cup of coffe
    int basePrice = 5;
    // add $1 if the user wants whipped cream
    if (addChocolate) {
        basePrice = basePrice + 1;
    }
    // add $2 if the user wants chocolate
    if (addChocolate) {
        basePrice = basePrice + 2;
    }

    // calcuate the tottal order price by multiplying by quantity
    return quantity * basePrice;

}