我已經瀏覽了一些其他問題,詢問了有關類似的問題,但我試圖將calculateMethod()的double'thirdPrice'稱爲main()。這個程序的目的是請求main()中的數據,將一些信息傳遞給calculateMethod(),然後將這些數據返回給main()以進行最終輸出。我正在使用DrJava,這是我的代碼。是不能從第二個字符串調用數據
import java.util.Scanner; //Imports input device
public class CraftPricing
{
public static void main(String[] args)
{
Scanner inputDevice = new Scanner(System.in); //Sets up input device
String productName; //Used for naming product
double costMaterials, hoursWorked; //Gives variables decimal format
System.out.println("Enter the name of the product "); //Enter product name
productName = inputDevice.nextLine(); //Passes variable for calculation
System.out.println("Enter the cost of materials prior to discount "); //Enter cost of materials
costMaterials = inputDevice.nextDouble(); //Passes variable for calculation
System.out.println("Enter the number of hours worked "); //Enter hours worked
hoursWorked = inputDevice.nextDouble(); //Passes variable for calculation
System.out.printf("The cost of " + productName + " is %.2f\n" , thirdPrice);
//Output product name and cost
}
public static void calculationMethod() //Method used to calcualte price
{
double itemDiscount = 0.75; //Gives decimal format to variable
double payRate = 14.00; //Gives decimal format to variable
double shipHandle = 6.00; //Gives decimal format to variable
double firstPrice = payRate * 7; //Calculates fisr portion of equation
double secondPrice = 7 + firstPrice; //Calculates second portion of equation
final double thirdPrice = itemDiscount * secondPrice + shipHandle;
//Calculates final portion of equation
return thirdPrice; //Returns double to main() for output
}
}
試圖編譯時收到的錯誤如下所示:實測值
2錯誤: 文件:C:\用戶\ unkno \ DrJava \爪哇\ CraftPricing.java [行:18] 錯誤:無法找到符號 符號:變量thirdPrice 位置:類CraftPricing 文件:C:\用戶\ unkno \ DrJava \的Java \ CraftPricing.java [行:28] 錯誤:不兼容的類型:意外的返回值
你不能從無效方法返回 –
'thirdPrice'沒有被定義,你試圖在'main'方法中使用它。 –
你知道'void'的意思麼,或者你只是盲目地輸入熟悉代碼的任何聲音而不理解它的意思和作用? –