如何从方法菜单中调用方法?

时间:2017-06-07 21:39:07

标签: razor

我正在学习方法,并尝试制作一个简单转换的方法菜单。我不知道如何从菜单中调用方法并使其执行该功能。我想知道如何调用一个方法,并使用不使用扫描仪类的输入运行它,除了在main中。

CompositionTarget.Rendering

public static void main(String[] args) { displayMenu(); Scanner keyboard = new Scanner(System.in); int selection = keyboard.nextInt(); switch(selection) { case 1: FahrToCel(); break; case 2: CelToFahr(); break; case 3: LbToKilo(); break; case 4: KiloToLb(); break; } } public static void displayMenu() { System.out.println("Enter 1 for Fahrenheit to Celsius" ); System.out.println("Enter 2 for Celsius to Fahrenheit"); System.out.println("Enter 3 for lb to kg"); System.out.println("Enter 4 for kg to lb"); return; public static double FahrToCel() { double Fahrenheit = ???; double Celsius = 5 * (Fahrenheit - 32) / 9; return Celsius; } public static double CelToFahr() { double Celsius = ???; double Fahrenheit = (9 * Celsius / 5) + 32; return Fahrenheit; } public static double LbtoKilo() { double pounds = ???; double factor = 0.454; double Kilogram = pounds * factor; return Kilogram; } public static double KiloToLb() { double kilograms = ???; double factor = 0.454; double Pound = kilograms / factor; return Pound; } 是我想要获得用户输入的变量,但是idk如何在不在每个方法中创建新的Scanner的情况下执行此操作。

idk如何获取方法中的输入。

当我运行代码时,我得到displayMenu选项,我可以输入1,2,3,4,但之后没有任何反应

3 个答案:

答案 0 :(得分:2)

你可能想让你的代码像这样。您的问题是您实际上并没有提供任何要转换的值,但这应该解决它。

public static void main(String[] args) {
    displayMenu();
    double result;
    Scanner keyboard = new Scanner(System.in);
    int selection = keyboard.nextInt();
    System.out.println("What number do you want converted?");
    int toBeConverted = keyboard.nextInt();
    switch(selection) {
    case 1:
        result = fahrToCel(toBeConverted);
        break;
    case 2:
        result = celToFahr(toBeConverted);
        break;
    case 3:
        result = lbToKilo(toBeConverted);
        break;
    case 4:
        result = kiloToLb(toBeConverted);
        break;
   }
   System.out.println(result);

}

public static void displayMenu() {
    System.out.println("Enter 1 for Fahrenheit to Celsius" ); 
    System.out.println("Enter 2 for Celsius to Fahrenheit");
    System.out.println("Enter 3 for lb to kg");
    System.out.println("Enter 4 for kg to lb");
    return;
}

public static double fahrToCel(int fahrenheit) {

    double celsius = 5 * (fahrenheit - 32) / 9;  

    return celsius;

}

public static double celToFahr(int celsius) {
    double fahrenheit = (9 * celsius / 5) + 32;
    return fahrenheit;

}

public static double lbtoKilo(int lb) {
    double factor = 0.454;
    double kilogram = lb * factor;

    return kilogram;

}
public static double kiloToLb(int kilo) {
    double factor = 0.454;
    double pound = kilo / factor;
    return pound;

}  

我还将所有变量和方法名称都设置为小写,因为这是一个很好的编码实践。通常只有类名在传统的java实践中大写。没有实际的好处,但当然遵循惯例总是更好。

答案 1 :(得分:1)

正如@JoshKopen所说,你没有提供你想要转换的值,我的答案几乎是一样的,只是更具体一点,使用双值而不是int来获得更高的精度。

试试这个:

ID  year    count
a   2001    2
b   2001    3
a   2001    2
a   2001    1
a   2001    4 
a   2001    0
a   2001    1
a   2001    3
a   2001    2
b   2001    3
b   2001    4
a   2001    5
b   2001    2
c   2001    3
a   2001    5
a   2001    1
c   2001    1
c   2002    2
a   2002    1
b   2002    2
c   2002    3
a   2002    2
a   2002    1
a   2002    3
a   2002    4
a   2002    3
a   2002    2
b   2002    4
b   2002    2
a   2002    1
b   2002    1
c   2002    2
a   2002    3
a   2002    4
a   2002    5
c   2002    2
a   2003    2
a   2003    1
a   2003    2
a   2003    4
c   2003    3
a   2003    2
a   2003    1
b   2003    3
a   2003    5
a   2003    3
a   2003    2
a   2003    1
a   2003    3
a   2003    2
c   2003    2
b   2003    2
b   2003    1
b   2003    4
c   2003    3

答案 2 :(得分:0)

我刚刚去了,让菜单显得格格不入:)

package com.boeing.tbng.ddm.ms;

import java.util.Scanner;


public class StackOverflow {

    private static final int FACTOR_FARENHEIT = 32;
    private static final double FACTOR = 0.454;

    public static void main(String[] args) {
        StackOverflow so = new StackOverflow();
        int selection;

        Scanner keyboard = new Scanner(System.in);

        do{
            /* request user input */
            so.displayMenu();

            /* read input */
            selection = keyboard.nextInt();

            System.out.print(selection + ") ");
            /* choose path based on selection */
            switch(selection) {
                case 1:
                    so.FahrToCel(keyboard);
                    break;
                case 2:
                    so.CelToFahr(keyboard);
                    break;
                case 3:
                    so.LbToKilo(keyboard);
                    break;
                case 4:
                    so.KiloToLb(keyboard);
                    break;
                case 9:
                    System.out.println("Quitting Program...");
                    break;
            }
        }while (selection != 9);

    }

    private void displayMenu() {
        System.out.println("");
        System.out.println("===================");
        System.out.println("Select an Option:");
        System.out.println("1) for Fahrenheit to Celsius");
        System.out.println("2) for Celsius to Fahrenheit");
        System.out.println("3) for lb to kg");
        System.out.println("4) for kg to lb");
        System.out.println("9) to quit");
        System.out.println("===================");
    }

    private double askForNextInput(Scanner keyboard, String unit){
        System.out.println("Enter how many " + unit + " you want to convert");
        double userInput = keyboard.nextDouble();
        return userInput;
    }

    private double FahrToCel(Scanner keyboard) {
        double Fahrenheit = askForNextInput(keyboard, "Farenheit");
        double Celsius = 5 * (Fahrenheit - FACTOR_FARENHEIT) / 9;

        System.out.println( "In Celcius, that's: " + Celsius);
        return Celsius;
    }

    private double CelToFahr(Scanner keyboard) {
        double Celsius = askForNextInput(keyboard, "Celsius");
        double Fahrenheit = (9 * Celsius / 5) + FACTOR_FARENHEIT;
        System.out.println( "In Fahrenheit, that's: " + Fahrenheit);
        return Fahrenheit;

    }

    private double LbToKilo(Scanner keyboard) {
        double pounds = askForNextInput(keyboard, "Pounds");
        double Kilogram = pounds * FACTOR;

        System.out.println( "In Kilograms, that's: " + Kilogram);
        return Kilogram;

    }

    private double KiloToLb(Scanner keyboard) {
        double kilograms = askForNextInput(keyboard, "Kilograms");
        double Pound = kilograms / FACTOR;

        System.out.println( "In Pounds, that's: " + Pound);
        return Pound;
    }
}