Java度量单位计算器

时间:2015-08-05 17:50:58

标签: java calculator units-of-measurement measure

如何更改此代码以使变量operation2在第二个switch函数中运行。 当我parseDouble时,它会收到错误bla bla parsedouble 即使我尝试创建全局operation2,它也无法正常工作。

package matura;

import java.util.Scanner;

public class MeasureUnits {

public static void main(String args[])

{
    String operation;
    String operation2;

    Scanner value = new Scanner(System.in);
    System.out.println(" Write value: :");

    double wartość;
    wartość = value.nextDouble();

    double yard, inch, foot, mile;
    double metr, decymetr, centymetr, kilometr;

    centymetr = 1;

    metr = 100 * centymetr;
    centymetr = 1;
    kilometr = centymetr * 100000;
    decymetr = 10 * centymetr;

    yard = 0.9144 * metr;
    inch = 2.54 * centymetr;
    mile = 1.609344 * kilometr;
    foot = 30.48 * centymetr;

    Scanner input1 = new Scanner(System.in);
    System.out.println(" Choose one of measure units" + " 'yard' 'inch' 'feet' 'mile' :");
    operation2 = input1.next();

    switch (operation2) {
    case "yard":
        System.out.println("You've choosed 'yard' ");
        break;
    case "inch":
        System.out.println(" You've choosed 'inch'");
        break;

    case "mile":
        System.out.println(" You've choosed 'mile'");
        break;

    case "foot":
        System.out.println("You've choosed 'foot'");
        break;

    }

    {

        Scanner input2 = new Scanner(System.in);
        System.out.println(" Choose one of polish measure units" + " 'metr' 'centymetr' 'decymetr' 'kilometr' :");
        operation = input2.next();

        switch (operation) {
        case "metr":
            System.out.println(operation2 * wartość / 100);
            break;

        case "centymetr":
            System.out.println(operation2 * wartość / 100);
            break;

        case "kilometr":
            System.out.println(operation2 * wartość / 100000);
            break;

        case "decymetr":
            System.out.println(operation2 * wartość / 10);
            break;

        }

    }
}

1 个答案:

答案 0 :(得分:0)

您是否看过JSR 363和Java测量单位项目:http://unitsofmeasurement.github.io/

参考实施和其他单位系统(SI,US等)包含Java中的所有相关单位定义,如果您需要更多,您可以在API之上定义自己的单位定义。