将switch case的值存储到变量中

时间:2016-03-24 14:00:51

标签: arduino switch-statement

我想将switch case中的值存储到变量中。以下是Arduino代码的一部分。

switch(i){
         case 0 :Serial.print("Time in UTC (HhMmSs): ");break;
         case 1 :Serial.print("Status (A=OK,V=KO): ");break;
         case 2 :Serial.print("Latitude: ");break;
         case 3 :Serial.print("Direction (N/S): ");break;
         case 4 :Serial.print("Longitude: ");break;
         case 5 :Serial.print("Direction (E/W): ");break;
         case 6 :Serial.print("Velocity in knots: ");break;
         case 7 :Serial.print("Heading in degrees: ");break;
         case 8 :Serial.print("Date UTC (DdMmAa): ");break;
         case 9 :Serial.print("Magnetic degrees: ");break;
         case 10 :Serial.print("(E/W): ");break;
         case 11 :Serial.print("Mode: ");break;
         case 12 :Serial.print("Checksum: ");break;
       }

我不想像现在这样打印案例0的值,但我想将案例0的值存储到变量中。请帮我。

1 个答案:

答案 0 :(得分:0)

声明一个String变量并在代码中使用它

String myvariable="";    
switch(i){
         case 0 : myvariable="Time in UTC (HhMmSs):";break;
...}

更新

我认为您可以使用substring和toInt方法来获取小时,分钟和秒的相应值

    String mytime = "121314";
    String temp = mytime.substring(0,2);//will give you "12"
    int hours = temp.toInt();
    temp = mytime.substring(2,4);//will give you "13"
    int minutes = temp.toInt();
    temp = mytime.substring(4);//will give you "14"
    int seconds = temp.toInt();

link to substring description

相关问题