2016-10-05 2 views
1

Ich bin mir ziemlich sicher, dass ich hier nur einen grundlegenden Fehler mache, aber ich kann den Rückgabewert in keiner meiner statischen Methoden erhalten, um die Hauptmethode zu erreichen. Meine Ausgabe für das Gebiet erscheint jedes Mal als Null. Was schief gelaufen ist?Methode Aufruf in einer Java-Klasse

Vielen Dank für jede Eingabe/Einsicht!

public class project_4 { public static void main (String [] args) {

Scanner input = new Scanner(System.in); 
    int choice = 0; 
    double celsius, fahrenheit=0, inches=0, centimeters=0, length=0, width=0, height=0, boxVolume=0, radius=0, sphereVolume=0, result=0; 
    double boxArea = box(result, length, width, height); 
    double sphereArea = sphere(result, radius); 
    char doAgain = 'y'; 

    while (doAgain == 'y' || doAgain == 'Y') 
    { 

    System.out.print(" Main Menu \n 1. Celsius to Fahrenheit \n 2. Inches to Centimeters \n " 
      + "3. Find Volume and Area of a box \n 4. Find Volume and Area of a sphere \n" 
      + "Please select one of the four choices \nThen press enter to continue"); 
    choice = input.nextInt(); 


    if (choice == 1) 
    { 
     System.out.println("This program will convert a celsius value into a fahrenheit one. \n"); 

     System.out.println("Please enter the tempature for conversion: "); 
     celsius = input.nextInt(); 

     fahrenheit = 1.8 * celsius + 32.0; 

     System.out.println(celsius + " degree(s) in celcius" + " is " + fahrenheit + 
       " in fahrenheit. \n"); 

     System.out.println("Do you want to continue?\n: " + 
        "enter \'Y\' for another round\n " + 
        " enter \'N\' to end the program\n"); 

     doAgain = input.next().charAt(0); 
    } 

    else if (choice == 2) 
    { 
     System.out.println("This program will convert inches to centimeters. \n"); 


     System.out.println("Please enter the length for conversion: "); 
     inches = input.nextInt(); 


     centimeters = 2.54 * inches; 


     System.out.println(inches + " inches is " + centimeters + " centimeters. \n"); 

     System.out.println("Do you want to continue?\n: " + 
        "enter \'Y\' for another round\n " + 
        " enter \'N\' to end the program\n"); 

     doAgain = input.next().charAt(0); 
    } 

    else if (choice == 3) 
    { 
     System.out.println("Enter length: "); 
     length = input.nextDouble(); 

     System.out.println("Enter height: "); 
     height = input.nextDouble(); 

     System.out.println("Enter width: "); 
     width = input.nextDouble(); 

     boxVolume = length * width * height; 

     System.out.println("The area of your box is: " + boxArea + "\n" 
       + "The volume of your box is: " + boxVolume); 

     System.out.println("Do you want to continue?\n: " + 
        "enter \'Y\' for another round\n " + 
        " enter \'N\' to end the program\n"); 
     doAgain = input.next().charAt(0); 

    } 

    else if (choice == 4) 
    { 
     System.out.println("Enter radius: "); 
     radius = input.nextDouble(); 

     sphereVolume = 4.0/3.0 * 3.14159 *Math.pow(radius, 3); 

     System.out.println("The area of your sphere is: " + sphereArea + "\n" 
       + "The volume of your sphereVolume is: " + sphereVolume); 

     System.out.println("Do you want to continue?\n: " + 
        "enter \'Y\' for another round\n " + 
        " enter \'N\' to end the program\n"); 
     doAgain = input.next().charAt(0); 
    } 

    else 
    { 
     System.out.println("That is not a valid option. Please restart and try again"); 

     } 
    } 
} 

public static double box(double result, double length, double width, double height) 
{ 
    result = length * width; 
    return result; 
} 

public static double sphere(double result, double radius) 
{ 
    result = 3.14 * (radius * radius); 
    return 0; 
} 

}

+1

Ähm, du rufst 'box' und' sphere', wenn die Werte aller Argumente 0 sind. Diese Methoden werden nicht jedesmal mit den "aktuellen" Variablenwerten aufgerufen, wenn du 'boxArea' und' sphereArea' verwendest '... –

+0

Ich dachte schon, aber ein Kollege der Klasse sagte, dass es diese Werte von der Hauptmethode nehmen würde. Kann ich die Werte in meiner Methodendeklaration/Signatur auf scannerObject.nextDouble setzen Oder müsste ich die Werte in meiner statischen Methode neu definieren? – recursivePython

Antwort

0

Die Variablen result, length, width, height Werte auf 0

So initialisiert wenn Sie box(result, length, width, height) anrufen, die ähnlich wie box(0, 0, 0, 0) anrufen ist. Außerdem müssen Sie keine result Variable senden, Sie können eine in der Methode selbst erstellen. Und senden Sie keine Höhenvariable, da Sie sie nicht innerhalb der Methode verwenden.

Innerhalb der Box Methode multipliziert man 0 mit 0, die wiederum gibt 0, die Sie zurückgeben. Gleiches gilt für Sphere Methode mit einer Sache, die return 0 zu return result geändert wird.

Beispielcode

double length = 5.0; 
double width = 5.0; 
double boxArea = box(length, width); 
System.out.println("Box Area is " + boxArea); 

public static double box (double length, double width){ 
    double result = length * width; 
    return result 
} 

Ausgang: Box Area is 25.0

Update: -

Wenn Sie wollen, dass die Benutzereingabe für length and width dann können Sie

den folgenden

Beispielcode tun

double length; 
double width; 
double boxArea; 

Scanner input = new Scanner(System.in); 

System.out.println("Enter the value for length and width") 

length = input.nextDouble(); 
width = input.nextDouble(); 

boxArea = box(length, width); 

// You can call box method inside main method with user input values of length and width. 
+0

Aber wie würde man die Benutzereingabe von der Hauptmethode in anderen Methoden verwenden? Ich brauche die Benutzereingabe von der Hauptmethode, um in meinen Bereich Methoden zu sein. Sie können nicht vom Programmierer definiert werden, da dies das Programm irrelevant machen würde. Und vielen Dank für die sehr gute Erklärung, wie die Variablen in einer Methode auf Null gesetzt werden. – recursivePython

+0

@JordanPritt Ich habe die Antwort aktualisiert. – Ravikumar

+0

Danke, ich habe es jetzt funktioniert. – recursivePython