可以重复if语句吗?

时间:2015-01-18 14:09:00

标签: java loops

我正在尝试进行某种角色创建,以便更熟悉Java,因为我对它很陌生。我希望用户能够选择一个名称,然后选择一个类。对于课程选择,我使用扫描仪方法,这意味着用户必须正确拼写他们的课程。

import static java.lang.System.out;
import java.util.Scanner;

public class FirstTestNew {

public static void main( String args[]) {

    Scanner nameInput = new Scanner(System.in);
    Scanner classInput = new Scanner(System.in);

    String Swordsman, Sorcerer, Elementalist, Assassin, Sharpshooter; // Classes
    int Level = 1;
    int Strength=1, Endurance=1, Vitality=1, Willpower=1, Luck=1, Intelligence=1; // Attributes
    int Health = Vitality*5*(Level*2); // HP = Vitality + Level
    int Mana = Willpower*10+(Level*2); // MP = Willpower + Level
    int Energy = Endurance*50+(Level*10); // NRG = Endurance + Level
    int PhysDmg = Strength*3+(Level*2); // Physical Damage
    int MagDmg = Willpower*3+(Level*2); // Magical Damage

    // Name selection
    out.println("Input your character name : ");
    String playerName = nameInput.nextLine();
    out.println("You are now to be known as "+playerName);

    // Class selection
    out.println("\nNow, what class would you like to play?\nSwordsman, Sorcerer, Elementalist, Assassin or Sharpshooter?");
    String playerClass = classInput.nextLine();

    if (playerClass.equals("Swordsman")){
        out.println("You chose to be a Swordsman!");}

    else if (playerClass.equals("Sorcerer")){
        out.println("You chose to be a Sorcerer!");}

    else if (playerClass.equals("Elementalist")){
        out.println("You chose to be an Elementalist!");}

    else if (playerClass.equals("Assassin")){
        out.println("You chose to be an Assassin!");}

    else if (playerClass.equals("Sharpshooter")){
        out.println("You chose to be a Sharpshooter!");}

    else {
        out.println("Make sure your spelling is correct.");}
    }
}        

所以,基本上,如果用户拼写不同的类,我希望使用'else',因此他们会被告知拼写错误,然后重复到if语句的开头,这样他们就可以尝试再次。这甚至可能吗?也许通过使用某种循环?

6 个答案:

答案 0 :(得分:1)

你可以通过两种方式实现: 第一种方法是在每次玩家拼写单词错误时调用函数中的类选择。

另一种方法是创建一个while循环。只需创建一个值为true的变量,只要值为true,就创建一个while循环,如果单词拼写正确,则变量设置为false。

答案 1 :(得分:1)

使用此模式:

boolean valid;
String playerClass = "";
do
{
    valid = true; // reset each time
    // Class selection
    out.println("\nNow, what class would you like to play?\nSwordsman, Sorcerer, Elementalist, Assassin or Sharpshooter?");
    playerClass = classInput.nextLine();

    if (playerClass.equals("Swordsman")){
        out.println("You chose to be a Swordsman!");}
    else if (playerClass.equals("Sorcerer")){
        out.println("You chose to be a Sorcerer!");}
    ...
    else {
        System.out.println("Make sure your spelling is correct.");
        valid = false;
    }
} while (!valid);

每次点击最终else时,循环都会重复,因为它会设置valid = false;。你也可以将它封装在一个函数中,这将是更好的编码实践。

替代方法

对于它的价值,您还可以使用if个选项和contains方法来简化List<String>语句,以测试有效输入。

为了更好的用户体验,我还会考虑向他们索要一个选项号而不是一个单词,如果你必须要求一个单词,那么使用classInput.nextLine().trim().toLowerCase()并与所有小写的单词进行比较,以减少空白和大写错误。

final String allOptions = "Swordsman,Sorcerer,Elementalist,Assassin,Sharpshooter";
List<String> options = Arrays.asList(allOptions.split(","));
//  construct the display message
String msg = "\nNow, what class would you like to play?\n";
// in general use a StringBuilder for this. += used for brevity only
for (int i = 0; i < options.size(); i++) msg += options.get(i) + ", ";
msg += options.get(options.size() - 1) + "?";

现在你的循环就像:

一样简单
boolean valid = false; 
String playerClass = "";
while (!valid)
{
    // Class selection
    out.println(msg);
    playerClass = classInput.nextLine();
    valid = options.contains(playerClass);
    if (!valid)
        System.out.println("Make sure your spelling is correct.");
}
out.println("You chose to be a " + playerClass + "!");

答案 2 :(得分:0)

您可以使用do-while循环:

boolean isSpelledCorrecly = true;
do {
   // get input
   ...
   else {
      out.println("Make sure your spelling is correct.");
      isSpelledCorrecly = false;
   }     
}
while(!isSpelledCorrecly)

答案 3 :(得分:-1)

最好将该逻辑移动到一个单独的方法中,并使用重复结构在无效输入时调用它

e.g。

//your logic
while(isValidClass());
//your other logic


public boolean isValidClass(){
    String playerClass = classInput.nextLine();
    boolean isValid = true;
    if (playerClass.equals("Swordsman")){
       out.println("You chose to be a Swordsman!");}
    else if (playerClass.equals("Sorcerer")){
        out.println("You chose to be a Sorcerer!");}
    else if (playerClass.equals("Elementalist")){
        out.println("You chose to be an Elementalist!");}
    else if (playerClass.equals("Assassin")){
        out.println("You chose to be an Assassin!");}
    else if (playerClass.equals("Sharpshooter")){
        out.println("You chose to be a Sharpshooter!");}
    else {
        out.println("Make sure your spelling is correct.");
        isValid = false;
    }
    return isValid;
}

答案 4 :(得分:-1)

do-while循环似乎是你的代码的一个很好的答案。控制循环的一种简单方法是定义一个bool:

boolean loop=true;
//everything else that goes into your code
//...
//println where you requesto to enter the class name
do{
String playerClass = classInput.nextLine();
if (playerClass.equals("Swordsman")){
    out.println("You chose to be a Swordsman!");
    loop=false;}

else if (playerClass.equals("Sorcerer")){
    out.println("You chose to be a Sorcerer!");
    loop=false;}

else if (playerClass.equals("Elementalist")){
    out.println("You chose to be an Elementalist!");
    loop=false;}

else if (playerClass.equals("Assassin")){
    out.println("You chose to be an Assassin!");
    loop=false;}

else if (playerClass.equals("Sharpshooter")){
    out.println("You chose to be a Sharpshooter!");
    loop=false;}

else {
    out.println("Make sure your spelling is correct.");
    }
    }while(loop);

答案 5 :(得分:-1)

这应该可以解决问题......

boolean incomplete = true;

while (incomplete) {
    // Class selection
    out.println("\nNow, what class would you like to play?\nSwordsman, Sorcerer, Elementalist, Assassin or Sharpshooter?");
    String playerClass = classInput.nextLine();
    switch (playerClass) {
        case "Swordsman":
            out.println("You chose to be a Swordsman!");
            incomplete = false;
            break;
        case "Sorcerer":
            out.println("You chose to be a Sorcerer!");
            incomplete = false;
            break;
        case "Elementalist":
            out.println("You chose to be an Elementalist!");
            incomplete = false;
            break;
        case "Assassin":
            out.println("You chose to be an Assassin!");
            incomplete = false;
            break;
        case "Sharpshooter":
            out.println("You chose to be a Sharpshooter!");
            incomplete = false;
            break;
        default:
            out.println("Make sure your spelling is correct.");
    }
}