简体   繁体   English

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

[英]How to check if a scanner for a string is equal and use that in java?

The following is an excerpt from a program I'm trying to make. 以下是我要制作的程序的摘录。 I want to get it so when someone enters in the phrase "Yes" or "No" it gets the response in the if-statements. 我想要得到它,所以当有人输入短语“是”或“否”时,它将在if语句中得到响应。 Notice they are placeholders for now, I plan on doing something more with those statements. 注意,它们现在是占位符,我计划对这些语句做更多的事情。

Please also take note that the code is incomplete, so it may not make complete sense, but my question should still. 另请注意,代码不完整,因此可能不完整,但我的问题仍然应该如此。

Main class: 主班:

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();

            }
    }
}

My particular concern is with case 2. 我特别关心的是案例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;
        }
    }
    }
}

Error: 错误:

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)

EDIT I fixed it by changing the "No" line to look like the "Yes" one and used another scanner than the 'sc' one. 编辑我通过更改“否”行使其看起来像“是”来修复它,并使用了另一台扫描仪而不是“ sc”扫描仪。

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;
     }
   }    
  }
}

You're reading the input with sc.nextInt() . 您正在使用sc.nextInt()读取输入。 That will choke on inputs like "Yes" or "No". 这会阻塞“是”或“否”之类的输入。 Instead, you should simply read the next line as a string. 相反,您应该简单地将下一行作为字符串读取。 You can then test whether it is "Yes" or "No" and, if it is neither, you can then try parsing it as an int . 然后,您可以测试它是“是”还是“否”,如果都不是,则可以尝试将其解析为int

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM