无法准确评估字符串

时间:2013-10-12 14:35:14

标签: java string equals

我编写了一些代码,用于请求用户输入并将其分配给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并且对它的所有评估都是错误的 有没有办法让它正确读取变量?

3 个答案:

答案 0 :(得分:1)

您将始终进入此条件范围if ((!(skillAssign.equals("stats"))) || (!(skillAssign.equals("done")))) 因为当一个单词未能通过它时,另一个条件会让它通过,因为它们完全不同。 例如: 让我们采取" stats",这将导致if(false || true)=>真的。
让我们来完成",这将产生if(true || false)=>真的。
让我们来看看" sTats",这会产生if(true || true)=>真。

你无法逃脱它,你需要检查它: if (!(skillAssign.equals("stats") || skillAssign.equals("done"))) 这样我们就可以检查逻辑表达式是否为真,然后我们将其更改为"而不是"然后检查整个条件是否成立。

<强> P.S
答:您可以使用&#34; abc&#34; .equalsIgnoreCase(&#34; AbC&#34;)代替您现在使用的内容。
B.不要为每个逻辑表达式括号添加它使得代码更复杂,第一眼看不清楚。

答案 1 :(得分:0)

问题是我在||

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

答案 2 :(得分:0)

Orel所说的几乎就是它。如果您正在使用System.out.println(skillAssign == "stats")来评估您的skillAssign,它将总是返回false,即使它们具有相同的值。 因为==比较字符串的引用而不是它们的值。 你必须使用equals()方法。