我收到此错误:
错误:类MainActivity中的方法createOrderSummary无法应用于给定类型;
必填:int,boolean
找到:int
原因:实际和形式参数列表的长度不同
这是我的代码:
public void submitOrder(View view) {
CheckBox whippedCreamCheckBox = (CheckBox) findViewById(R.id.whipped_cream_checkbox);
boolean hasWhippedCream = whippedCreamCheckBox.isChecked();
int price = calculatePrice();
String priceMessage = createOrderSummary(int price)
displayMessage(priceMessage);
}
private String createOrderSummary(int price, boolean addWhippedCream) {
String priceMessage = "Name: Samantha";
priceMessage += "\nAdd Whipped Cream?" + addWhippedCream;
priceMessage += "\nQuantity: " + quantity;
priceMessage += "\nTotal: $" + price;
priceMessage += "\nThank You!";
return priceMessage;
我认为,问题始于将布尔变量添加到字符串中。我不明白为什么布尔变量感谢您的帮助!
答案 0 :(得分:0)
传递这些值。我猜你只是通过int。 例子
调用函数
createOrderSummary(20, true);
答案 1 :(得分:0)
您对方法的调用应如下所示:
string SomeString = createOrderSummary(10, true);
您得到的错误基本上是因为您进行的调用缺少一个参数(可能在您的Main方法中):
//this is wrong. The second argument is missing.
string SomeString = createOrderSummary(10);
顺便说一句。您不能只添加字符串和布尔值。您必须先转换布尔值。
boolean addWhippedCream = true;
String str = String.valueOf(addWhippedCream);
System.out.println("The String is: "+ str);
或
System.out.println("The String is :" + String.valueOf(addWhippedCream));
答案 2 :(得分:0)
您粘贴的方法必须在一个类中,所以我将其嵌入到其中:
class A{
private String createOrderSummary(int price, boolean addWhippedCream) {
String priceMessage = "Name: Samantha";
priceMessage += "\nAdd Whipped Cream?" + addWhippedCream;
priceMessage += "\nQuantity: " + quantity;
priceMessage += "\nTotal: $" + price;
priceMessage += "\nThank You!";
return priceMessage;
}
}
要调用(调用)它,您需要创建一个A
类的对象,然后为该对象调用此方法。
例如:
A a = new A();
a.createOrderSummary(10, true);
new A().createOrderSummary(5, false);
您可以保存这样的调用结果:
string result1 = a.createOrderSummary(10, true);
string result2 = new A().createOrderSummary(5, false);