将用户指定的时间单位转换为另一个时间单位(秒到几周,几周到几小时等)

时间:2018-05-25 09:38:59

标签: java if-statement switch-statement

这里有Java新手。我编写了一个时间转换程序,它接受0到9e + 18之间的任何数字作为有效输入,并要求用户提供转换参数,例如从几周到几秒。在do-while循环中的一大块if-else-if-else行中写下所有计算。只要用户输入“q'退出。我目前的程序在不同的时间单位之间完美地进行转换。

我的问题是,我如何利用switch语句来代替if-else这个可怕的块呢?

if (fromUnit.equals("w") && toUnit.equals("s")) {
        results = userNumber * SECONDS_PER_WEEK;
    System.out.printf("There are %,.2f seconds", results); 
    System.out.printf(" in %,d %s.\n", userNumber, userNumber == 1 ?          "week" : "weeks");
        } else if (fromUnit.equals("h") && toUnit.equals("s")) {
    results = userNumber * SECONDS_PER_HOUR;
            System.out.printf("There are %,.2f seconds", results);
            System.out.printf(" in %,d %s.\n", userNumber, userNumber == 1 ? "hour" : "hours");
    } else if (fromUnit.equals("d") && toUnit.equals("s")) {
            results = userNumber * SECONDS_PER_DAY;
            System.out.printf("There are %,.2f seconds", results);
            System.out.printf(" in %,d %s.\n", userNumber, userNumber == 1 ? "day" : "days");
    } else if (fromUnit.equals("m") && toUnit.equals("s")) {
            results = userNumber * SECONDS_PER_MINUTE;
            System.out.printf("There are %,.2f seconds", results);
            System.out.printf(" in %,d %s.\n", userNumber, userNumber == 1 ? "minute" : "minutes");
    } else if (fromUnit.equals("s") && toUnit.equals("s")) {
            results = userNumber * SECONDS_PER_SECOND;
            System.out.printf("There are %,.2f seconds per second.\n", results);
    } else if (fromUnit.equals("w") && toUnit.equals("m")) {
            results = userNumber * MINUTES_PER_WEEK;
            System.out.printf("\nThere are %,.2f minutes", results);
            System.out.printf(" in %,d %s.\n", userNumber, userNumber == 1 ? "week" : "weeks");
    } else if (fromUnit.equals("d") && toUnit.equals("m")) {
            results = userNumber * MINUTES_PER_DAY;
            System.out.printf("\nThere are %,.2f minutes", results);
            System.out.printf(" in %,d %s.\n", userNumber, userNumber == 1 ? "day" : "days");
    } else if (fromUnit.equals("h") && toUnit.equals("m")) {
            results = userNumber * MINUTES_PER_HOUR;
            System.out.printf("\nThere are %,.2f minutes", results);
            System.out.printf(" in %,d %s.\n", userNumber, userNumber == 1 ? "hour" : "hours");
    } else if (fromUnit.equals("m") && toUnit.equals("m")) {
            results = userNumber * MINUTES_PER_MINUTE;
    System.out.printf("\nThere are %,.2f minutes", results);
            System.out.printf(" in %,d %s.\n", userNumber, userNumber == 1 ? "minute" : "minutes");
        } else if (fromUnit.equals("h") && toUnit.equals("h")) {
            results = userNumber * HOURS_PER_HOUR;
    System.out.printf("\nThere are %,.2f hours in %,d hours.\n", results, userNumber);
    } else if (fromUnit.equals("d") && toUnit.equals("h")) {
            results = userNumber * HOURS_PER_DAY;
    System.out.printf("\nThere are %,.2f hours in %,d days.\n", results, userNumber);
    } else if (fromUnit.equals("w") && toUnit.equals("h")) {
            results = userNumber * HOURS_PER_WEEK;
    System.out.printf("\nThere are %,.2f hours in %,d weeks.\n", results, userNumber);
    } else if (fromUnit.equals("d") && toUnit.equals("d")) {
            results= userNumber * DAYS_PER_DAY;
    System.out.printf("\nThere are %,.2f days in %,d days.\n", results, userNumber);
    } else if (fromUnit.equals("w") && toUnit.equals("d")) {
            results = userNumber * DAYS_PER_WEEK;
    System.out.printf("\nThere are %,.2f days in %,d weeks.\n", results, userNumber);
    } else if (fromUnit.equals("s") && toUnit.equals("ms")) {
            results = userNumber * MICROSECONDS_PER_SECOND;
            System.out.printf("\nThere are %,.3f microseconds in %,d seconds.\n", results, userNumber);
    } else if (fromUnit.equals("d") && toUnit.equals("w")) {
            results = userNumber * WEEKS_PER_DAY;
            System.out.printf("\nThere are %,.3f weeks in %,d days.\n", results, userNumber);
        } else if (fromUnit.equals("h") && toUnit.equals("d")) {
            results = userNumber * DAYS_PER_HOUR;
            System.out.printf("\nThere are %,.3f days in %,d hours.\n", results, userNumber);
    } else if (fromUnit.equals("s") && toUnit.equals("h")) {
            results = userNumber * HOURS_PER_MINUTE * MINUTES_PER_SECOND;
            System.out.printf("\nThere are %,.3f hours in %,d seconds.\n", results, userNumber);                
    } else if (fromUnit.equals("ms") && toUnit.equals("s")) {
            results = userNumber * SECONDS_PER_MICROSECOND;
            System.out.printf("\nThere are %,.3f seconds in %,d microseconds.\n", results, userNumber);
    } else if (fromUnit.equals("m") && toUnit.equals("ms")) {
    results = userNumber * MICROSECONDS_PER_MINUTE;
    System.out.printf("\nThere are %,.3f microseconds in %,d minutes.\n", results, userNumber);
        } else if (fromUnit.equals("h") && toUnit.equals("ms")) {
            results = userNumber * MICROSECONDS_PER_HOUR;
            System.out.printf("\nThere are %,.3f microseconds in %,d hours.\n", results, userNumber); 
        } else if (fromUnit.equals("d") && toUnit.equals("ms")) {
            results = userNumber * MICROSECONDS_PER_DAY;
            System.out.printf("\nThere are %,.3f microseconds in %,d days.\n", results, userNumber);
        } else if (fromUnit.equals("w") && toUnit.equals("ms")) {
            results = userNumber * MICROSECONDS_PER_WEEK;
            System.out.printf("\nThere are %,.3f microseconds in %,d weeks.\n", results, userNumber);
        } else {
            System.out.println("\nCannot calculate!\n");
    }

} while (userNumber >= 0);
}

}

1 个答案:

答案 0 :(得分:0)

使用switch语句的一种方法是:

<v-form ref="form" v-model="valid" lazy-validation>
        <v-select
            class="elevation-0 mt-border-bottom"
            v-model="phoneNumber"
            label="Add phone number"
            chips
            tags
            solo
            prepend-icon="phone"
            clearable
            data-vv-name="Phone Number"
            required
            :rules="inputRules"
        >
            <template slot="selection" slot-scope="data">
                <v-chip
                    close
                    outline
                    dark
                    @input="remove(data.item)"
                    :selected="data.selected"
                >
                    <strong>{{ data.item }}</strong>
                </v-chip>
            </template>
        </v-select>
        <v-btn @click.native="submitNewNumber">Test</v-btn>
    </v-form>

另一种方法是将数据放入String fromTo = fromUnit + toUnit; switch (fromTo) { case "ws": // weeks to seconds // ... break; case "hs": // hours to seconds // ... break; case "ds": // days to seconds // ... break; // ... }

HashMap<String, Integer>