我在计算方法中遇到问题
它应该像给我总价一样 数量*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);
}
}
你乘以数量两次。 像这样更改它:
*/
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;
}