这是我的代码:
import java.util.Scanner;
public class pg3b {
public static void main(String[] args) {
int choice;
do {
Scanner keyboard = new Scanner(System.in);
String name1="", name2="", character="", character1="";
int level=0, level1=0;
System.out.println("Please choose an option: \n1. Change Trainer1's name \n2. Change Trainer2's name \n3. Set Trainer1's Pokemon \n4. Set Trainer2's Pokemon \n5. Display the trainers' names and Pokemon \n6. Display the trainer who has the highest level Pokemon \n7. Exit the program");
System.out.print("Choice: ");
choice = keyboard.nextInt();
switch(choice) {
case 1:
System.out.println("Trainer1's name is currently Trainer1. Enter a new name: ");
name1 = keyboard.next();
System.out.println("Trainer1's name is now " + name1);
break;
case 2:
System.out.println("Trainer2's name is currently Trainer2. Enter a new name: ");
name2 = keyboard.next();
System.out.println("Trainer2's name is currently " + name2);
break;
case 3:
System.out.println(name1 + "'s Pokemon is currently Pokemon with level 0. Enter a new Pokemon and level: ");
String input = keyboard.next();
int inputLevel = keyboard.nextInt();
character = input.substring(0);
character = character.toLowerCase();
level = inputLevel;
boolean validPoke = true;
boolean validLevel = true;
if (level < 1 || level > 15) {
System.out.println("Invalid level!");
validLevel = false;
}
if (character.equalsIgnoreCase("bulbasaur")) {
validPoke = true;
}
else if (character.equalsIgnoreCase("pikachu")) {
validPoke = true;
}
else if (character.equalsIgnoreCase("charmander")) {
validPoke = true;
}
else if (character.equalsIgnoreCase("squirtle")) {
validPoke = true;
}
else validPoke = false;
System.out.println("Invalid Pokemon!");
if (validLevel == true && validPoke == true) {
System.out.println("Added level " + level + character + " for " + name1);
}
break;
case 4:
System.out.println(name2 + "'s Pokemon is currently Pokemon with level 0. Enter a new Pokemon and level: ");
String input1 = keyboard.next();
int inputLevel1 = keyboard.nextInt();
character1 = input1.substring(0);
character = character.toLowerCase();
level1 = inputLevel1;
boolean validPoke1 = true;
boolean validLevel1 = true;
if (level1 < 1 || level1 > 15) {
System.out.println("Invalid level!");
validLevel1 = false;
}
if (character1.equalsIgnoreCase("Bulbasaur")) {
validPoke1 = true;
}
else if (character1.equalsIgnoreCase("Pikachu")) {
validPoke1 = true;
}
else if (character1.equalsIgnoreCase("Charmander")) {
validPoke1 = true;
}
else if (character1.equalsIgnoreCase("Squirtle")) {
validPoke1 = true;
}
else validPoke1 = false;
System.out.println("Invalid Pokemon!");
if (validLevel1 == true && validPoke1 == true) {
System.out.println("Added level " + level1 + character1 + " for " + name2);
}
break;
case 5:
System.out.println(name1 + character + level);
System.out.println(name2 + character1 + level1);
break;
case 6:
if (level > level1) {
System.out.println(name1 + "has the highest level Pokemon.");
}
else if (level1 > level) {
System.out.println(name2 + "has the highest level Pokemon.");
}
break;
case 7:
System.out.println("Quit");
break;
}
}
while (choice != 7);
}
}
如何将案例 1、2、3 和 4 中定义的信息应用于案例 5?现在,当您在菜单中输入 5 时,什么都没有显示。此外,目前当您为案例 3 和案例 4 输入口袋妖怪和等级时,即使它是正确的,它也会说它是无效的。有谁知道为什么以及如何解决它?
***通过获取其他情况下的信息,我的意思是我在案例 1 中定义了 name1,但在案例 5 中我需要再次引用它。有没有办法做到这一点而不会出现错误,说变量不能被发现?
另外,很抱歉在帖子中提出了超过 1 个问题。我是 Java 和一般编程的新手,感谢任何帮助。