Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/377.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何检查字符串的扫描器是否相等,并在java中使用它?_Java_String_If Statement - Fatal编程技术网

如何检查字符串的扫描器是否相等,并在java中使用它?

如何检查字符串的扫描器是否相等,并在java中使用它?,java,string,if-statement,Java,String,If Statement,下面是我正在制作的一个节目的摘录。我想知道,当某人输入短语“是”或“否”时,它会在if语句中得到响应。请注意,现在它们是占位符,我计划用这些语句做更多的事情 还请注意,代码不完整,因此可能没有完全的意义,但我的问题仍然应该存在 主要类别: package adventure; import java.util.Scanner; public class Main { int boots, leggings, chestplate, helm; static int hp =

下面是我正在制作的一个节目的摘录。我想知道,当某人输入短语“是”或“否”时,它会在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)
线程“main”java.util.InputMismatchException中的异常 位于java.util.Scanner.throwFor(Scanner.java:840) 下一步(Scanner.java:1461) 位于java.util.Scanner.nextInt(Scanner.java:2091) 位于java.util.Scanner.nextInt(Scanner.java:2050) 在adventure.Level1.(Level1.java:14) 位于adventure.Main.Main(Main.java:52)
编辑我将“否”行更改为“是”行,并使用了另一个扫描仪而不是“sc”行。

您正在使用
sc.nextInt()读取输入。
。这将扼杀像“是”或“否”这样的输入。相反,您应该简单地将下一行作为字符串读取。然后,您可以测试它是“是”还是“否”,如果两者都不是,那么您可以尝试将其解析为
int

问题到底出在哪里?让我再添加一些信息,这样Level1类中的所有代码都没有方法。我是一名新手程序员,如果代码看起来很糟糕,我向您道歉:\n我以为他/她正在使用sc.nextInt()获取内部开关语句。你确定吗?谢谢你的回答。有人提到我在第二个类中没有使用方法,我该怎么做?@Ashish-异常在该行抛出(在类
Level1
中,而不是在主类中)。仅当输入为整数值时,OP才需要输入
开关
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;
     }
   }    
  }
}