Java 我的阵列没有上升
我正在做一个基于文本的冒险游戏。(这根本不是OOP)问题出现在我循环中的第二个if语句中,它不是将1添加到我的数组位置[],而是继续打印位置[0],然后在最后打印一个1。我不太清楚这里发生了什么Java 我的阵列没有上升,java,arrays,Java,Arrays,我正在做一个基于文本的冒险游戏。(这根本不是OOP)问题出现在我循环中的第二个if语句中,它不是将1添加到我的数组位置[],而是继续打印位置[0],然后在最后打印一个1。我不太清楚这里发生了什么 package com.PenguinGaming; import java.util.Scanner; public class Game{ /* * Command list: * - investigate (advanced description)
package com.PenguinGaming;
import java.util.Scanner;
public class Game{
/*
* Command list:
* - investigate (advanced description)
* - go north
* - go south
* - go west
* - go east
* - pick up
* - eat
* - drink from
* - drink object
* - climb up/down
* - burn
* - use object
* - attack
* - defend
* - description (basic description)
* - read
* - life
* - help (brings up command list)
*/
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
int life = 25;
boolean running = true;
String input = null;
String[] locations = new String[3];
locations[0] = "Location: Canyon\nDescription: You stand alone over looking a large caynon, \nwater crashing" +
" into the forsaken pit. A orange cactus stands tall near by. \nTheir is some light brown brush" +
" blowing in the wind to the east. \nThe sky is red... Darkness is coming soon.";
locations[1] = "River";
locations[2] = "Field";
//Starting player location
String player = locations[0];
//This is how we will navigate the map
// north + 1, east + 2, south -1, west - 2;
//player = locations[0] + 1;
String[] commands = new String[6];
commands[0] = "investigate";
commands[1] = "go north";
commands[2] = "go east";
commands[3] = "go south";
commands[4] = "go west";
commands[5] = "terminate game";
System.out.println(locations[0]);
do{
input = scanner.nextLine();
input = input.toLowerCase();
if (input.equals(commands[0])) {
// give description for location
System.out.println(player);
}
if(input.equals(commands[1])){
player = player + 1;
System.out.println(player);
}
if(input.equals(commands[5])){
break;
}
}
while(running == true);
System.exit(0);
}
}
语句
player=player+1
获取字符串player
,并将字符串1
添加到其中。所以是的,它采用了以前的player
并在末尾添加了1
这里的关键是player
属于String
类型。因此它将“Location:…..”+1处理为“Location:…”+“1”=“Location….1”
如果每次他们选择“向北”时,应该增加位置变量,那么应该有一个int
类型的位置变量。因此,请加上:
int loc = 0;
转到初始化部分,并替换player=player+1代码>一行加两行
loc++;
player = locations[loc];
我认为应该将变量player
定义为int
:
int player =0;
然后打印location[player]
实际上,变量“player”应该是一个int。在开始时将其设置为0。如果要打印玩家当前位置,请执行以下操作:
System.out.println(位置[player]) 我想这样做,但是,因为这是一个基于文本的冒险游戏,向北将在数组中添加1。我希望能够键入“向北”,它只会将1添加到我的位置,例如从位置[0]>向北>位置[1]>向北位置[2]