满足字符串选择条件,不调用class

string choice condition is met, class is not called

再次编辑:

每次我 运行 并输入 "String a" 时,所有方法都会被调用,但我不知道为什么。

我知道这都是新手的东西,但直觉相反,java 上的教程和主题太多,无法正确解决此类基本问题。

进口java.util.*; public class ShopTest {

public static Scanner navigate = new Scanner(System.in);
public static Scanner playerStats = new Scanner(System.in);
public static Scanner shop = new Scanner(System.in);

static int playerGold = 0;
static String items;
static int ironSword = 200;
static int rustySword = 75;
static int lightLeatherArmor = 50;
static int minorHealthpotion = 25;
static String bought = "item added to inventory";
static String notBought = "Sorry, you don't have enough for that!";

public static void main(String[] args) {
    System.out.println("Welcome player.\nWhat is your name?");
    String playerName = playerStats.next();
    System.out.println("\nAh!" + playerName + "\n\nWe've been expecting you.\nAs we agreed, you get half the gold now and half after you kill that goblin!\nYou recieved 150 gold.");
    playerGold = playerGold +150;
    Navigate();
    }

public static void Shop() {
    System.out.println("\nWelcome to my shop\nWould you like to see my wares?");
    String shopChoice1 = shop.next();

    if (shopChoice1.equals("yes")) {
        System.out.println("\nSee anything you need?\nIron sword: " + ironSword + "\nRusty sword: " + rustySword + "\nLight leather armor: " + lightLeatherArmor + "\nMinor health potion: " + minorHealthpotion);
        }
    String shopChoice2 = shop.next();{
        System.out.println("ok");
    if (shopChoice2.equals("iron sword")) {
        IronSword();}
    else if (shopChoice2.equals("rusty sword")) {
        RustySword();}
    else if (shopChoice2.equals("light leather armor")) {
        LightleatherArmor();}
    else if (shopChoice2.equals("minor health potion")) {
        MinorhealthPotion();}
    else if (shopChoice2.isEmpty()) {
        Shop();}
    }
}

public static void IronSword() {
    if (playerGold >= ironSword) 
        System.out.println(bought);
        items = items + "Iron sword,\n";
    if (playerGold < ironSword) 
        System.out.println(notBought);
        Shop();
}

public static void LightleatherArmor() {

    }

public static void RustySword() {
    if (playerGold >= rustySword)
        System.out.println(bought);
        items = items + "Rusty Sword,\n";
    if (playerGold < rustySword) 
        System.out.println(notBought);
        Shop();
    }

public static void MinorhealthPotion() {

    }

public static void Inn() {
    System.out.println("**You enter the inn and approach the barkeep**\nHow can I help you?");

}
public static void Inventory() {
    System.out.println("Gold: " + playerGold + "\nItems: \n" + items + "\n\n");
    Navigate();

}

public static void Navigate() {
    System.out.println("\nWhat next?\n  Shop\n  Inn\n   Inv");
    String a = navigate.nextLine();

        if (a.equals("shop")) 
            Shop();
        else if (a.equals("inn")) 
            Inn();
        else if (a.equals("inv")) 
            Inventory();
}

}

*出于某种原因需要添加更多文本!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!*出于某种原因需要添加更多文本!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!!*需要为某些内容添加更多文字

扫描仪的默认分隔符是白色space。这意味着它将在每个 whitespace(space、制表符、换行符)上分解字符串。因此,如果您输入 "iron sword",您只会在 shop.next() 上获得 "iron",在之后的通话中获得 "sword"。

由于要整行阅读,可以将分隔符设置为换行符。像这样:

public static Scanner shop = new Scanner(System.in).useDelimiter("\n");

扫描仪的 Javadoc class 提到了这一点:https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html

顺便说一句:您使用哪个 IDE?如果您不熟悉编程,请熟悉它的调试功能。您可以在 shop.next() 调用后立即设置断点,然后检查 shopChoice2 的内容。这会告诉你 shop.next() 没有做你期望的事情。

编辑:

您编辑代码后出现错误:

if (shopChoice2.equals("iron sword"))
    System.out.println("ok");
    IronSword();}
if (shopChoice2.equals("rusty sword"))

这应该是:

if (shopChoice2.equals("iron sword")) {
    System.out.println("ok");
    IronSword();}
} else if (shopChoice2.equals("rusty sword"))

只有 "System.out.println("可以");"如果 if 语句为真,则被调用。请注意,如果 "if" 语句为真,则仅调用下一条语句。如果您需要执行多个语句,则需要将它们括在大括号中。作为一般规则,应该 ALWAYS 在 if 语句中使用大括号。哪怕只有一个说法。这有助于避免犯这样的错误。

编辑 2:

这应该有效。找出代码的不同之处:

String shopChoice2 = shop.next();
System.out.println(shopChoice2);
if (shopChoice2.equals("iron sword")) {
    IronSword();"
} else if (shopChoice2.equals("rusty sword")) {
    RustySword();
} else if (shopChoice2.equals("light leather armor")) {
    LightleatherArmor();
} else if (shopChoice2.equals("minor health potion")) {
    MinorhealthPotion();
} else if (shopChoice2.isEmpty()) {
    Shop();
}