Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/349.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 - Fatal编程技术网

Java 我似乎不知道该把它放在哪里

Java 我似乎不知道该把它放在哪里,java,Java,在过去的两个月里,我刚开始编写java代码,向我的朋友索要代码,并尝试在上面进行实验。问题是,我不知道该怎么做。我遗漏了什么吗?在异常处理方面,您应该只在try-catch块中放置可能导致异常的代码行/代码块。例如,在您的案例中,您可以尝试捕获用户输入,以确保他提供了整数: Story.direct3(); br.readLine(); Story.des(); try{ short choice1

在过去的两个月里,我刚开始编写java代码,向我的朋友索要代码,并尝试在上面进行实验。问题是,我不知道该怎么做。我遗漏了什么吗?

在异常处理方面,您应该只在try-catch块中放置可能导致异常的代码行/代码块。例如,在您的案例中,您可以尝试捕获用户输入,以确保他提供了整数:

 Story.direct3();
            br.readLine();
            Story.des();
            try{
            short choice1 = Short.parseShort(br.readLine());

            switch(choice1){
                case 1:
                    Display.yes();
                    br.readLine();
                    Story.rid();
                    String answ = br.readLine();

                    if ("Gravity".equals(answ) || "gravity".equals(answ)){
                        Display.correct();
                        Short choice2 = Short.parseShort(br.readLine());

                        switch(choice2){
                            case 1:

                                break;
                        }
                    }
                    else{
                        int less = player.hp - 10;
                        System.out.println("Wrong answer! Your hp will be lessen by 10. The correct answer is : 'Gravity'");
                        System.out.println("Your remaining HP is: " + less + "[press enter to continue...]");
                        br.readLine();
                        Story.wrong();
                        br.readLine();

                    }
                    break;
                case 2:
                    Story.wrong1();
                    br.readLine();
                    break;
            }
            break;
    }
            catch(NumberFormatException ex){
                System.out.println("Would you like to change your battle item?"
            + "\n[1] Yes"
             + "\n[2] No");
            }
但这只会检查他的输入是否为数字,如果他写下3或4,而你的开关无法处理该怎么办?您应该包括开关的默认情况,或者检查数字是否大于2或小于1,然后重复问题

此外,在不依赖错误处理的情况下,可以更轻松地处理此类输入:

try{
        short choice1 = Short.parseShort(br.readLine());
}catch(NumberFormatException ex){
    System.out.println("Would you like to change your battle item?"
        + "\n[1] Yes"
         + "\n[2] No");
}

请记住,如果可以在不使代码过于复杂的情况下使用try-catch,则应尽量避免使用try-catch。在这种情况下,您将得到更简单的代码。

在哪里需要它?现在代码有什么问题?请多解释一下你有什么问题。@SamiKuhmonen当我输入错误答案时,玩家应该能够装备战斗物品。那就是你想改变你的战斗物品的地方?出来了。我该怎么做?
String s = br.readLine();
while(!s.equals("2") || s.equals("1")){
    System.out.println("Invalid input, try again:");// change message to fit your game
    s = br.readLine();
}
if(s.equals("1")){
    //Do something
}else{
    //Do something
}