3 回答

TA貢獻(xiàn)1847條經(jīng)驗(yàn) 獲得超11個(gè)贊
您粘貼的方法需要位于類中,因此我將它嵌入到一個(gè)類中:
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;
}
}
要調(diào)用(調(diào)用)它,您需要?jiǎng)?chuàng)建一個(gè)類的對(duì)象,然后為該對(duì)象調(diào)用此方法。A
例如:
A a = new A();
a.createOrderSummary(10, true);
new A().createOrderSummary(5, false);
您可以保存此類調(diào)用的結(jié)果:
string result1 = a.createOrderSummary(10, true);
string result2 = new A().createOrderSummary(5, false);

TA貢獻(xiàn)1866條經(jīng)驗(yàn) 獲得超5個(gè)贊
傳遞這些值。你只是通過int我猜。例
調(diào)用函數(shù)
createOrderSummary(20, true);

TA貢獻(xiàn)1824條經(jīng)驗(yàn) 獲得超8個(gè)贊
您對(duì)該方法的調(diào)用應(yīng)如下所示:
string SomeString = createOrderSummary(10, true);
您收到的錯(cuò)誤基本上表明您所做的調(diào)用缺少一個(gè)參數(shù)(可能在您的Main方法中):
//this is wrong. The second argument is missing.
string SomeString = createOrderSummary(10);
順便說一句。您不能只添加字符串和布爾值。您必須首先轉(zhuǎn)換布爾值。
boolean addWhippedCream = true;
String str = String.valueOf(addWhippedCream);
System.out.println("The String is: "+ str);
或
System.out.println("The String is :" + String.valueOf(addWhippedCream));
添加回答
舉報(bào)