再次编辑:
每次我运行它并输入“String a”时,所有方法都会被调用,但我不知道为什么。
我知道这都是新手的东西,但直觉上相反,java 上有太多的教程和线程来正确解决这样的基本问题。
导入java.util.*; 公共(public)类 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();
}
}
*由于某种原因需要添加更多文本!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!*由于某种原因需要添加更多文本!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!*需要添加更多文本
最佳答案
扫描仪的默认分隔符是空格。这意味着它将在每个空格(空格、制表符、换行符)上分解字符串。因此,如果您输入“铁剑”,您只会在 shop.next() 上获得“铁”,并在之后的调用中获得“剑”。
由于要读取整行,因此可以将分隔符设置为换行符。像这样:
public static Scanner shop = new Scanner(System.in).useDelimiter("\\n");
Scanner 类的 Javadoc 提到了这一点: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(“ok”);”如果 if 语句为真则被调用。请注意,如果“if”语句为真,则仅调用下一个语句。如果您需要执行多个语句,则需要将它们括在大括号中。作为一般规则,应该始终在 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();
}
关于java - 满足字符串选择条件,不调用类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48552659/