gpt4 book ai didi

java - 在构建字符串并将它们添加到 ArrayList 时出错

转载 作者:行者123 更新时间:2023-12-03 08:36:52 25 4
gpt4 key购买 nike

我正在做一个最终项目,这里是项目的信息和我得到的错误:
我们应该模拟一种名为“compAlien”的生命形式,看看它们如何相互 react 。这些外星人的创造方式是使用 128 个字符长且仅由字母 X、Y 或 Z 组成的遗传密码。
根据一些规则,我们应该计算并找到有关这些外星人的此类信息(例如,如果遗传密码中有 20 个 X,它的眼睛颜色将变为蓝色等),创建一个包含此信息的字符串并它到一个ArrayList。我能够创建代码(如下所示),但大多数时候当我尝试创建多个外星人时,我总是收到一条错误消息:

 Exception in thread "main" java.lang.StringIndexOutOfBoundsException:
String index out of range: 128 at
java.base/java.lang.StringLatin1.charAt(StringLatin1.java:48) at
java.base/java.lang.String.charAt(String.java:709) at
compAlien.calculateHealth(compAlien.java:75) at
compAlien.generateCompAlienInfos(compAlien.java:34) at
compAlien.main(compAlien.java:193)
这是我能够编写的代码:
import java.util.Scanner;
import java.util.ArrayList;

public class compAlien {

static Scanner input = new Scanner(System.in);
static ArrayList<String> compAlienInfos = new ArrayList<String>();
static String geneticCode = "";
static String compAlienInfo = "";
static int colonySize = 0;
static int count = 0;

public static void generateGeneticCode() { //Method for creating the genetic codes of the compAliens.

for(int i = 0; i < 128; i++) {

char geneticCodeChar = (char) ('X' + Math.random() * ('Z' - 'X' + 1));
geneticCode += geneticCodeChar;

}

}

public static void generateCompAlienInfos() { //Method for creating all the info of the compAliens, assigning them an ID and adding their info to the compAlienInfos ArrayList.

while(count < colonySize) {

geneticCode = "";
compAlienInfo = "";
compAlienInfo += "ID:" + (count + 1) + ", ";

generateGeneticCode();
findGender();
calculateHealth();
findSkinTone();
findEyeColor();

compAlienInfos.add(compAlienInfo);
System.out.println(compAlienInfo);
count ++;

}

}

public static void findGender() { //Method for finding the gender of the compAliens.

if(geneticCode.charAt(127) == 'X') {

compAlienInfo += "Gender: Female, ";

}

else if(geneticCode.charAt(127) == 'Y') {

compAlienInfo += "Gender: Male, ";

}

else {

compAlienInfo += "Gender: Female, ";

}

}

public static void calculateHealth() { //Method for calculating the health of the compAliens.

int health = 1;
int i = 0;

while(i < 127) {

if(geneticCode.charAt(i) == 'Y' && geneticCode.charAt(i + 1) == 'X' && geneticCode.charAt(i + 2) == 'Z') {

health ++;

}

i++;

}

compAlienInfo += "Health: " + health + ", ";

}

public static void findEyeColor() { //Method for finding the eye color of the compAliens.

int xCount = 0;

for(int i = 0; i < 128; i++) {

if(geneticCode.charAt(i) == 'X') {

xCount ++;

}

}

if(xCount <= 10) {

compAlienInfo += "Eye Color: Green";

}

else if (10 < xCount && xCount <= 25) {

compAlienInfo += "Eye Color: Blue";

}

else {

compAlienInfo += "Eye Color: Brown";

}

}

public static void findSkinTone() { //Method for finding the skin tone of the compAliens.

int yCount = 0;

for(int i = 0; i < 128; i++) {

if(geneticCode.charAt(i) == 'Y') {

yCount ++;

}

}

if(20 <= yCount && yCount < 30) {

compAlienInfo += "Skin Tone: Red ";

}

else if (30 <= yCount && yCount <= 40) {

compAlienInfo += "Skin Tone: Green, ";

}

else {

compAlienInfo += "Skin Tone: Orange, ";

}

}

public static void main(String[] args) {

System.out.println("Enter the population size of the compAlien colony: ");
colonySize = input.nextInt();

System.out.println("Simulating the compAlien colony...");
generateCompAlienInfos();

}

}

最佳答案

根据堆栈跟踪,在 calculateHealth()方法,此行试图访问字符串中不存在的位置的字符。更具体地说是 charAt() 调用,如下所示。该索引可能大于遗传代码字符串的长度。

if (geneticCode.charAt(i) == 'Y' && geneticCode.charAt(i + 1) == 'X' && geneticCode.charAt(i + 2) == 'Z')

关于java - 在构建字符串并将它们添加到 ArrayList 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65692316/

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