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

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));
添加回答
舉報