java 有没有更好的方法在java中解决这个问题?https://www.reddit.com/r/dailyprogrammer/comments/7hhyin/20171204_challeng

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 有没有更好的方法在java中解决这个问题?https://www.reddit.com/r/dailyprogrammer/comments/7hhyin/20171204_challeng相关的知识,希望对你有一定的参考价值。




public class Tests {


    public static void main(String[] args) {

        //long start = System.nanoTime();
        System.out.println(note("B", "Ti"));
        //long end = System.nanoTime();
        //System.out.println(end - start);

    }

    private static String note(String majorScale, String note) {

        String[] notes = {"C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "B"};

        int base = 0;
        int shift = 0;
        int result = 0;

        switch (majorScale) {
            case "C":
                base = 0;
                break;
            case "C#":
                base = 1;
                break;
            case "D":
                base = 2;
                break;
            case "D#":
                base = 3;
                break;
            case "E":
                base = 4;
                break;
            case "F":
                base = 5;
                break;
            case "F#":
                base = 6;
                break;
            case "G":
                base = 7;
                break;
            case "G#":
                base = 8;
                break;
            case "A":
                base = 9;
                break;
            case "A#":
                base = 10;
                break;
            case "B":
                base = 11;
                break;
        }

        switch (note) {
            case "Do":
                shift = 0;
                break;
            case "Re":
                shift = 2;
                break;
            case "Mi":
                shift = 4;
                break;
            case "Fa":
                shift = 5;
                break;
            case "So":
                shift = 7;
                break;
            case "La":
                shift = 9;
                break;
            case "Ti":
                shift = 11;
                break;
        }

        if (base + shift > notes.length - 1) {
            result = base + shift - notes.length;
        }
        else {
            result = base + shift;
        }

        return notes[result];
    }




}




以上是关于java 有没有更好的方法在java中解决这个问题?https://www.reddit.com/r/dailyprogrammer/comments/7hhyin/20171204_challeng的主要内容,如果未能解决你的问题,请参考以下文章

用Java复制文件的标准简洁方法?

在 Java 中使用 Mysql 和 servlet [重复]

消灭空指针,Java 8 给我们更好的解决方案

Java判断一个值有没有在集合里

Java中String类(字符串操作)的10个常见问题和解决方法

有没有更好的方法在 Java 中进行空检查? [复制]