2016-09-19 4 views
-7

Ich kann nicht herausfinden, warum die zweite Methode nicht ausgeführt wird. Es kompiliert und akzeptiert Eingaben, aber danach passiert nichts. Kann mir jemand in die richtige Richtung zeigen?Java Zweite statische Methode nicht ausgeführt

import javax.swing.JOptionPane; 

public class Test1 { 
    public static void main(String[] arg) { 
     String hoursOfWorkString, costOfMaterialsString, nameOfProductString; 

     nameOfProductString = JOptionPane.showInputDialog(null, "Enter the name of the product. ", JOptionPane.QUESTION_MESSAGE); 
     hoursOfWorkString = JOptionPane.showInputDialog(null, "Enter the number of hours worked. ", JOptionPane.QUESTION_MESSAGE); 

     double hoursOfWork = Double.parseDouble(hoursOfWorkString); 

     costOfMaterialsString = JOptionPane.showInputDialog(null, "Enter the cost of the product. ", JOptionPane.QUESTION_MESSAGE);   
     double costOfMaterials = Double.parseDouble(costOfMaterialsString); 
    } 



    public static void CalulatePrice(double costOfMaterials, double hoursOfWork, String nameOfProductString) { 
     double salePercentage = 0.75, shipping = 6, markup = 14, retailPrice; 

     retailPrice = salePercentage * (costOfMaterials + (markup * hoursOfWork)) + shipping; 

     JOptionPane.showMessageDialog(null, String.format("The retaill price of the %. product is $ %.02f.", nameOfProductString, retailPrice)); 
    } 
} 

Update: Ich habe das Beispiel verwendet und ein paar andere Fehler behoben und es läuft. Nachdem ich den Kommentar gelesen hatte, dachte ich nicht, dass es funktionieren würde. Ich wusste nur, dass mit meinem Code etwas nicht stimmte.

Vielen Dank für Ihre Eingaben.

+4

Es kann sein, weil Sie es nicht anrufen? – Li357

+0

Ich bin neugierig zu wissen, warum Sie denken, dass es ausgeführt werden würde. – Andreas

Antwort

2

Das ist, weil Sie Ihre Funktion definiert haben, aber Sie haben es nicht aufgerufen. Legen Sie einen Anruf am Ende Ihrer main Methode, wie

double costOfMaterials = Double.parseDouble(costOfMaterialsString); 
// Here... 
CalulatePrice(costOfMaterials, hoursOfWork, nameOfProductString); 

Auch, ich glaube, Sie gemeint berechnen. Und, bitte folgen Java Namenskonventionen, sollte es so etwas wie calculatePrice sein.

Verwandte Themen