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

时间:2017-05-14 01:28:42

标签: java

尝试将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
    }
}

4 个答案:

答案 0 :(得分:1)

让我们像这样宣布你的计算方法:

public static double calculationMethod(double costMaterials, double hoursWorked)

并在main中调用它:

calculationMethod(costMaterials, hoursWorked)

答案 1 :(得分:0)

这是因为您在main方法中扫描的输入对计算方法不可见。一种解决方案是将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
    }
}

答案 2 :(得分:0)

您可以通过以下方式简单地传递参数:

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 函数即可。或者,在类中声明它们是静态的也可以。

答案 3 :(得分:0)

尝试更改main无法正常工作的原因是Java无法弄清楚如何启动程序。要运行程序,Java会查找满足特定条件的方法:

  • 必须被称为main; AND
  • 必须声明static;和
  • 必须具有void返回类型; AND
  • 必须接受一个参数,该参数是String的数组。

通常使用名称args作为main的参数,但实际上您喜欢的任何名称都可以。但是,其他一切必须完全按照描述,或者Java不知道从哪里开始你的程序。