2012-03-12 56 views
0

我正在爲我的課程之一使用旋轉式手機和arduino套件創建基於自動手機菜單的遊戲進行體驗設計項目。旋轉撥號的串行輸入通過arduino運行,現在我正在使用處理來編寫菜單。如何編寫嵌套if或case來提示輸入菜單?

我有一個行動綱要,並已開始編碼一些,如果然後語句去,但現在我偶然發現案件和切換。

我對這個完全陌生,但在課堂上學到了很多東西。

我的問題是如何製作嵌套的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"); 
     } 
} 

我的教練幫助我走到這一步,我已經沖刷有關如何繼續前進到下一個任務/提示答案。使用案例和切換會更容易嗎?我甚至在做嵌套的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; 
     } 
    } 
} 

我還在困惑如何使這個有水平,不是所有的同時發生。

回答

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; 
} 

最好是使用「否則,如果」使其不必通過雙方如果要檢查是否正確

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

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