我想知道是否可以编写一个命令,假设我们有一个扫描仪
。
示例如下:
Scanner INPUT = new Scanner(System.in);
int IsPoints = 0;
String isValue = INPUT.nextLine();
if (isValue.equalsIgnoreCase("give"){
isPoints += The int value you want here;
}
所以我真正的问题是,是否可以通过字符串命令增加点的 int
值?
The output, if it worked, would be give X (Amount of points you want = x)
因此,如果我确实给出了 5000
,我会获得 5000
积分,这可能吗?
谢谢
最佳答案
你在找这个吗?
Scanner input = new Scanner(System.in);
int points = 0;
String value = input.nextLine(); // give 500
String[] tokens = value.split(" ");
if (tokens[0].equalsIgnoreCase("give")) {
points += Integer.parseInt(tokens[1]);
}
System.out.println(points); // 500
这将把give XXX
作为表达式,然后将X添加到您的计数器中。
关于Java 整数与字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20787520/