Java 将数据从输入扫描仪传递到另一个方法

Java 将数据从输入扫描仪传递到另一个方法,java,Java,试图将成本材料和工时的输入从main传递到Calculation Method,以便正确计算第三个价格。我尝试将主字符串的void改为double,以便main可以返回值。那不起作用的原因我仍然不知道/不理解。我也在oracle网站上搜索了关于这个问题的教程,但似乎找不到。任何帮助都将不胜感激 import java.util.Scanner; //Imports input device public class CraftPricing { public static void ma

试图将成本材料和工时的输入从main传递到Calculation Method,以便正确计算第三个价格。我尝试将主字符串的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
    }
}

让我们这样声明您的CalculationMethod:

public static double calculationMethod(double costMaterials, double hoursWorked)
并将其主要称为:

calculationMethod(costMaterials, hoursWorked)

发生这种情况是因为您在主方法中扫描的输入对计算方法不可见。一种解决方案是将hoursWorked、costMaterials声明为类中的静态变量,这在main方法之外。那就行了

public class CraftPricing { 
 private static double hoursWorked;//<--Just add this
 private static double costMaterials; //<--Just add this
 public static void main(String[] args)
    {
        Scanner inputDevice = new Scanner(System.in); //Sets up input device
        String productName; //Used for naming product

        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 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(costMaterials, hoursWorked));
    //Output product name and cost
}
public static double calculationMethod(double costMaterials, double hoursWorked) //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
}

这里,您只需要将参数传递给calculationMethod函数。或者,在类中声明它们为静态也可以起作用。

尝试更改main失败的原因是Java不知道如何启动程序。要运行程序,Java会查找满足特定条件的方法:

它必须被称为主要的;和 它必须声明为静态的;和 必须有作废退回类型;和 它必须只接受一个字符串数组参数。
对于main的参数,通常使用名称args,但实际上任何您喜欢的名称都可以。但是,其他所有内容都必须完全符合说明,否则Java将不知道从何处启动程序。

发生这种情况是因为您在主方法中扫描的输入对计算方法不可见。一种解决方案是将hoursWorked、costMaterials声明为类中的静态变量,这在main方法之外。那就行了。谢谢你的帮助!您的解决方案允许程序正确编译和执行公式。我很高兴!:@Stich19您的解决方案帮助我编译了该程序,但它没有正确执行calculateMethod数学。无论我输入的成本材料和工作时间的数字是多少,我总是得到相同的答案6.00。