Java Can';t从第二个字符串调用数据
我已经浏览了一些关于类似问题的其他问题,但我试图将双“thirdPrice”从calculationMethod()调用为main()。该程序的目的是请求main()中的数据,将一些信息传递给calculationMethod(),然后将该数据返回main()以进行最终输出。我正在使用DrJava,这里是我目前为止的代码Java Can';t从第二个字符串调用数据,java,Java,我已经浏览了一些关于类似问题的其他问题,但我试图将双“thirdPrice”从calculationMethod()调用为main()。该程序的目的是请求main()中的数据,将一些信息传递给calculationMethod(),然后将该数据返回main()以进行最终输出。我正在使用DrJava,这里是我目前为止的代码 import java.util.Scanner; //Imports input device public class CraftPricing { public
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[行:18]
错误:找不到符号
符号:可变第三方价格
地点:班级
文件:C:\Users\unkno\DrJava\Java\CraftPricing.Java[行:28]
错误:不兼容类型:意外返回值为什么需要变量 调用该方法
System.out.printf("The cost of %s is %.2f\n" , productName, calculationMethod());
或者了解变量范围
并修复返回类型
public static double calculationMethod()
您无法从无效方法返回
thirdPrice
未定义,并且您正试图在main
方法中使用它。您是否了解void
的含义,或者您只是盲目地在代码中键入任何听起来熟悉的内容,而不了解它的含义和作用?对于编码周期来说仍然非常陌生。回到我的课本上,读了关于“空虚”的书。现在我明白了为什么当我把它放在那里的时候它什么都没做。非常感谢你的帮助!我一直在翻阅我的课本,毫无头绪地阅读其他资源。没问题。您可以通过使用postWell旁边的复选标记接受答案来表示感谢。现在我有一个新问题,我忘记了我已将一些变量更改为精确的数字,以便消除一些错误。如果您无法修复它们,请随时创建新的帖子。不要在评论中提出新问题,除非是关于这个答案