为了账号安全,请及时绑定邮箱和手机立即绑定

“实际和正式的参数列表在长度上有所不同”

“实际和正式的参数列表在长度上有所不同”

饮歌长啸 2022-08-17 17:32:06
我收到此错误:错误:方法 createOrderSummary 在类 MainActivity 中不能应用于给定的类型;必需:整型,布尔型已找到: 整型原因:实际参数列表和正式参数列表的长度不同这是我的代码: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;我认为,问题始于将布尔变量添加到字符串中。我不明白为什么布尔变量感谢您的帮助!
查看完整描述

3 回答

?
回首忆惘然

TA贡献1847条经验 获得超11个赞

您粘贴的方法需要位于类中,因此我将它嵌入到一个类中:


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);


查看完整回答
反对 回复 2022-08-17
?
心有法竹

TA贡献1866条经验 获得超5个赞

传递这些值。你只是通过int我猜。例

调用函数

createOrderSummary(20, true);


查看完整回答
反对 回复 2022-08-17
?
有只小跳蛙

TA贡献1824条经验 获得超8个赞

您对该方法的调用应如下所示:


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));


查看完整回答
反对 回复 2022-08-17
  • 3 回答
  • 0 关注
  • 255 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号