gpt4 book ai didi

java - 将数据从输入扫描仪传递到另一种方法

转载 作者:行者123 更新时间:2023-12-02 13:11:04 25 4
gpt4 key购买 nike

尝试按顺序将 costMaterialshoursWorked 的输入从 main() 传递到 calculationMethod()正确计算thirdPrice。我尝试将主字符串的 void 更改为 double 以便 main() 可以返回值。由于我仍然不知道/理解的原因,这不起作用。我还在 Oracle 网站上搜索了有关此问题的教程,但似乎找不到。任何帮助将不胜感激。

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(); //Inputs product name
System.out.println("Enter the cost of materials prior to discount "); //Enter cost of materials
costMaterials = inputDevice.nextDouble(); //Inputs cost of materials
System.out.println("Enter the number of hours worked "); //Enter hours worked
hoursWorked = inputDevice.nextDouble(); //Inputs hours worked
System.out.printf("The cost of " + productName + " is %.2f\n" , calculationMethod());
//Output product name and cost
}
public static double 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 * hoursWorked; //Calculates first portion of equation
double secondPrice = costMaterials + 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
}
}

最佳答案

让我们像这样声明您的计算方法:

public static double calculationMethod(double costMaterials, double hoursWorked)

并在main中调用它:

calculationMethod(costMaterials, hoursWorked)

关于java - 将数据从输入扫描仪传递到另一种方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43959545/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com