2017-05-14 5 views
0

。私はdoubleにメインストリングのためにvoidを変更して、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 
    } 
} 
+0

これは、メインメソッドでスキャンした入力が計算方法に表示されないために発生します。 1つの解決策は、メインメソッドの外側にあるhoursWorked、costMaterialsをクラス内の静的変数として宣言することです。その後、それは動作します。 –

答えて

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関数にパラメータを渡す必要があります。代わりに、それらをクラス内で静的宣言することもできます。

+0

助けてくれてありがとう!あなたのソリューションは、プログラムがコンパイルして式を正しく実行できるようにしました。 – Stich19

+0

私の喜び! :) @ Stich19 –

1

のは、このようなあなたのcalculatioinMethodを宣言してみましょう:

public static double calculationMethod(double costMaterials, double hoursWorked) 

とメインでそれを呼び出す:

calculationMethod(costMaterials, hoursWorked) 
0

をこれが起こっているあなたは、メインメソッドでスキャンした入力が、あるため、計算方法には表示されません。 1つの解決策は、メインメソッドの外側にあるhoursWorked、costMaterialsをクラス内の静的変数として宣言することです。その後、それは動作します。

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 
    } 
} 
+0

あなたのソリューションは私がプログラムをコンパイルするのを助けましたが、 'calculateMethod()'の計算を正しく実行しませんでした。 「costMaterials」と「hoursWorked」に入力した数字が何であっても、私はいつも同じ回答を6.00受け取った。 – Stich19

+0

これでコードを修正できますか? –

0

mainを変更しようとした理由は、Javaがプログラムの起動方法を理解できなかったためです。プログラムを実行するには、Javaはメソッドの会議を探し、一定の基準:

  • それmain呼び出さなければなりません。 AND
  • staticと宣言する必要があります。 AND
  • voidの返品タイプである必要があります。 AND
  • それはStringの配列です正確に一つ引数を受け入れる必要があります。

mainに引数の名前argsを使用する従来のですが、実際に好きな名前を行います。しかし、他のすべては記述されているとおりにする必要があります。そうでないと、Javaはプログラムをどこから起動するのかわかりません。

関連する問題