小编Sha*_*cer的帖子

使用错误的用户输入Java进行异常处理

我需要帮助使用错误的用户输入进行异常处理.我正在创建一个基于文本的游戏,欢迎用户,然后进入主菜单.然后它告诉用户选项,然后查找用户输入.出于某种原因,每当我输入1或2时,它会说:"您的输入无效,请再试一次"然后返回选择.我不知道到底哪里出错了,希望有人可以帮助我.此外,它也不会捕获Mismatch Exception.希望你能帮忙!谢谢,山丹

   public static void main(String[] args) {



    System.out.println("Welcome to Spec Ops!");
    System.out.println("Please state your name:");
    Scanner name = new Scanner(System.in);
    String Name = name.next();

    System.out.println("Hello "+Name);
    mainMenu();

    }


public static void mainMenu() {

    System.out.println("1. Story Mode");
    System.out.println("2. Infinant Combat");



    Scanner input = new Scanner(System.in);



    Object Selection = input.nextInt();

    boolean validOption = true;
    Integer x;


    try {
    x = (Integer)Selection;
    } catch(ClassCastException cce){
        System.out.println("Your input is invalid, please try again");
        validOption = false;
    } catch(InputMismatchException ime){
        System.out.println("Your input is invalid, …
Run Code Online (Sandbox Code Playgroud)

java user-input exception try-catch java.util.scanner

3
推荐指数
1
解决办法
3万
查看次数

该方法必须重写或实现超类方法

快问。我正在使用 Eclipse,并且收到 The method Must override or Implement a superclass method 错误,但 Eclipse 使用的是 Java 1.7 的合规性。
这是我的代码:

public abstract class M4 implements Armory {

@Override
public Integer weaponAmmo(int wepAmmo) {
    wepAmmo = 10;
    return wepAmmo;
}

@Override
public Integer weaponDamage(int wepDamage) {
    wepDamage = 2;
    return wepDamage;
}

@Override
public String weaponName(String wepName) {
    wepName = "M4";
    return wepName;
}
Run Code Online (Sandbox Code Playgroud)

这是接口代码:

public interface Armory {
        public Integer weaponAmmo(int wepAmmo);
        public Integer weaponDamage(int wepDamage);
        public String weaponName(String wepName);

    }
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

java eclipse implementation interface superclass

3
推荐指数
1
解决办法
2万
查看次数