0

私はクラスの 1 つのエクスペリエンス デザイン プロジェクトに取り組んでおり、ロータリー式電話と arduino キットを使用して、自動化された電話メニューに基づいたゲームを作成しています。ロータリー ダイヤルからのシリアル入力は arduino を介して実行され、現在は処理を使用してメニューを記述しています。

私はアクションのアウトラインを持っており、いくつかの if then ステートメントをコーディングし始めましたが、今は case と switch に出くわしました。

私はこれにまったく慣れていませんが、クラスで多くのことを学びました。

私の質問は、ネストされた if/then ステートメントまたはユースケースとスイッチの連続セットを作成して、一連のプロンプトと入力を移動するにはどうすればよいですか?

これまでの私のスケッチは次のとおりです。

import processing.serial.*;

Serial port;  // Create object from Serial class
float val;    // Data received from the serial port

boolean task1prompted;
boolean task1;
boolean task2;
boolean dialed;

PFont font;

void setup() {
    size(800, 400);
    background(0, 0, 0);
    smooth();
    // IMPORTANT NOTE:
    // The first serial port retrieved by Serial.list()
    // should be your Arduino. If not, uncomment the next
    // line by deleting the // before it. Run the sketch
    // again to see a list of serial ports. Then, change
    // the 0 in between [ and ] to the number of the port
    // that your Arduino is connected to.
    //println(Serial.list());
    String arduinoPort = Serial.list()[0];
    port = new Serial(this, arduinoPort, 9600);

    task1 = false;
    task2 = false;
    task1prompted = false;

    font = createFont("Arial", 32);
    textFont(font, 32);
    textAlign(CENTER);
}

void draw() {
    if (port.available() > 0) { // If data is available,
        val = port.read();      // read it and store it in val
        if (val >= 48 && val <= 57) {
            val = map(val, 48, 57, 0, 9);  // Convert the value
        }
        println(val);
    }

    if (val == 97) {
        println("dialing");
    }

    if (val == 98){
        println("dialed");
        dialed = true;
    }

    /// switch will activate the task1 variable.
    // Play sound file for the prompt.
    if (task1prompted == false){
      delay(1000);
       println("for spanish, press one. for french, press 2...");
      task1prompted = true;
    }

    task1 = true;

    if (task1 == true && dialed == true) {
        ///play sound file

        if (val == 5) {
            println("Thank you for playing... Blah blah next prompt.");
            dialed = true;
            task1=false;
            task2=true;
        } else
            if (val != 5) {
            println("We're sorry, all of our international operators are busy");
            task1 = true;
            task2 = false;
            dialed = false;
        }

    }
    else
        if (task2 == true){
            delay(1000);
            println("task2 start");
        }
}

私のインストラクターは私がここまで到達するのを助けてくれました.次のタスク/プロンプトに進み続ける方法についての答えを探していました. case と switch を使用する方が簡単でしょうか? そして、ネストされた if ステートメントを正しい方法で実行していますか?

さて、私は次のようにスケッチとケースコマンドでこれを試しました:

    /// Switch will activate the task1 variable.
    //  Play sound file for the prompt.
    if (task1prompted == false){
        delay(1000);
        println("for spanish, press one. for french, press 2...");
        task1prompted = true;
    }

    task1 = true;

    if (task1 == true && dialed == true) {
        ///Play sound file

        int lang = (int)(val+0);

        switch(lang) {
            case 1:
            case 2:
            case 3:
            case 4:
                println("sorry no international operators");  // If 1-4 go back to choices
                task1 = true;
                task2 = false;
                dialed = false;
                break;
            case 5:
                println("thank you, move to next prompt");  // If 5 go to next prompt
                task1=false;
                task2=true;
                dialed = true;
                break;
            case 6:
            case 7:
            case 8:
            case 9:
            case 0:
                println("not a valid option, you lose");  // If not 1-5 go back to beginning
                task1=false;
                task2=false;
                dialed = true;
                break;
        }

        if (task2prompted == false){
            delay(1000);
            println("please listen while we test the line");
            task2prompted = true;
        }

        task2 = true;

        if (task2 == true && dialed == true) {
        } ///Play sound file

        int tone = (int)(val+0);

        switch(tone) {
          case 1:
          case 2:
          case 3:
          case 5:
          case 6:
          case 7:
          case 8:
          case 9:
          case 0:
            println("not a valid connection, you lose");  // If not 4 go back to beginning
            task2 = false;
            task3 = false;
            dialed = false;
            break;
          case 4:
            println("thank you, move to next prompt");  // If 4 move to next prompt
            task2=false;
            task3=true;
            dialed = true;
            break;
        }
    }
}

これにレベルを持たせ、すべてが同時に発生しないようにする方法については、まだ混乱しています。

4

2 に答える 2

1

有限状態マシンを調べたいと思うかもしれません。これは、イベント駆動型のユーザー インターフェイスを処理するための非常に一般的なアプローチです。

于 2012-03-15T23:26:19.477 に答える
0

あなたの質問が何であるかは完全にはわかりませんが、あなたが探しているものを明確にするだけでなく、ここで何かが答えてくれるかもしれません

case ステートメントを使用すると、発生する可能性のあるすべての出力に対してケースを作成する必要はありません。それを回避する方法は、デフォルトステートメントを使用することです

例:

    switch(tone) {
      case 4:
        println("thank you, move to next prompt");  // If 4 move to next prompt
        task2=false;
        task3=true;
        dialed = true;
        break;
      default:
        println("not a valid connection, you lose");  // If not 4 go back to beginning
        task2 = false;
        task3 = false;
        dialed = false;
    }

デフォルトのケースは最後にあるため、休憩は必要ありません。しかし、本質的には、他に何もヒットしない場合はすべてをキャッチします。

上記のコードの一部でも

if (val == 97) {
    println("dialing");
}

if (val == 98){
    println("dialed");
    dialed = true;
}

「else if」を使用して、どちらかが正しい場合に両方をチェックする必要がないようにすることをお勧めします

if (val == 97) {
    println("dialing");
}

else if (val == 98){
    println("dialed");
    dialed = true;
}
于 2012-07-31T15:12:15.630 に答える