6
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

JavaでPPAP

Last updated at Posted at 2016-11-23

ちょうどPPAPがCMで流れてたので。

enumを定義

enum PPAP_STATE {
    UNKNOWN,
    PEN,
    PINEAPPLE,
    APPLE,
    PPAP;

    static PPAP_STATE getState(PPAP_STATE state, int talking) {
        if (state == UNKNOWN && talking == 0) {
            return PEN;
        } else if (state == PEN && talking == 1) {
            return PINEAPPLE;
        } else if (state == PINEAPPLE && talking == 2) {
            return APPLE;
        } else if (state == APPLE && talking == 0) {
            return PPAP;
        } else {
            return UNKNOWN;
        }
    }
}

処理部分

enumのおかげで本処理はすっきり!

PPAP_STATE state = PPAP_STATE.UNKNOWN;
Random random = new Random();

while (true) {
    int talking = random.nextInt(3);
    Log.i("talk: ", new String[]{"ペン", "パイナップル", "アップル"}[talking]);

    state = PPAP_STATE.getState(state, talking);
    if (state == PPAP_STATE.PPAP) {
        Log.i("finish: ", "Pen Pineapple Apple Pen!");
        break;
    }
}

結果

Screenshot_1479921556.png

追記

(本当は、 PPAP_STATE.getState() の処理を switch文 でやりたかった...)

switch(int a, int b) {
    case (0, 0):
        // do
    case (1, 1):
        // do
    default:
        // do
}

Javaでこんな形でかけるのかがわからなくて断念した...

GitHub Gist

参考

Swift3でPPAPキヨシ

6
6
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?