0

我编写了一些代码,应该要求用户输入并将其分配给 String SkillAssign。当我尝试评估 SkillAssign 时,无论如何它都会返回 false。这是我的代码:

import java.util.*;
public class CharacterCustomization
{
    public CharacterCustomization()
    {
    }
    public static void Customization()
    {
        Scanner keyboard = new Scanner(System.in);
        int skillPoints = 100;
        String skillAssign = "";
        int newMaxHealth = 0;
        int newMaxMagic = 0;
        int newMaxStamina = 0;
        int assignmentValue = 0;
        boolean isDone = false;
        System.out.println("Welcome to character customization, you have 100 points to allocate to your skills.");
        System.out.println("To allocate points, type name of skill, followed by the points you want to assign (blank for positive, - for negative, ex. -5)");
        System.out.println("Put Skill on one line, and press enter, then the value on the next line");
        System.out.println("Type \"stats\" to view full stats at any time");
        System.out.println("Type \"done\" to finish");
        while (true)
        {
            while (isDone == false)
            {
                skillAssign = keyboard.nextLine();

                if (skillPoints == 0)
                {
                System.out.println("Max Health: " + newMaxHealth);
                System.out.println("Max Magic: " + newMaxMagic);
                System.out.println("Max Stamina: " + newMaxStamina);
                System.out.println("Skill points left: " + skillPoints);
                System.out.println("Type \"done\"to finish");
            }
            if ((!(skillAssign.equals("stats"))) || (!(skillAssign.equals("done"))))
            {
                assignmentValue = keyboard.nextInt();
                if (((skillAssign.equals("health")) || (skillAssign.equals("Health"))) && (skillPoints - assignmentValue >=0))
                {
                    skillPoints = (skillPoints - assignmentValue);
                    newMaxHealth = (assignmentValue + newMaxHealth);
                }
                else if (((skillAssign.equals("magic")) || (skillAssign.equals("Magic"))) && (skillPoints - assignmentValue >=0))
                {
                    skillPoints = (skillPoints - assignmentValue);
                    newMaxMagic = (assignmentValue + newMaxMagic);
                }
                else if (((skillAssign.equals("stamina")) || (skillAssign.equals("Stamina"))) && (skillPoints - assignmentValue >=0))
                {
                    skillPoints = (skillPoints - assignmentValue);
                    newMaxStamina = (assignmentValue + newMaxStamina);
                }
                else
                {
                    //System.out.println("Sorry, I could not read that!");
                    System.out.println(skillAssign == "stats");
                }
            }
            else if (skillAssign.equals("stats"))
            {
                System.out.println("Max Health: " + newMaxHealth);
                System.out.println("Max Magic: " + newMaxMagic);
                System.out.println("Max Stamina: " + newMaxStamina);
                System.out.println("Skill points left: " + skillPoints);
            }
            else if ((skillAssign.equals("done")) || (skillAssign.equals("Done")))
            {
                isDone = true;
                continue;
            }
            else
            {
                System.out.println("Sorry, I could not read that!");
            }
        }
        System.out.println("Are you sure this is the setup you want? [y] [n]");
        System.out.println("Max Health: " + newMaxHealth);
        System.out.println("Max Magic: " + newMaxMagic);
        System.out.println("Max Stamina: " + newMaxStamina);
        System.out.println("Skill points left: " + skillPoints);
        //skillAssign = keyboard.nextLine();
        if (((keyboard.nextLine()).equals("y")) || ((keyboard.nextLine()).equals("Y")) || ((keyboard.nextLine()).equals("yes")) || ((keyboard.nextLine()).equals("Yes")))
        {
            Player player = new Player(newMaxHealth, newMaxMagic, newMaxStamina);
        }
        else
        {
            isDone = false;
        }
    }
}   

}

它不完整,但我stats在提示时输入并且对它的所有评估都是错误的有没有办法让它正确读取变量?

4

3 回答 3

1

你总是会进入这个条件范围内if ((!(skillAssign.equals("stats"))) || (!(skillAssign.equals("done")))) ,因为当一个词没有通过时,另一个条件会让它通过,因为它们完全不同。例如:让我们使用“stats”,这将导致 if (false || true) => true。
让我们“完成”,这将导致 if (true || false) => true。
让我们以“sTats”为例,这将导致 if (true || true) => true。

你无法逃避它,你需要像这样检查它: if (!(skillAssign.equals("stats") || skillAssign.equals("done"))) 这样我们检查任何一个逻辑表达式是否为真,然后我们将其更改为“不”,然后检查整个条件是否为真。

PS
A. 您可以使用 "abc".equalsIgnoreCase("AbC") 代替您现在使用的。
B. 不要为每个逻辑表达式添加括号,这会使代码乍一看更加复杂和不清楚。

于 2013-10-12T14:51:10.963 回答
0

奥廖尔所说的几乎就是它。只是想添加,如果您System.out.println(skillAssign == "stats")用于评估您的 SkillAssign,即使它们具有相同的值,它也将始终返回 false。因为==比较字符串的引用而不是它们的值。你必须使用equals()方法。

于 2013-10-12T15:01:28.613 回答
0

问题是我使用的是||而不是&&if ((!(skillAssign.equals("stats"))) || (!(skillAssign.equals("done"))))

于 2013-10-12T14:58:01.853 回答