无法从第二个字符串调用数据

时间:2017-05-13 23:56:44

标签: java

我已经查看了一些其他问题,询问了类似的问题,但我试图称之为双重'第三个价格'从calculationMethod()到main()。此程序的目的是在main()中请求数据,将一些信息传递给calculationMethod(),然后将该数据返回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:\ Users \ unkno \ DrJava \ Java \ CraftPricing.java [line:18] 错误:找不到符号   符号:变量thirdPrice   location:class CraftPricing 文件:C:\ Users \ unkno \ DrJava \ Java \ CraftPricing.java [line:28] 错误:不兼容的类型:意外返回值

1 个答案:

答案 0 :(得分:0)

为什么你需要一个变量?

调用方法。

System.out.printf("The cost of %s is %.2f\n" , productName, calculationMethod());

或了解变量范围。

并修复返回类型。

public static double calculationMethod()