Java if语句和try catch不';不能防止错误

Java if语句和try catch不';不能防止错误,java,if-statement,try-catch,Java,If Statement,Try Catch,我的这部分代码应执行以下操作: 检查输入是否为int类型(try-catch) 如果输入为int,请检查它是否位于列表之间: 代码 根据您的代码,您可能需要更像这样的东西 while(true){ try{ id=sclocal.nextInt(); if(listperformer.size() <= id || id < 0) { System.out.println("Invalid input! 294");

我的这部分代码应执行以下操作:

  • 检查输入是否为int类型(try-catch)
  • 如果输入为int,请检查它是否位于列表之间:
  • 代码


    根据您的代码,您可能需要更像这样的东西

    while(true){
        try{
            id=sclocal.nextInt();
            if(listperformer.size() <= id || id < 0) {
                System.out.println("Invalid input! 294");
            }
            else {
                idlist.add(listperformer.get(id));
                break;
            }
        } catch(InputMismatchException exception) {
            System.out.println("Invalid input!");
        }
    }
    
    while(true){
    试一试{
    id=sclocal.nextInt();
    
    if(listperformer.size()我在这里看到两个问题

  • 第一个是,在第一个catch中,您只向System.out打印一条消息,然后程序将继续正常运行,从而转到下一个
    id=sclocal.nextInt()
    。相反,您可能应该将函数保留在catch子句中
  • 现在,在try-catch块之后,您可以再次调用
    id=sclocal.nextInt()
    。这一次从sclocal获取一个全新的值时,不会捕获可能的异常。您可以通过删除该调用并将if-else子句移到try块中来解决此问题
  • 您需要在try-catch块中包含所有依赖于异常抛出操作的代码


    我重新组织了几行代码,在代码中添加了一些注释,还更改了System.out语句以反映实际发生的情况。在catch块中,“sclocal.nextLine()”将首先使用导致异常的无效输入,以便控制可以向前移动到下一次迭代

    一般来说,最好使用“camelCase”作为变量名

    public static void selection(List<Performer> listperformer) {
        int i = 0;
        List<Performer> idlist = new ArrayList<Performer>();
        Scanner sclocal = new Scanner(System.in);
    
        if (listperformer.isEmpty() == true) {
            System.out.println("- empty -");
            return;
        } else {
            int id;//This is being used as an offset, so I recommend you rename it to "offset"
            System.out.println("Enter ID:");
    
            while (sclocal.hasNext()) {
                try {
                    id = sclocal.nextInt();
                    if (listperformer.size() <= id || id < 0) {
                        System.out.println("Invalid input! You requested the element at offset [" + id + "], but the max offset available is [" + (listperformer.size()-1) + "]. Exiting.");
                        return;
                    } else {
                        System.out.println("Input is valid. We have added the offset identifier [" + id + "] to listperformer.");
                        idlist.add(listperformer.get(id));
                    }
                } catch (InputMismatchException exception) {
                    System.out.println("Invalid input!");
                    sclocal.nextLine();//throw away the invalid input so that we can await for the next input
                }
                System.out.println("Enter ID:");
            }
        }
    }
    
    公共静态无效选择(列表listperformer){
    int i=0;
    List idlist=new ArrayList();
    扫描仪sclocal=新扫描仪(System.in);
    if(listperformer.isEmpty()==true){
    System.out.println(“-empty-”);
    返回;
    }否则{
    int id;//这被用作偏移量,因此我建议您将其重命名为“offset”
    System.out.println(“输入ID:”);
    while(sclocal.hasNext()){
    试一试{
    id=sclocal.nextInt();
    
    如果(listperformer.size(),您观察到的“有问题的”用例完全按照预期工作,或者更确切地说,按照编码和预期工作。要更好地理解代码所做的工作,请使用IDE中内置的调试器(或独立调试器)然后一步一步地浏览代码。在学习编程时,使用调试器是必不可少的,而不是可选的。如果调试器不起作用,将导致无休止的输出:“无效输入!294”好的,添加所有缺少的信息,你能再次检查一下吗?我重新整理了你的代码并添加了一些注释以使事情正常运行。在这个线程中还有另一个关于异常处理的注释,所以我在那里添加了一行代码,你需要让整个事情正常运行。
    while(true){
        try{
            id=sclocal.nextInt();
            if(listperformer.size() <= id || id < 0) {
                System.out.println("Invalid input! 294");
            }
            else {
                idlist.add(listperformer.get(id));
                break;
            }
        } catch(InputMismatchException exception) {
            System.out.println("Invalid input!");
        }
    }
    
    import java.util.*;
    
    public class TryCatch
    {
        public static void main(String[] args)
        {
            Scanner sclocal = new Scanner(System.in);
            List<Integer> listperformer = new ArrayList<>(Arrays.asList(1,2,3));
            List<Integer> idlist = new ArrayList<>();
            try
            {
                int id=sclocal.nextInt();
                if(listperformer.size() <= id || id < 0)
                {
                    System.out.println("Invalid input! 294");
                    return;
                }
                else
                {
                    idlist.add(listperformer.get(id));
                }
            }
            catch(InputMismatchException exception)
            {
                System.out.println("Invalid input!");
            }
        }
    }
    
    Input: string
    Output: Invalid input!
    
    Input: 1
    Output: [2]
    
    Input: 5
    Output: Invalid input! 294
    
    public static void selection(List<Performer> listperformer) {
        int i = 0;
        List<Performer> idlist = new ArrayList<Performer>();
        Scanner sclocal = new Scanner(System.in);
    
        if (listperformer.isEmpty() == true) {
            System.out.println("- empty -");
            return;
        } else {
            int id;//This is being used as an offset, so I recommend you rename it to "offset"
            System.out.println("Enter ID:");
    
            while (sclocal.hasNext()) {
                try {
                    id = sclocal.nextInt();
                    if (listperformer.size() <= id || id < 0) {
                        System.out.println("Invalid input! You requested the element at offset [" + id + "], but the max offset available is [" + (listperformer.size()-1) + "]. Exiting.");
                        return;
                    } else {
                        System.out.println("Input is valid. We have added the offset identifier [" + id + "] to listperformer.");
                        idlist.add(listperformer.get(id));
                    }
                } catch (InputMismatchException exception) {
                    System.out.println("Invalid input!");
                    sclocal.nextLine();//throw away the invalid input so that we can await for the next input
                }
                System.out.println("Enter ID:");
            }
        }
    }