0

这是我的代码:

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 和一般编程的新手,感谢任何帮助。

4

2 回答 2

0

程序运行时,每个 case 都根据输入的值执行(例如,如果用户输入 1 进行选择,则只执行 Case 1 而不执行其余部分),这是因为每个 case 末尾都有 break 关键字,如果您希望所有 case 语句都运行,则需要。1. 要么删除break(这意味着用户选择 1 没有意义,因为他将经历所有选项) 2. 重新设计,您事先询问相关信息或在每种情况下询问所有必要信息。

如果在方法的开头声明变量也会更好。因为每次迭代都会清除这些值(不确定这是否是故意的)

恕我直言,如果您创建更多方法而不是编写冗长的案例结构会更好。它使代码有点难以阅读:)

于 2013-10-02T04:50:52.723 回答
0
String name1="", name2="", character="", character1="";  int level=0, level1=0;

在这个类中都是局部变量。所以每个while循环执行变量都会被重置,所以请在do whlie循环之外声明。

import java.util.Scanner;

public class pg3b {

public static void main(String[] args) {

int choice; 

String name1="", name2="", character="", character1="";
  int level=0, level1=0;
do {

  Scanner keyboard = new Scanner(System.in); 

  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 = 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 = 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);


}
}
于 2013-10-02T05:06:59.307 回答