gpt4 book ai didi

java - 如何在 if 语句中定义变量并在该语句之外使用它们?

转载 作者:行者123 更新时间:2023-11-30 04:00:28 24 4
gpt4 key购买 nike

if (difficulty.equals("easy")) {
System.out.println("Setting stats to " + difficulty);
int food = 3000;
int foodperson = 5;
int energy = 0;
int hulltotal = 5000;
int hullhealth = 5000;
int enemy1basehealth = 10;
int enemy1basedamage = 1;
int daystillevents = 80;
}

System.out.println("food: " + food);

当您尝试打印 food 变量时,您会收到错误。您如何才能使当您尝试打印它时,它能够成功打印正确的信息?

最佳答案

在 if 语句之外定义它们。

int food = 0;
int foodperson = 0;
int energy = 0;
int hulltotal = 0;
int hullhealth = 0;
int enemy1basehealth = 0;
int enemy1basedamage = 0;
int daystillevents = 0;

if(difficulty.equals("easy")){
System.out.println("Setting stats to " + difficulty);
food = 3000;
foodperson = 5;
energy = 0;
hulltotal = 5000;
hullhealth = 5000;
enemy1basehealth = 10;
enemy1basedamage = 1;
daystillevents = 80;
}
System.out.println("food: " + food);

关于java - 如何在 if 语句中定义变量并在该语句之外使用它们?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22121241/

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