I’m developing a Java RPG where the boss menu lists the bosses of the dungeons in pages. Once the player chooses a dungeon, the second menu lists the available actions.
String[]heroArr={"Alice", "Annie", "Charmander", "Charmeleon", "Charizard",
"Bob", "Jack", "Metapod", "Butterfree", "Weedle",
"Lily", "Lucy", "Pikachu", "Pidgeotto", "Pidgeot"};
int currentPage = 0;
int pageSize = 5;
Scanner sc = new Scanner(System.in);
while (true) {
int startIndex = currentPage*pageSize;
int endIndex = Math.min(startIndex+pageSize, heroArr.length);
for (int i = startIndex; i < endIndex; i++) {
System.out.println(heroArr[i]+ "(" + String.valueOf(i+1) + ")");
}
String input = sc.nextLine().trim();
if (input.isEmpty()) {
currentPage++;
if (endIndex >= heroArr.length) {
break;
}
continue;
} else {
int heroId = Integer.parseInt(input)-1;
System.out.println("challenge "+heroArr[heroId]);
while (true) {
System.out.println("challenge begins! choose: 1(attack), 2(escape)");
int usrSelectedIdx = sc.nextInt();
if (usrSelectedIdx == 1) {
System.out.println("challenge succeeds! "+heroArr[heroId]+"'s fragment+1");
} else if (usrSelectedIdx == 2) {
System.out.println("escaped");
} else if (usrSelectedIdx == 9) {
// System.out.println();
currentPage = 0;
break;
} else {
System.out.println("erro");
}
}
}
}
When the player launches the game and the boss menu is rendered for the first time, everything goes well.
The player chooses a boss to challenge and then attacks the boss. However, things go wrong when the player chooses 9 to go back to the boss menu. The game is supposed to render the first pageSize = 5
bosses, but it actually renders 10 bosses. How do I fix it?