如何在Java中使用一个循环进入另一个循环

时间:2019-06-18 07:44:52

标签: java loops while-loop switch-statement java.util.scanner

我正在尝试做一个小游戏:

  • 您可以选择2位英雄:1位战士和2位法师。

  • 接下来,您应该选择旅行方式:1用马2传送(仅法师可用)。

  • 最后,选择一种武器:1把剑2杖(*法师只能使用杖;战士可以同时使用)。

我为第一个问题(选择一个英雄)创建了一个循环,以便如果用户输入除1或2之外的其他内容,程序将重复该问题(“选择您的英雄:...)。我需要对于第二个问题和第三个问题也是如此(特别是由于存在一些限制,例如,如果用户选择“战士”,则他不能选择“远距离传送”作为旅行选项)。

public static void main(String[] args) {
    int hero, travel, weapon;
    Scanner scan = new Scanner(System.in);

    loop:
        while (true) {
            System.out.println("Choose your hero: 1 for Warrior, 2 for Mage");
            hero = scan.nextInt();
            switch (hero) {
            case 1:
                System.out.println("Choose your travel option: 1 for Horse; 2 for Teleportation");
                travel = scan.nextInt(); 
                break loop;
            case 2:
                System.out.println("Choose your travel option: 1 for Horse; 2 for Teleportation");
                travel = scan.nextInt(); 
                break loop;
            default:
                break;
            }
        }                       
}

我不知道如何在另一个循环中正确使用一个循环。我尝试了几种方法,但始终会返回错误。

3 个答案:

答案 0 :(得分:1)

您的流程可以编写为简单的过程代码。因此,我将以最简单的形式编写它-至少作为开始。

switch内使用labels loopsloops并没有真正的道理

只需编写三个简单的循环,一个接一个地循环-读取,理解和调试将很简单。

我不想为您编写代码,这不是本网站的目的,但这是伪代码:

Loop 1 (selecting heroes)

If(heroes != Warrior)
    Loop 2 (selecting travel)
else travel=Horse

Loop 3 (selecing weapon)

答案 1 :(得分:1)

就像评论一样,我不会在循环内使用循环。相反,您应该一次分配一个变量。我已经编写了一个辅助方法selectVariable(String description, String varOne, String varTwo),您可以使用该方法来分配变量并为您的故事游戏入门。如果您想给用户更多选择,可以扩展它。如果在这种情况下没有选择,也不要给幻觉做出选择。

这是应该执行所需操作的代码:

import java.util.Scanner;

public class Story {

  static Scanner scan = new Scanner(System.in);

  public static void main(String[] args) {
    int hero = chooseHero();
    int travel = chooseTravel(hero);
    int weapon = chooseWeapon(hero);

    //For printing purposes give your choice their respective string name again. 
    String heroDesc = hero == 1 ? "Warrior" : "Mage";
    String travelDesc = travel == 1 ? "Horse" : "Teleportation";
    String weaponDesc = weapon == 1 ? "Sword" : "Staff";

    System.out.printf("you are a %s, traveling by %s, wielding a %s" + System.lineSeparator(), heroDesc, travelDesc, weaponDesc);
  }

  private static int chooseHero() {
    return selectVariable("choose your hero class", "warrior", "mage");
  }

  private static int chooseTravel(int hero) {
    if (hero == 1) { // if the user has no choice don't give the illusion a choice can be made
      System.out.println("you are a Warrior you will travel by horse");
      return 1;
    } else {
      return selectVariable("choose your way of travel", "by horse", "teleportation");
    }
  }

  private static int chooseWeapon(int hero) {
    if (hero == 2) {
      System.out.println("you are a mage you will wield a staff");
      return 2;
    } else {
      return selectVariable("choose your weapon", "sword", "staff");
    }
  }

  //you can reuse this method to also assign other traits to your story
  private static int selectVariable(String description, String varOne, String varTwo) {
    int var;
    do {
      System.out.printf("%s: 1 %s, 2 for %s" + System.lineSeparator(), description, varOne, varTwo);
      var = scan.nextInt();
      switch (var) {
        case 1:
          System.out.printf("you have chosen %s" + System.lineSeparator(), varOne);
          return var;
        case 2:
          System.out.printf("you have chosen %s" + System.lineSeparator(), varTwo);
          return var;
        default:
          System.out.println(var + " is an invalid choice");
      }
    }
    while (true);
  }
}

答案 2 :(得分:1)

将内容拆分而不是嵌套循环总是一个好主意。这是将程序分为3种方法的简单方法,每种方法都处理一个选择。

英雄选择:同时提供选择和循环选择,直到给出有效答案为止。然后返回答案

private static int queryHero() {
    Scanner scan = new Scanner(System.in);
    int hero;
    while (true) {
        System.out.println("Choose your hero: 1 for Warrior, 2 for Mage");
        hero = scan.nextInt();
        if(hero == 1 || hero == 2) {
            break;
        } else {
            System.out.println(hero + " is not a valid choice");
        }
    }
    return hero;
}

旅行选项选择:根据所选择的英雄和循环提供选择,直到给出有效答案为止。然后返回答案

private static int queryTravelOptionForHero(int hero) {
    Scanner scan = new Scanner(System.in);
    int travelOption;
    while (true) {
        if (hero == 1) {
            System.out.println("Choose your travel option: 1 for Horse");
            travelOption = scan.nextInt();
            if (travelOption == 1) {
                break;
            } else {
                System.out.println(travelOption + " is not a valid choice");
            }
        } else if (hero == 2) {
            System.out.println("Choose your travel option: 1 for Horse; 2 for Teleportation");
            travelOption = scan.nextInt();
            if (travelOption == 1 || travelOption == 2) {
                break;
            } else {
                System.out.println(travelOption + " is not a valid choice");
            }
        }
    }
    return travelOption;
}

武器选择:根据所选择的英雄和循环来提供选择,直到给出有效答案为止。然后返回答案

private static int queryWeaponForHero(int hero) {
    Scanner scan = new Scanner(System.in);
    int weapon;
    while (true) {
        if(hero == 1) {
            System.out.println("Choose your weapon: 1 for Sword; 2 for Staff");
            weapon = scan.nextInt();
            if (weapon == 1 || weapon == 2) {
                break;
            } else {
                System.out.println(weapon + " is not a valid choice");
            }
        } else if(hero == 2) {
            System.out.println("Choose your weapon: 2 for Staff");
            weapon = scan.nextInt();
            if(weapon == 2) {
                break;
            }else {
                System.out.println(weapon + " is not a valid choice");
            }
        }

    }
    return weapon;
}

然后在您的主目录中:

int hero = queryHero();
int travelOption = queryTravelOptionForHero(hero);
int weapon = queryWeaponForHero(hero);

System.out.println("hero: " + hero);
System.out.println("travelOption: " + travelOption);
System.out.println("weapon: " + weapon);

注意:我不确定您是否了解它们,但是有一些方法可以使用枚举和列表使此代码更好