如何将参数传递给类方法并将值返回给主方法?

问题描述:

我在网上发现了一个随机任务,并将其作为实践来尝试和改进,作为一名编码员,但是我在这个任务的一个领域遇到了很多困难。它希望我“在单独的方法中进行计算和转换,将所需的信息从主方法传递到方法中,计算完成后,将结果返回到主要的打印方法中。”我一直在研究这个问题很长一段时间,如果我没有完成这个,那真的会让我烦恼,所以任何帮助都将不胜感激。如果这是一个愚蠢的问题,我很抱歉,我对编码很陌生。如何将参数传递给类方法并将值返回给主方法?

这里是我的代码:

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

如果你的任务是创建其他方法,那么你应该这样做。你只有一个方法,main。 –

+1

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

+0

当你在这,你可能要考虑多少代码的重复使用小的差异,你如何能够重构它。 – ChiefTwoPencils

作为一个例子,将以下代码在main方法:

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("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 

,并创建一个新的方法在你的班级是这样的:

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

然后,您可以添加更多的方法,或者如果你想(像从用户等采取了所需的输入),甚至做更多的工作,在这些方法中。

+0

该程序仍然工作后,我取代所有的方法,并添加新的类,所以我认为它现在就像它应该。非常感激你的帮助。谢谢 – cpitt6477

+0

@ cpitt6477,NP,如果您还有其他问题,请告知我。 – rohitvats