以下是我正在尝试制作的节目的摘录。我希望得到它,当有人输入短语“是”或“否”时,它会在if语句中得到响应。注意它们现在是占位符,我打算用这些语句做更多的事情。
请注意代码不完整,因此可能没有完全合理,但我的问题仍然存在。
主要课程:
package adventure;
import java.util.Scanner;
public class Main {
int boots, leggings, chestplate, helm;
static int hp = 25;
int dogde;
int dagger = 1;
int ssword = 0;
int lsword = 0;
int shield = 0;
static int strength = 1;
static int agility = 1;
static int health = 25;
int stats, inventory;
static int gold = 10;
static int x = 0;
int senemy = 0, menemy = 0, lenemy = 0, boss = 0;
public static void main (String[]args) {
System.out.println("Welcome to the adventure game! Enter 0 to start");
Scanner sc = new Scanner(System.in);
int menu = sc.nextInt();
if (menu == 0) {
System.out.println("Welcome to the menu! It can be reopened anytime by entering 0");
System.out.println("Enter:");
System.out.println("1 to view player stats");
System.out.println("2 to view your inventory");
System.out.println("3 to view your gold amount");
System.out.println("4 to continue your adventure");
while (x == 0)
switch (sc.nextInt()) {
case 1:
System.out.println("You have " + hp + " out of " + health + " hp, " + agility + " agility and " + strength + " strength");
break;
case 2:
System.out.println("You have ");
break;
case 3:
System.out.println("You have " + gold + " gold");
break;
case 4:
System.out.println("Let the adventure begin..."); x=2;
break;
default:
System.out.println("Menu exited"); x=1;
break;
}
}
if (x == 2){
Level1 level1Object = new Level1();
}
}
}
我特别关注案例2。
package adventure;
import java.util.Scanner;
public class Level1 {
String yes = "Yes";
String no = "No";
{ Scanner sc = new Scanner(System.in);
int x = 2;
System.out.println("Level 1 out of 12");
System.out.println("You wake up in a town called Haven");
System.out.println("Enter:");
System.out.println("1 = Visit the armory, 2 = Find a quest, 3 = Proceed to the next level");
while (x == 2){
switch (sc.nextInt()){
case 1:
System.out.println("You enter the armory"); //placeholder
break;
case 2:
System.out.println("You ask the local guard captain for a quest");
System.out.println("Captain: Orcs have taken the south watchtower, take it back and I will reward you 10 gold");
System.out.println("Accept quest? Enter: Yes or No");
String input = sc.nextLine();
System.out.println(input);
if (input.equals("Yes")) {
System.out.println("Quest accepted"); //placeholder
}
else if (input.equals("No")) {
System.out.println("Quest denied"); //placeholder
}
break; //placeholder
case 3:
System.out.println("You proceed to the next level"); //placeholder
break;
default:
System.out.println("Invalid response");
System.out.println(0); //test, delete
break;
}
}
}
}
错误:
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:840)
at java.util.Scanner.next(Scanner.java:1461)
at java.util.Scanner.nextInt(Scanner.java:2091)
at java.util.Scanner.nextInt(Scanner.java:2050)
at adventure.Level1.<init>(Level1.java:14)
at adventure.Main.main(Main.java:52)
编辑我通过将“否”线更改为“是”并使用了另一个扫描仪而不是“sc”来修复它。
答案 0 :(得分:1)
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Level1 {
private static String YES = "Yes";
private static String NO = "No";
public Level1(){
Scanner sc = new Scanner(System.in);
int x = 2;
System.out.println("Level 1 out of 12");
System.out.println("You wake up in a town called Haven");
System.out.println("Enter:");
System.out.println("1 = Visit the armory, 2 = Find a quest, 3 = Proceed to the next level");
while (x == 2){
switch (sc.nextInt()){
case 1:
System.out.println("You enter the armory"); //placeholder
break;
case 2:
System.out.println("You ask the local guard captain for a quest");
System.out.println("Captain: Orcs have taken the south watchtower, take it back and I will reward you 10 gold");
System.out.println("Accept quest? Enter: Yes or No");
String input = sc.nextLine();
System.out.println(input);
if (input.equalsIgnoreCase(YES)) {
System.out.println("Quest accepted"); //placeholder
}
else if (input.equalsIgnoreCase(NO)) {
System.out.println("Quest denied"); //placeholder
}
break; //placeholder
case 3:
System.out.println("You proceed to the next level"); //placeholder
break;
default:
System.out.println("Invalid response");
System.out.println(0); //test, delete
break;
}
}
}
}
答案 1 :(得分:-1)
您正在使用sc.nextInt()
阅读输入。这会阻碍“是”或“否”等输入。相反,您应该简单地将下一行读作字符串。然后,您可以测试它是“是”还是“否”,如果不是,则可以尝试将其解析为int
。