2016-08-20 3 views
0

Ich habe eine zufällige Aufgabe online gefunden und verwende sie als Übung, um mich als Programmierer zu verbessern, aber ich habe große Schwierigkeiten mit diesem einen Bereich der Aufgabe. Ich möchte, dass ich die Berechnungen und Konvertierungen in separaten Methoden mache. Übergeben Sie die benötigten Informationen von der Hauptmethode an die Methode. Nach der Berechnung geben Sie das Ergebnis in die Hauptmethode zum Drucken zurück. Ich arbeite jetzt schon lange daran, und wenn ich das nicht mache, wird es mich wirklich nerven, also wird jede Hilfe sehr geschätzt. Ich entschuldige mich, wenn das eine dumme Frage ist, bin ich sehr neu in der Codierung.Wie Übergeben von Argumenten an Klassenmethoden und Rückgabe von Werten an die Hauptmethode?

Hier ist mein Code:

import java.util.Scanner; // Allows for use of scanner in class 

public class MeasurementConversion // Start of class 
{ 
    public static void main(String[]args) // Start of main 
    { 
     Scanner readConsole = new Scanner(System.in); // This is the scanner 
     String convert; 
     double poundAmount; // These are the doubles that store the measurements 
     double kilogAmount; 
     double ounceAmount; 
     double gramAmount; 
     double feetAmount; 
     double meterAmount; 
     double mileAmount; 
     double kilomAmount; 

     System.out.println("Hello, I am a conversion calculator. I can convert pounds to kilograms, ounces to grams,"); // This is where the scanner begins prompting the user for input 
     System.out.println("feet to meters, miles to kilometers, and vice versa."); 
     System.out.println("What would you like to convert?"); 
     convert = readConsole.nextLine(); 
     if (convert.equalsIgnoreCase("pounds to kilograms")) // This converts from pounds to kilograms 
     { 
      System.out.println("Ok, what is the amount of pounds you know?"); 
      poundAmount = readConsole.nextDouble(); 
      kilogAmount = poundAmount * .4536; 
      System.out.println("That is " + kilogAmount + " kilograms."); 
     } // End of if statement 
     if (convert.equalsIgnoreCase("kilograms to pounds")) // This converts from kilograms to pounds 
     { 
      System.out.println("Ok, what is the amount of kilograms you know?"); 
      kilogAmount = readConsole.nextDouble(); 
      poundAmount = kilogAmount * 2.20462; 
      System.out.println("That is " + poundAmount + " pounds."); 
     } // End of if statement 
     if (convert.equalsIgnoreCase("ounces to grams")) // This converts from ounces to grams 
     { 
      System.out.println("Ok, what is the amount of ounces you know?"); 
      ounceAmount = readConsole.nextDouble(); 
      gramAmount = ounceAmount * 28.5; 
      System.out.println("That is " + gramAmount + " grams.");  
     } // End of if statement 
     if (convert.equalsIgnoreCase("grams to ounces")) // This converts from grams to ounces 
     { 
      System.out.println("Ok, what is the amount of grams you know?"); 
      gramAmount = readConsole.nextDouble(); 
      ounceAmount = gramAmount * .035274; 
      System.out.println("That is " + ounceAmount + " ounces."); 
     } // End of if statement 
     if (convert.equalsIgnoreCase("feet to meters")) // This converts from feet to meters 
     { 
      System.out.println("Ok, what is the amount of feet you know?"); 
      feetAmount = readConsole.nextDouble(); 
      meterAmount = feetAmount * .3048; 
      System.out.println("That is " + meterAmount + " meters."); 
     } // End of if statement 
     if (convert.equalsIgnoreCase("meters to feet")) // This converts from meters to feet 
     { 
      System.out.println("Ok, what is the amount of meters you know?"); 
      meterAmount = readConsole.nextDouble(); 
      feetAmount = meterAmount * 3.28084; 
      System.out.println("That is " + feetAmount + " feet."); 
     } // End of if statement 
     if (convert.equalsIgnoreCase("miles to kilometers")) // This converts from miles to kilometers 
     { 
      System.out.println("Ok, what is the amount of miles you know?"); 
      mileAmount = readConsole.nextDouble(); 
      kilomAmount = mileAmount * 1.61; 
      System.out.println("That is " + kilomAmount + " kilometers."); 
     } // End of if statement 
     if (convert.equalsIgnoreCase("kilometers to miles")) // This converts from kilometers to miles 
     { 
      System.out.println("Ok, what is the amount of kilometers you know?"); 
      kilomAmount = readConsole.nextDouble(); 
      mileAmount = kilomAmount * .621371; 
      System.out.println("That is " + mileAmount + " miles."); 
     } // End of if statement 
    } // End of main 
} // End of class 
+2

Wenn Ihre Aufgabe ist, andere Methoden zu erstellen, dann sollten Sie das tun. Sie haben nur eine einzige Methode, Haupt. –

+1

https://docs.oracle.com/javase/tutorial/java/javaOO/methods.html – OldProgrammer

+0

Während Sie dabei sind, möchten Sie vielleicht prüfen, wie viel von diesem Code mit geringen Unterschieden wiederholt wird und wie Sie möglicherweise in der Lage sind umstrukturieren. – ChiefTwoPencils

Antwort

1

Als Beispiel den folgenden Code in Ihre Hauptmethode ersetzen:

if (convert.equalsIgnoreCase("pounds to kilograms")) // This converts from pounds to kilograms 
    { 
     System.out.println("Ok, what is the amount of pounds you know?"); 
     poundAmount = readConsole.nextDouble(); 
     kilogAmount = poundAmount * .4536; 
     System.out.println("That is " + kilogAmount + " kilograms."); 
    } // End of if statement 

mit:

if (convert.equalsIgnoreCase("pounds to kilograms")) // This converts from pounds to kilograms 
    { 
     System.out.println("Ok, what is the amount of pounds you know?"); 
     poundAmount = readConsole.nextDouble(); 
     kilogAmount = convertPoundToKilogram(poundAmount); // call method to do the calculation 
     System.out.println("That is " + kilogAmount + " kilograms."); 
    } // End of if statement 

und eine neue Methode erstellen in deiner Klasse so:

private static double convertPoundToKilogram(double poundAmount) { 
    return poundAmount * .4536; 
} 

Sie können dann weitere Methoden hinzufügen, oder sogar mehr Arbeit in diesen Methoden, wenn Sie möchten (wie die erforderlichen Eingaben vom Benutzer usw.).

+0

Das Programm funktioniert immer noch, nachdem ich alle Methoden ersetzt und die neue Klasse hinzugefügt habe, also gehe ich davon aus, dass es jetzt so funktioniert, wie es sollte. Ihre Hilfe wird sehr geschätzt. Danke – cpitt6477

+0

@ cpitt6477, NP, lassen Sie mich wissen, wenn Sie weitere Fragen haben .. – rohitvats

Verwandte Themen