Prolog中的动态切换案例

时间:2016-05-26 14:09:32

标签: prolog switch-statement

如何使用 Prolog 编程语言实现切换案例?我对这种语言很陌生,这就是为什么我没有亲自尝试过! 假设我们想将此Java Switch Case更改为Prolog:

    int month = 8;
        String monthString;
        switch (month) {
            case 1:  monthString = "January";
                     break;
            case 2:  monthString = "February";
                     break;
            case 3:  monthString = "March";
                     break;
            case 4:  monthString = "April";
                     break;
            case 5:  monthString = "May";
                     break;
            case 6:  monthString = "June";
                     break;
            case 7:  monthString = "July";
                     break;
            case 8:  monthString = "August";
                     break;
            case 9:  monthString = "September";
                     break;
            case 10: monthString = "October";
                     break;
            case 11: monthString = "November";
                     break;
            case 12: monthString = "December";
                     break;
            default: monthString = "Invalid month";
                     break;
        }
        System.out.println(monthString);
    }
}

1 个答案:

答案 0 :(得分:1)

不要在Prolog中使用命令式编程。但是,命令式代码:

switch(N) {
    case 1: procedure1(); break;
    case 2: procedure2(); break;
    default: procedure(); break;
    }

可以写成:

switch(1) :-
  procedure1.
switch(2) :-
  procedure2.
switch(_) :-
  procedure;