java esp_在我的ESP游戏方法中非法开始表达

公共静态字符串guessColor(扫描仪键盘)是我的错误所在 . 错误如下:非法开始表达:公开,静态 . 错误:';'预期:在String和guessColor之间 . 错误';'预期:在扫描仪和键盘之间 . 非法开始表达:at)在那一行 .

public class RichardsonESPGame

{

public static void main(String[] args)

{

String random;

String guess;

Scanner keyboard = new Scanner(System.in);

random = computerColor();

guess = guessColor(keyboard);

String end = recordGame(random, guess);

System.out.println(end);

keyboard.close();

}

/**

In this method the color choices will be displayed.

*/

public static String computerColor()

{

Random rand = new Random();

int color = rand.nextInt(5) + 1;

return receiveColor(color);

}

/**

This method will give the computer and user its color options.

*/

public static String receiveColor(int color)

{

String selection;

switch (color)

{

case 1:

selection = "Red";

break;

case 2:

selection = "Blue";

break;

case 3:

selection = "Green";

break;

case 4:

selection = "Yellow";

break;

case 5:

selection = "Orange";

break;

default:

System.out.println("Make a valid selection.");

selection = null;

}

return selection;

/**

This method will get the user's color choice.

*/

public static String guessColor(Scanner keyboard)

{

System.out.println("Enter a number from 1 to 5 to receive a color.\n 1 is Red\n 2 is Blue\n 3 is Green\n 4 is Yellow\n 5 is Orange\n");

int richardsonGuessColor = keyboard.nextInt();

String color = receiveColor(richardsonGuessColor);

while (color == null)

{

System.out.println("Enter a number from 1 to 5 to receive a color.\n 1 is Red\n 2 is Blue\n 3 is Green\n 4 is Yellow\n 5 is Orange\n");

int guess = keyboard.nextInt();

color = receiveColor(richardsonGuessColor);

}

return color;

}

/**

This method will determine if user guesses correctly.

*/

public static String recordGame(String richardsonComputerColor, String richardsonGuessColor)

{

String end;

end = "The computer's choice was: " + richardsonComputerColor;

end += "Your choice was: " + richardsonGuessColor;

if(richardsonGuessColor.equalsIgnoreCase("Red"))

{

if (richardsonComputerColor.equalsIgnoreCase("Red"))

{

end += "You guessed correctly!";

}

else

{

result += "You guessed incorrectly.";

}

}

else if(richardsonGuessColor.equalsIgnoreCase("Blue"))

{

if (richardsonComputerColor.equalsIgnoreCase("Blue"))

{

end += "You guessed correctly!";

}

else

{

result += "You guessed incorrectly.";

}

}

else if(richardsonGuessColor.equalsIgnoreCase("Green"))

{

if (richardsonComputerColor.equalsIgnoreCase("Green"))

{

end += "You guessed correctly!";

}

else

{

result += "You guessed incorrectly.";

}

}

else if(richardsonGuessColor.equalsIgnoreCase("Yellow"))

{

if (richardsonComputerColor.equalsIgnoreCase("Yellow"))

{

end += "You guessed correctly!";

}

else

{

result += "You guessed incorrectly.";

}

}

else if(richardsonGuessColor.equalsIgnoreCase("Orange"))

{

if (richardsonComputerColor.equalsIgnoreCase("Orange"))

{

end += "You guessed correctly!";

}

else

{

result += "You guessed incorrectly.";

}

}

return end;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
`esp_intr_alloc` 是 ESP32 的 API 函数之一,用于配置和分配断(interrupt)资源。下面是 `esp_intr_alloc` 函数的使用方法: ```c esp_err_t esp_intr_alloc(int intr_source, int flags, void (*fn)(void*), void* arg, intr_handle_t* handle); ``` 参数说明: - `intr_source`:要分配的断源的编号,可以在 [ESP32 技术参考手册](https://www.espressif.com/sites/default/files/documentation/esp32_technical_reference_manual_en.pdf) 找到。例如,GPIO 0 的断源编号为 `ETS_GPIO_INTR_SOURCE`。 - `flags`:断标志,指定断类型和触发方式。例如,可以使用 `ESP_INTR_FLAG_LOWMED` 标志配置断为低电平触发或下降沿触发,使用 `ESP_INTR_FLAG_EDGE` 标志配置断为边沿触发。 - `fn`:断处理函数,当断发生时会调用该函数。 - `arg`:传递给断处理函数的参数。 - `handle`:用于保存分配的断资源句柄。 注意事项: - `esp_intr_alloc` 函数会动态分配断资源,使用完毕后要使用 `esp_intr_free` 函数释放。 - 断处理函数需要尽快完成,以避免影响系统的实时性能。 - 为避免断处理函数在执行时被其他断打断,可以使用 `portENTER_CRITICAL` 和 `portEXIT_CRITICAL` 函数进行临界区保护。 下面是 `esp_intr_alloc` 函数的一个简单示例: ```c #include "driver/gpio.h" #include "esp_intr_alloc.h" void IRAM_ATTR gpio_isr_handler(void* arg) { uint32_t gpio_num = (uint32_t) arg; printf("GPIO %d interrupt\n", gpio_num); } void app_main(void) { gpio_config_t gpio_cfg = { .pin_bit_mask = (1ULL << GPIO_NUM_0), .mode = GPIO_MODE_INPUT, .pull_up_en = GPIO_PULLUP_ENABLE, .intr_type = GPIO_INTR_NEGEDGE }; gpio_config(&gpio_cfg); intr_handle_t gpio_intr_handle; esp_intr_alloc(ETS_GPIO_INTR_SOURCE, ESP_INTR_FLAG_LOWMED, gpio_isr_handler, (void*) GPIO_NUM_0, &gpio_intr_handle); while(1) { vTaskDelay(1000 / portTICK_RATE_MS); } esp_intr_free(gpio_intr_handle); } ``` 以上代码将 GPIO 0 配置为下降沿触发断,并将断处理函数设置为 `gpio_isr_handler`。当 GPIO 0 引脚下降沿触发时,断处理函数会被调用。注意,由于断处理函数需要在断上下文执行,因此在函数声明前需要添加 `IRAM_ATTR` 关键字。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值