Ich habe einige der anderen Fragen nach einem ähnlichen Problem durchgesehen, aber ich versuche, den doppelten "ThirdPrice" von calculationMethod() zu main() aufzurufen. Der Zweck dieses Programms besteht darin, Daten in main() anzufordern, einige der Informationen an calculationMethod() zu übergeben und diese Daten dann zur endgültigen Ausgabe an main() zurückzugeben. Ich benutze DrJava, hier ist mein Code soweit.Kann Daten von zweiter Zeichenfolge nicht aufrufen
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(); //Passes variable for calculation
System.out.println("Enter the cost of materials prior to discount "); //Enter cost of materials
costMaterials = inputDevice.nextDouble(); //Passes variable for calculation
System.out.println("Enter the number of hours worked "); //Enter hours worked
hoursWorked = inputDevice.nextDouble(); //Passes variable for calculation
System.out.printf("The cost of " + productName + " is %.2f\n" , thirdPrice);
//Output product name and cost
}
public static void 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 * 7; //Calculates fisr portion of equation
double secondPrice = 7 + 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
}
}
Die Fehler, die ich erhalten, wenn sie versuchen zu kompilieren wie folgt:
2 Fehler gefunden: Datei: C: \ Users \ unkno \ DrJava \ Java \ CraftPricing.java [line: 18] Fehler: kann nicht Symbol findet Symbol: variable thirdPrice Ort: Klasse CraftPricing Datei: C: \ Benutzer \ unkno \ DrJava \ Java \ CraftPricing.java [line: 28] Fehler: inkompatible Typen: unerwarteter Rückgabewert
Sie können nicht von einer void-Methode zurückgeben –
'thirdPrice' nicht definiert ist und Sie versuchen, es in der' main' Methode zu verwenden. –
Verstehst du, was 'void' bedeutet, oder tust du einfach blind die in deinem Code bekannten Sounds, ohne zu verstehen, was es bedeutet und tut? –