尝试制作一个重量转换器,将以千克为单位的重量转换为石头、磅和盎司- Java

Trying to make a weight converter that converts a weight in kg to stones, pounds and ounces- Java

我想创建自己的转换器 class,它将从终端获取输入重量并将其转换为英石、磅和盎司。我想知道我会怎么做?

我将在这个 class 中创建一个名为转换器的方法,我想知道我的所有计算是否都应该在这个方法中,或者我是否需要更多方法?我很着急,我还没有正确地实现自己的方法。

另外,我可以将输入权重(使用 EasyIn 库)作为此方法的参数吗?

任何例子将不胜感激! 非常感谢!

您是在 eclipse 还是其他 IDE 中尝试控制台,而不是查看 system.in

class MyProg {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Printing the file passed in:");
        while(sc.hasNextLine()) System.out.println(sc.nextLine());
    }
}

欢迎来到堆栈溢出。幸运的是,您需要做的就是编写一些执行数学运算的基本方法。

public double convertStone();
{
     // conversion code here
}

您可以为每个需要的值重复该操作。对于转换代码,您只需要将 KG 到 LBS 的实际数学转换成如下代码:

kgToLbs = kg * 2.21;

希望对您有所帮助!

我从事过一个与此类似的项目。我所做的是在终端内提出一个问题,询问他们想要进行哪种转换,然后会提示一些 "if" 语句 运行 选定的转换。这可能不是您要查找的内容,但如果您不确定是否要输入自己的方法,它会很有用。

        String selection;
        double weight, stones;
        Scanner sc = new Scanner(System.in);

        //Print statements asking the user what conversion they would like to do
        selection = sc.nextLine();
            if(selection.equalsIgnoreCase("stones"))
            {
                //conversion
            }
            //etc