看不懂计算方法

问题描述 投票:0回答:1

我的计算方法有问题

应该给我总价,比如数量5
它应该像任何数字
5一样 15 =5 25 =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 increment 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);
    }
}
android calculated-field
1个回答
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;

}
© www.soinside.com 2019 - 2024. All rights reserved.