如何更改我的 int 变量的值?
How can I change the value of my int variable?
在我的游戏中,我试图在收集木材时增加木材的价值,但我不知道如何做。这是我的代码:
package Main;
import java.util.Random;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
System.out.println("In order to build your house, you need 25 wood"); //I haven't added building the house yet
System.out.println("To gather wood type 'gather wood'. (no caps)");
while (true) {
Scanner scan = new Scanner(System.in);
Random Random = new Random();
String getWood = "gather wood";
int randomNumber = Random.nextInt(11);
int wood = 0;
String s = scan.nextLine();
if (s.contains( getWood )) {
System.out.println("You have gathered " + randomNumber + " wood!");
} else {
}
}
}
}
当我键入"gather wood"时,我希望它向int变量"wood"添加一个数量,最好是"System.out.println("中相同的随机数你收集了“+ randomNumber +”木头!");"
感谢任何帮助!
谢谢! :D
应该这样做
System.out.println("In order to build your house, you need 25 wood"); //I haven't added building the house yet
System.out.println("To gather wood type 'gather wood'. (no caps)");
int wood = 0;
while (true) {
Scanner scan = new Scanner(System.in);
Random Random = new Random();
String getWood = "gather wood";
int randomNumber = Random.nextInt(11);
String s = scan.nextLine();
if (s.contains( getWood )) {
System.out.println("You have gathered " + randomNumber + " wood!");
wood+=randomNumber ;
System.out.println("You now have " + wood + " wood!");
} else {
}
}
在 if 语句中它将是:
if (s.contains( getWood )) {
System.out.println("You have gathered " + randomNumber + " wood!");
wood = wood + randomNumber;
} else {
}
}
您可能想考虑创建一个 class 并访问 "wood" 变量?
你要在while循环之外声明变量wood,否则它总是被重置为零。
然后你可以像这样添加随机数:
wood += randomNumber;
这是以下的简称:
wood = wood + randomNumber;
将 int wood = 0;
放在循环之外。您在每个循环中创建一个值为 0 的新变量。
您还需要将 wood += randomNumber;
放在 if 语句中。否则木材的价值不变。
如何给 int 变量加一
int something = 1;
something++;
变量 something 现在为 2。
如何添加金额:
int something = 1;
something += 5;
变量 something 现在为 6。
如何添加超过 1 个金额:
int something = 1;
something = something+1+1+1;
现在变量 something 将为 4。
如何在创建变量时向变量添加金额:
int something = 1+3;
变量 something 现在为 4。
我认为这就是你的意思,当你不评论你的意思时,我会尽力帮助你!
在我的游戏中,我试图在收集木材时增加木材的价值,但我不知道如何做。这是我的代码:
package Main;
import java.util.Random;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
System.out.println("In order to build your house, you need 25 wood"); //I haven't added building the house yet
System.out.println("To gather wood type 'gather wood'. (no caps)");
while (true) {
Scanner scan = new Scanner(System.in);
Random Random = new Random();
String getWood = "gather wood";
int randomNumber = Random.nextInt(11);
int wood = 0;
String s = scan.nextLine();
if (s.contains( getWood )) {
System.out.println("You have gathered " + randomNumber + " wood!");
} else {
}
}
}
}
当我键入"gather wood"时,我希望它向int变量"wood"添加一个数量,最好是"System.out.println("中相同的随机数你收集了“+ randomNumber +”木头!");"
感谢任何帮助!
谢谢! :D
应该这样做
System.out.println("In order to build your house, you need 25 wood"); //I haven't added building the house yet
System.out.println("To gather wood type 'gather wood'. (no caps)");
int wood = 0;
while (true) {
Scanner scan = new Scanner(System.in);
Random Random = new Random();
String getWood = "gather wood";
int randomNumber = Random.nextInt(11);
String s = scan.nextLine();
if (s.contains( getWood )) {
System.out.println("You have gathered " + randomNumber + " wood!");
wood+=randomNumber ;
System.out.println("You now have " + wood + " wood!");
} else {
}
}
在 if 语句中它将是:
if (s.contains( getWood )) {
System.out.println("You have gathered " + randomNumber + " wood!");
wood = wood + randomNumber;
} else {
}
}
您可能想考虑创建一个 class 并访问 "wood" 变量?
你要在while循环之外声明变量wood,否则它总是被重置为零。 然后你可以像这样添加随机数:
wood += randomNumber;
这是以下的简称:
wood = wood + randomNumber;
将 int wood = 0;
放在循环之外。您在每个循环中创建一个值为 0 的新变量。
您还需要将 wood += randomNumber;
放在 if 语句中。否则木材的价值不变。
如何给 int 变量加一
int something = 1;
something++;
变量 something 现在为 2。
如何添加金额:
int something = 1;
something += 5;
变量 something 现在为 6。
如何添加超过 1 个金额:
int something = 1;
something = something+1+1+1;
现在变量 something 将为 4。
如何在创建变量时向变量添加金额:
int something = 1+3;
变量 something 现在为 4。 我认为这就是你的意思,当你不评论你的意思时,我会尽力帮助你!