Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我正在尝试使用方法和I';我卡住了,我不知道下一步该怎么办?_Java - Fatal编程技术网

Java 我正在尝试使用方法和I';我卡住了,我不知道下一步该怎么办?

Java 我正在尝试使用方法和I';我卡住了,我不知道下一步该怎么办?,java,Java,我在这里有我的代码,我知道这是一个混乱,但我对我的逻辑感到困惑,需要帮助,试图把这一切。因此,我们非常感谢您的帮助。我希望输出是这样的“ 转换工具 1.加仑对升 升到加仑 茶匙到毫升 毫升到茶匙 请选择要进行的转换类型:1 请输入加仑数:5 总升数为18.925“ 您还需要调用转换方法。 改变 进入 及 进入 除了Aidin给出的正确答案之外,我想建议使用switch语句,而不是if/else,并去掉所有不需要且不重要的变量,以使代码可读: import java.util.Scanner; p

我在这里有我的代码,我知道这是一个混乱,但我对我的逻辑感到困惑,需要帮助,试图把这一切。因此,我们非常感谢您的帮助。我希望输出是这样的“

转换工具

1.加仑对升

  • 升到加仑

  • 茶匙到毫升

  • 毫升到茶匙

  • 请选择要进行的转换类型:1

    请输入加仑数:5

    总升数为18.925“


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

    进入

    进入


    除了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);
        }
    }
    

    您当前的输出量是多少?“转换工具1.加仑到升升到加仑茶匙到毫升毫升毫升到茶匙请选择您要进行的转换类型:1请输入加仑:5”它得到了这个帮助,谢谢。我需要有一个方法来打印结果,我将如何在当前用于每次转换的代码中使用该方法?@NicholasNPeck see Changes谢谢!好的选择。但是,最好总是以
    默认值
    结束切换!
    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);
    }
    
    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);
        }
    }