2016-11-04 32 views
-2

我在這裏有我的代碼這是一個混亂,我知道,但我與我的邏輯混淆,需要幫助,試圖讓這一起。所以,任何幫助表示讚賞。我想輸出是這個 「我想轉換多個指標使用方法,我卡住我不知道下一步該怎麼做?

轉換工具

1.Gallons到升

  • 升至加侖

  • 茶匙毫升

  • 毫升至茶匙

  • 請選擇轉換類型,你想作: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 
    
    +0

    你目前的輸出是什麼? – Aidin

    +0

    「轉換工具 1.Gallons到升 公升轉加侖 茶匙毫升 毫升至茶匙 請選擇轉換類型,你想作:1 請輸入加侖:5 」它得到到目前爲止 – Nick

    回答

    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); 
    } 
    
    +0

    這有幫助,謝謝。我需要一個打印結果的方法,我如何在每次轉換的代碼中使用它? – Nick

    +0

    @NicholasNPeck查看變化 – Aidin

    +0

    非常感謝你的讚賞! – Nick

    0

    除了由Aidin給予我想建議使用switch語句,而不是正確的答案我的f/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); 
        } 
    } 
    
    +0

    不錯的選擇。但是,最好總是使用'default'結束交換機 – Aidin

    +0

    真棒謝謝你們! – Nick

    相關問題