如何用java中的最终输入重试调用方法?

rbpvctlc  于 2021-06-29  发布在  Java
关注(0)|答案(0)|浏览(171)

我是java初学者。我完全搞不懂这项任务。任务描述如下:
描述

package com.efimchick.tasks.risky;

import com.efimchick.tasks.risky.util.RussianRoulette;
public class RiskyShot {

final int input;
final RussianRoulette roulette;

public RiskyShot(final int input,
                 final RussianRoulette roulette) {
    this.input = input;
    this.roulette = roulette;
}

public void handleShot() /*You may not add "throws" here*/ {
    try{ roulette.shot(input);

    }  catch (Exception e) {
        if(input == 2 ){

          throw new IllegalArgumentException("File error");
        }
        if(input == 1){
            throw new IllegalArgumentException("File is missing");
        }
          if(input== 3 || input==4){

          }
          if(input == 5){

          }
          else {
              throw new RuntimeException();
          }

    }
    // handle method call
    //roulette.shot(input);
}
}

这是Russianroulete类,进口的

package com.efimchick.tasks.risky.util;

import java.io.FileNotFoundException;
import java.io.IOException;

//do not edit
public class RussianRoulette {

final FileNotFoundException fileNotFoundException = new FileNotFoundException("1");
final IOException ioException = new IOException("2");
final ArithmeticException arithmeticException = new ArithmeticException("3");
final NumberFormatException numberFormatException = new NumberFormatException("4");
final UnsupportedOperationException unsupportedOperationException = new UnsupportedOperationException("5");

final Exception[] cylinder = new Exception[]{
        fileNotFoundException,
        ioException,
        arithmeticException,
        numberFormatException,
        unsupportedOperationException,
        null
};

private final int shift;

public RussianRoulette() {
    this(0);
}

public RussianRoulette(final int shift) {
    this.shift = shift;
}

public int shot(int i) throws Exception {
    Exception e = cylinder[Math.abs(i + shift) % 6];
    if (e == null) {
        return i;
    }
    throw e;
}

}

我在“riskyshot”类中编写了“handleshot”方法的主体,但是我不知道如何重试调用“shot”方法,当它的输入是final时?我不明白如何处理这部分任务:
“不欢迎nullpointerexception。它不应该被抓住。我也不该出现。”
感谢您的帮助:)

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题