如何不重复 if 语句并允许它以某种方式查找它们?我很新,我确定这是基本的
How to not repeat the if statements and allow it to look them up somehow? I am very new, im sure this is basic
我是新手,想知道是否有办法让代码查找 if 语句,而不必复制并通过它们
也非常感谢关于如何清理它的任何其他提示,而不是一个严肃的项目,只是玩弄和尝试学习
此代码是在 mbed 上使用 Nucleo 开发板完成的以获得输出
#include "mbed.h"
#include "../lib/uopmsb/uop_msb_2_0_0.h"
using namespace uop_msb_200;
Buzzer buzz;
DigitalOut greenLED(LED1);
DigitalOut blueLED(LED2);
DigitalOut redLED(LED3);
int main()
{ while(true)
{
greenLED = 1;
blueLED = 0;
redLED = 0;
if( greenLED == 1 )
{
buzz.playTone("C");
}
else {
NULL;
}
if( blueLED == 1 )
{
buzz.playTone("D");
}
else {
NULL;
}
if( redLED == 1 )
{
buzz.playTone("G");
}
else {
NULL;
}
wait_us(2500000);
greenLED = 0;
blueLED = 1;
redLED = 0;
if( greenLED == 1 )
{
buzz.playTone("C");
}
else {
NULL;
}
if( blueLED == 1 )
{
buzz.playTone("D");
}
else {
NULL;
}
if( redLED == 1 )
{
buzz.playTone("G");
}
else {
NULL;
}
wait_us(2500000);
greenLED = 0;
blueLED = 0;
redLED = 1;
if( greenLED == 1 )
{
buzz.playTone("C");
}
else {
NULL;
}
if( blueLED == 1 )
{
buzz.playTone("D");
}
else {
NULL;
}
if( redLED == 1 )
{
buzz.playTone("G");
}
else {
NULL;
}
wait_us(2500000);
greenLED = 0;
blueLED = 0;
redLED = 0;
if( greenLED == 1 )
{
buzz.playTone("C");
}
else {
NULL;
}
if( blueLED == 1 )
{
buzz.playTone("D");
}
else {
NULL;
}
if( redLED == 1 )
{
buzz.playTone("G");
}
else {
NULL;
}
}
}
你总是可以在 main 函数之前创建一个像这样的 void 函数,这样你就可以调用它,运行 里面有什么,然后 return 回到 main 函数。
void play(){
if( greenLED == 1 ){
buzz.playTone("C");
}
if( blueLED == 1 ){
buzz.playTone("D");
}
if( redLED == 1 ){
buzz.playTone("G");
}
}
所以你将像这样实现它,每次你想播放声音时调用该函数。
Buzzer buzz;
DigitalOut greenLED(LED1);
DigitalOut blueLED(LED2);
DigitalOut redLED(LED3);
void play(){
if( greenLED == 1 ){
buzz.playTone("C");
}
if( blueLED == 1 ){
buzz.playTone("D");
}
if( redLED == 1 ){
buzz.playTone("G");
}
}
int main(){
while(true){
greenLED = 1;
blueLED = 0;
redLED = 0;
play();
wait_us(2500000);
greenLED = 0;
blueLED = 1;
redLED = 0;
play();
wait_us(2500000);
然后迭代打开和关闭 LED 等。
我是新手,想知道是否有办法让代码查找 if 语句,而不必复制并通过它们
也非常感谢关于如何清理它的任何其他提示,而不是一个严肃的项目,只是玩弄和尝试学习
此代码是在 mbed 上使用 Nucleo 开发板完成的以获得输出
#include "mbed.h"
#include "../lib/uopmsb/uop_msb_2_0_0.h"
using namespace uop_msb_200;
Buzzer buzz;
DigitalOut greenLED(LED1);
DigitalOut blueLED(LED2);
DigitalOut redLED(LED3);
int main()
{ while(true)
{
greenLED = 1;
blueLED = 0;
redLED = 0;
if( greenLED == 1 )
{
buzz.playTone("C");
}
else {
NULL;
}
if( blueLED == 1 )
{
buzz.playTone("D");
}
else {
NULL;
}
if( redLED == 1 )
{
buzz.playTone("G");
}
else {
NULL;
}
wait_us(2500000);
greenLED = 0;
blueLED = 1;
redLED = 0;
if( greenLED == 1 )
{
buzz.playTone("C");
}
else {
NULL;
}
if( blueLED == 1 )
{
buzz.playTone("D");
}
else {
NULL;
}
if( redLED == 1 )
{
buzz.playTone("G");
}
else {
NULL;
}
wait_us(2500000);
greenLED = 0;
blueLED = 0;
redLED = 1;
if( greenLED == 1 )
{
buzz.playTone("C");
}
else {
NULL;
}
if( blueLED == 1 )
{
buzz.playTone("D");
}
else {
NULL;
}
if( redLED == 1 )
{
buzz.playTone("G");
}
else {
NULL;
}
wait_us(2500000);
greenLED = 0;
blueLED = 0;
redLED = 0;
if( greenLED == 1 )
{
buzz.playTone("C");
}
else {
NULL;
}
if( blueLED == 1 )
{
buzz.playTone("D");
}
else {
NULL;
}
if( redLED == 1 )
{
buzz.playTone("G");
}
else {
NULL;
}
}
}
你总是可以在 main 函数之前创建一个像这样的 void 函数,这样你就可以调用它,运行 里面有什么,然后 return 回到 main 函数。
void play(){
if( greenLED == 1 ){
buzz.playTone("C");
}
if( blueLED == 1 ){
buzz.playTone("D");
}
if( redLED == 1 ){
buzz.playTone("G");
}
}
所以你将像这样实现它,每次你想播放声音时调用该函数。
Buzzer buzz;
DigitalOut greenLED(LED1);
DigitalOut blueLED(LED2);
DigitalOut redLED(LED3);
void play(){
if( greenLED == 1 ){
buzz.playTone("C");
}
if( blueLED == 1 ){
buzz.playTone("D");
}
if( redLED == 1 ){
buzz.playTone("G");
}
}
int main(){
while(true){
greenLED = 1;
blueLED = 0;
redLED = 0;
play();
wait_us(2500000);
greenLED = 0;
blueLED = 1;
redLED = 0;
play();
wait_us(2500000);
然后迭代打开和关闭 LED 等。