我正在尝试使用方法转换多个指标,而且我被困住了,我不知道接下来该做什么?

时间:2016-11-04 08:48:15

标签: java

我的代码在这里是一个混乱我知道,但我与我的逻辑混淆,需要帮助尝试将它们组合在一起。所以任何帮助都表示赞赏。我希望输出为#34;

转换工具

1.Gallons to升

  1. 升加仑

  2. 茶匙到毫升

  3. Milliliters to teaspoons

  4. 请选择您要制作的转化类型:1

    请输入加仑:5

    总升数为18.925"

    import java.util.Scanner;
    public class ConversionProgram {
    
    public static final double LT_TO_GAL = .274;   
    public static final double GAL_TO_LT = 3.785;        
    public static final double TSP_TO_ML = 4.9289;
    public static final double ML_TO_TSP = .202;
    
    
    public static void main(String[] args) {
        int nInitLit = 0;                       
        int nInitGal = 0;                       
        int nTotalLit = 0;
        double dResult = 0;
        double dNumber1 = 0;
        double dNumber2 = 0;
        int nPrice = 0;
        double dTotalGal = 0.0;             
        Scanner input = new Scanner(System.in); 
    
        System.out.println("CONVERSION TOOL: ");   
        System.out.println("1. Gallons to liters ");
        System.out.println("2. Liters to gallons");
        System.out.println("3. Teaspoons to milliliters");
        System.out.println("4. Milliliters to teaspoons");
        System.out.print("");
    
        System.out.print("Please select the type of conversion you would like to make: ");
            int nConversion = input.nextInt();
    
        if (nConversion == 1) {        
            System.out.print("Please enter the gallons: ");
            double dGal = input.nextDouble();
    
        }
    
            else if (nConversion == 2) {
                System.out.print("Please enter the total liters: ");               
                double dLit = input.nextDouble();
            }
    
            else if (nConversion == 3 ) {
                System.out.print("Please enter the total teaspoons ");
                double dTsp = input.nextDouble();           
            }
    
            else if (nConversion == 4 ) {
                System.out.print("Please enter the total milliliters ");
                double dMil = input.nextDouble();               
            }
    
    
    } //end main method
    
    public static double calculateGallonsToLiters(double dGal) {
        double dTotGal = 0;                  
        dTotGal = dGal * GAL_TO_LT;
            return dTotGal;
        } 
    
    
    
    public static double calculateLitersToGallons(double dLit){
    
        double dTotLt = 0;              
        dTotLt = dLit * LT_TO_GAL;
            return dTotLt;
        } //end
    
    
    public static double calculateTeaspoonsToMliters(double dMlit){
    
        double dTotMl = 0;              
        dTotMl = dMlit * TSP_TO_ML;
            return dTotMl;
        } //end
    
    
    public static double calculateMlitersToTeaspoons(double dTsp){
        double dTotTsp = 0;             
        dTotTsp = dTsp * ML_TO_TSP;
            return dTotTsp;
        } //end
    
    
    public static void printResult(int nOpt, int nResultOperation){
            System.out.println("The total gallons is : " + nResultOperation);
    
        }//end method printResult} //end class
    

2 个答案:

答案 0 :(得分:0)

您还需要调用转换方法。 变化

public static void printResult(int nOpt, int nResultOperation){
    System.out.println("The total gallons is : " + nResultOperation);
}

public static void printResult(String unit, double result){
   System.out.println("The total " + unit + " is : " + result);
}

if (nConversion == 1) {        
    System.out.print("Please enter the gallons: ");
    double dGal = input.nextDouble();
}

if (nConversion == 1) {        
    System.out.print("Please enter the gallons: ");
    double dGal = input.nextDouble();
    double litres = calculateGallonsToLiters(dGal);
    printResult("litres", litres);
}

答案 1 :(得分:0)

除了Aidin给出的正确答案之外,我想建议使用switch语句而不是if / else并删除所有不需要的变量,并且不重要使代码可读:

import java.util.Scanner;
public class ConversionProgram {

    public static final double LT_TO_GAL = .274;   
    public static final double GAL_TO_LT = 3.785;        
    public static final double TSP_TO_ML = 4.9289;
    public static final double ML_TO_TSP = .202;


    public static void main(String[] args) {

        Scanner input = new Scanner(System.in); 

        System.out.println("CONVERSION TOOL: ");   
        System.out.println("1. Gallons to liters ");
        System.out.println("2. Liters to gallons");
        System.out.println("3. Teaspoons to milliliters");
        System.out.println("4. Milliliters to teaspoons");
        System.out.print("");

        System.out.print("Please select the type of conversion you would like to make: ");
        int nConversion = input.nextInt();

        switch (nConversion){
            case 1:
                System.out.print("Please enter the gallons: ");
                printResult("liters",calculateGallonsToLiters(input.nextDouble()));
                break;
            case 2:
                System.out.print("Please enter the total liters: ");
                printResult("gallons",calculateLitersToGallons(input.nextDouble()));
                break;
            case 3:
                System.out.print("Please enter the total teaspoons ");
                printResult("milliliters",calculateTeaspoonsToMliters(input.nextDouble()));
                break;
            case 4:
                System.out.print("Please enter the total milliliters ");
                printResult("teaspoons",calculateTeaspoonsToMliters(input.nextDouble()));
                break; 
            default:
                System.out.println("Not a valid option!");       
        }   
    }

    public static double calculateGallonsToLiters(double dGal) {
       return dGal * GAL_TO_LT;
    } 

    public static double calculateLitersToGallons(double dLit){
        return dLit * LT_TO_GAL;
    } 

    public static double calculateTeaspoonsToMliters(double dMlit){
        return dMlit * TSP_TO_ML;
    }

    public static double calculateMlitersToTeaspoons(double dTsp){
        return dTsp * ML_TO_TSP;
    }

    public static void printResult(String nOpt, double nResultOperation){
        System.out.println("The total " + nOpt + " is : " + nResultOperation);
    }
}