Java 如何将字符串附加到整数数组?
我已经在这个网站上做了一些关于这方面的研究,但是,我在寻找我可以使用的东西时遇到了一些困难。我正在尝试创建一个类似于口袋妖怪的程序,但要简单得多,并且基于文本。目前我正在使用数组来存储口袋妖怪的统计数据。这很好,但是当我需要打印口袋妖怪的名字时,我必须手动输入。这很好,但我想让玩家与一个随机的口袋妖怪战斗。这是我当前的代码:Java 如何将字符串附加到整数数组?,java,arrays,int,Java,Arrays,Int,我已经在这个网站上做了一些关于这方面的研究,但是,我在寻找我可以使用的东西时遇到了一些困难。我正在尝试创建一个类似于口袋妖怪的程序,但要简单得多,并且基于文本。目前我正在使用数组来存储口袋妖怪的统计数据。这很好,但是当我需要打印口袋妖怪的名字时,我必须手动输入。这很好,但我想让玩家与一个随机的口袋妖怪战斗。这是我当前的代码: public static void main(String[] args) { //Declare the Pokemon to be used int[
public static void main(String[] args) {
//Declare the Pokemon to be used
int[] Player = {0, 0, 0, 0, 0, 0 };
int[] Blastoise = {79, 83, 100, 85, 105, 78 };
int[] Raichu = {60, 90, 55, 90, 80, 110};
int[][] Pokemon = new int[][] {Blastoise, Raichu, Player};
Scanner input = new Scanner(System.in);
startBattle(input, Pokemon)
}
public static String startBattle(Scanner input, int[][] Pokemon) {
System.out.println("A wild Pokemon appeared!");
int r = (int) (Math.random() * 1);
System.out.println("A wild " + **POKEMON NAME** + " appeared!");
System.out.print("What Pokemon do you choose? ");
String userPokemon = input.next();
return userPokemon;
}
上面写着口袋妖怪名字的地方就是我想要的口袋妖怪名字的地方。我知道获取数组名称或向int数组添加字符串非常困难(我会将名称添加到列表[0]点)。有没有办法让我至少把一个字符串与其中一个列表关联起来,这样我就可以调用随机选择的口袋妖怪的名字?谢谢 创建一个类。赋予它表示描述真实世界实体的事物的属性,在本例中为口袋妖怪 属性至少是一个字符串,表示用于描述字符的名称和整数数组 下面是一个例子:
public class Pokemon {
private String name;
public String getName() { return this.name; }
public void setName(String name) { this.name = name; }
private int hp;
public int getHP() { return this.hp; }
public void setHP(int hp) { this.hp = hp; }
// Repeat the pattern for all properties that describe a Pokemon
// hp, attack, defense, special attack, special defense, and speed
}
查看Java面向对象的方面,并将每个口袋妖怪当作自己的对象来使用。如果没有将它们作为命名属性,那么在数组中处理统计数据可能会变得非常乏味<代码>“等等,ATK是什么?第三个还是第四个?”所以我肯定会推荐这种方法,这是一个很好的观点。我不确定这些数字代表什么,但如果它们是状态值,则最好在类中添加单独的属性,而不是使用整数数组。您应该给它们一个代码示例:)我认为这会有所帮助。@EricJ。如果你打算举个例子,只需说出“HP”、“攻击”、“防御”等字段。这些数字是统计数据,顺序是:HP、攻击、防御、特殊攻击、特殊防御和速度。我创建的类应该包含什么?