gpt4 book ai didi

c - Switch Case 中的 If Else 和字符串

转载 作者:行者123 更新时间:2023-11-30 16:42:54 25 4
gpt4 key购买 nike

我有一个 switch case 语句,应该以字符串形式输入,例如:A、S、M、D、P。我尝试了多种方法来实现这项工作,据我所知, switch case 只接受整数。我尝试过枚举{};我尝试过 Serial.read/Serial.readString;我尝试过 charAt();但要么它们不起作用并导致更多错误,要么我只是不知道应该将它们放在哪里。因此,实际上是否可以更改我的代码,以便允许每种情况输入字符串?此时我已将它们标记为 1、2、3、4、5。

此外,我已经用尽了所有论坛和问题,以解决在循环()函数中的 switch 语句中包含 if/else 语句的问题。

我知道,我的代码非常困惑且冗长,但是当我输入 5 作为电位计读数时,它只会打印锅上的当前读数并打开该特定的灯,而不是我希望它连续打印出读数并在转动电位计时改变读数,同时在转动时打开或关闭 LED。

我已在单独的文件中(而不是在 switch case 中)尝试过此代码,并且它运行良好。我知道它可能会这样做,因为它不在 switch case 内的循环中,并且显然 switch case 以 break; 结束。但是我尝试过 Do While 循环,并且尝试过 while(1) 循环,但它仍然只会从 jar 中输出单个读数,然后再次循环回到开关盒的开头。

如有任何建议,我们将不胜感激,谢谢。

char dataInput() {
while (!Serial.available()) {
//wait for the user to enter value
;
}
return Serial.parseInt();
}

long firstNumber, secondNumber, value;

#define LED_RED 5
#define LED_ORANGE 6
#define LED_YELLOW 9
#define LED_GREEN 10
#define LED_BLUE 11

int potValue; // The output value of the potentiometer

/*int ledList[5] = {
LED_RED,
LED_ORANGE,
LED_YELLOW,
LED_GREEN,
LED_BLUE
};*/

#define potPin A0 // The pin the potentiometer is located

void setup() {

pinMode(LED_RED, OUTPUT);
pinMode(LED_ORANGE, OUTPUT);
pinMode(LED_YELLOW, OUTPUT);
pinMode(LED_GREEN, OUTPUT);
pinMode(LED_BLUE, OUTPUT);

pinMode(potPin, INPUT);

Serial.begin(9600);
}

void loop() {

Serial.println ("Enter your chosen operator");
Serial.println ("1 for Addition, 2 for Subtraction, 3 for Multiplication, and 4 for Division");
Serial.println ("5 for Potentiometer");

char theOperator = dataInput(); // Allows the user to enter their chosen operator

switch (theOperator) {

case 1: // Case 1 is for Addition
Serial.println ("A");
Serial.println ("Please enter first number in format nnn: ");
firstNumber = dataInput(); // Asks the user to input the first set of numbers
// Message must be in format cnnnnnn, therefore first number must be greater than or equal to -99 and less than or equal to 999
if (firstNumber >= -99 && firstNumber <= 999) {
Serial.println (firstNumber); // Prints the first set of numbers for the user to view
} else {
// If the data input does not match the format cnnnnnn then this error message will display
Serial.println ("--------------- ERROR ---------------");
break;
}

Serial.println ("Please enter second number in format nnn: ");
secondNumber = dataInput(); // Asks the user to input the second set of numbers
if (secondNumber >= -99 && secondNumber <= 999) {
Serial.println (secondNumber); // Prints the second set of numbers for the user to view
} else {
Serial.println ("--------------- ERROR ---------------");
break;
}

value = (firstNumber + secondNumber); // As this case is for addition, it will add the first and second numbers
Serial.print("Value: ");
Serial.println(value); // Prints the value of the two sets of numbers so that the user can see the value of their message
break;
/////////////////////////////////////////////////////////////////////////////////////////////

case 2: // Case 2 is for Subtraction
Serial.println ("S");
Serial.println ("Please enter the first number in format nnn: ");
firstNumber = dataInput();
if (firstNumber >= -99 && firstNumber <= 999) {
Serial.println (firstNumber); // Prints the first set of numbers for the user to view
} else {
Serial.println ("--------------- ERROR ---------------");
break;
}

Serial.println ("Please enter second number in format nnn: ");
secondNumber = dataInput();
if (secondNumber >= -99 && secondNumber <= 999) {
Serial.println (secondNumber); // Prints the second set of numbers for the user to view
} else {
Serial.println ("--------------- ERROR ---------------");
break;
}

value = (firstNumber - secondNumber); // As this case is for subtraction, it will subtract the first and second numbers
Serial.print("Value: ");
Serial.println(value);
break;
/////////////////////////////////////////////////////////////////////////////////////////////

case 3: // Case 3 is for Multiplication
Serial.println ("M");
Serial.println ("Please enter the first number in format nnn: ");
firstNumber = dataInput();
if (firstNumber >= -99 && firstNumber <= 999) {
Serial.println (firstNumber); // Prints the first set of numbers for the user to view
} else {
Serial.println ("--------------- ERROR ---------------");
break;
}

Serial.println ("Please enter second number in format nnn: ");
secondNumber = dataInput();
if (secondNumber >= -99 && secondNumber <= 999) {
Serial.println (secondNumber); // Prints the second set of numbers for the user to view
} else {
Serial.println ("--------------- ERROR ---------------");
break;
}

value = (firstNumber * secondNumber); // As this case is for multiplication, it will multiply the first and second numbers
Serial.print("Value: ");
Serial.println(value);
break;
/////////////////////////////////////////////////////////////////////////////////////////////

case 4: // Case 4 is for Division
Serial.println ("D");
Serial.println ("Please enter the first number in format nnn: ");
firstNumber = dataInput();
// Division by 0 is not allowed
if (firstNumber >= -99 && firstNumber <= 999 && firstNumber != 0) {
Serial.println (firstNumber); // Prints the first set of numbers for the user to view
} else {
Serial.println ("--------------- ERROR ---------------");
break;
}

Serial.println ("Please enter second number in format nnn: ");
secondNumber = dataInput();
// Division by 0 is not allowed
if (secondNumber >= -99 && secondNumber <= 999 && secondNumber != 0) {
Serial.println (secondNumber); // Prints the second set of numbers for the user to view
} else {
Serial.println ("--------------- ERROR ---------------");
break;
}

value = (firstNumber / secondNumber); // As this case is for division, it will divide the first and second numbers
Serial.print("Value: ");
Serial.println(value);
break;
/////////////////////////////////////////////////////////////////////////////////////////////

case 5: // Case 5 is for the Potentiometer
Serial.println("P");
potValue = analogRead(potPin); // Reads the value on the pin
potValue = map(potValue, 0, 1023, 0, 32); // Changes the default range from 0 to 1023 to 0 to 32

// The value depends on binary, but skipping 1
// 2, 4, 8, 16, 32
if (potValue >= 0 && potValue <= 2) {
Serial.println(potValue); // Prints the values of the pot, allowing user to know what value they are on
delay(500); // Slows down the reading

digitalWrite(LED_RED, HIGH);
digitalWrite(LED_ORANGE, LOW);
digitalWrite(LED_YELLOW, LOW);
digitalWrite(LED_GREEN, LOW);
digitalWrite(LED_BLUE, LOW);
} /////////////////////////////////////////

else if (potValue >= 3 && potValue <= 4) {
Serial.println(potValue);
delay(500);

digitalWrite(LED_RED, LOW);
digitalWrite(LED_ORANGE, HIGH);
digitalWrite(LED_YELLOW, LOW);
digitalWrite(LED_GREEN, LOW);
digitalWrite(LED_BLUE, LOW);
} /////////////////////////////////////////

else if (potValue >= 5 && potValue <= 8) {
Serial.println(potValue);
delay(500);

digitalWrite(LED_RED, LOW);
digitalWrite(LED_ORANGE, LOW);
digitalWrite(LED_YELLOW, HIGH);
digitalWrite(LED_GREEN, LOW);
digitalWrite(LED_BLUE, LOW);
} /////////////////////////////////////////

else if (potValue >= 9 && potValue <= 16) {
Serial.println(potValue);
delay(500);

digitalWrite(LED_RED, LOW);
digitalWrite(LED_ORANGE, LOW);
digitalWrite(LED_YELLOW, LOW);
digitalWrite(LED_GREEN, HIGH);
digitalWrite(LED_BLUE, LOW);
} /////////////////////////////////////////

else if (potValue >= 17 && potValue <= 32) {
Serial.println(potValue);
delay(500);

digitalWrite(LED_RED, LOW);
digitalWrite(LED_ORANGE, LOW);
digitalWrite(LED_YELLOW, LOW);
digitalWrite(LED_GREEN, LOW);
digitalWrite(LED_BLUE, HIGH);
}
break;
default:
Serial.println ("--------------- ERROR ---------------");
}
}

最佳答案

您的大小写需要与您要匹配的字符相对应。如果您想匹配 +,则需要 + 的 ascii 代码,即 43,如此处 http://www.asciitable.com/ 所示。 。在 C 中,字 rune 字会自动转换,因此您需要使用 case 43:case '+':+ 替换为任何内容您想要检测的字符。

请记住使用单引号,因为它表示字 rune 字而不是字符串文字。

关于c - Switch Case 中的 If Else 和字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45645087/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com