有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Catch block不允许用户继续执行程序

我对java中的catch块有点困难。非常感谢您的帮助

do {
    System.out.println ("If you want to exit the program, press 0. To continue, press 1.");
        try {
            returnint = Input.nextInt();    
        } catch (Exception e) {
            returnint=12;
            return; //after this code executes, I CANNOT RE-ENTER A NUMBER
        } finally {
        if (!(returnint==1 || returnint==0)) {
            System.out.println ("Invalid response. Please retry.");
        } continue;
    } while (returnint!=1 && returnint!=0);

所以问题是出于某种原因,循环不会重复。在此方面的任何帮助,包括更好地理解try-catch-finally blocks,将不胜感激

编辑:在程序开始时还有一个try块,程序返回后,它也会以某种方式触发该块。有人帮忙吗

编辑2:下面的完整代码

import java.util.Scanner;


public class PrimeNumberChecker {

/**
 * @param args
 */
public static void main(String[] args) {
    int returnint=1;
    boolean isprime=true;
    Scanner Input = new Scanner(System.in); //creates scanner
do{ 
    long prime,root,primediv,i;
    try {
    System.out.println("Please input a number to be checked"); //prompts user for input
    prime = Input.nextLong();//detects and stores next int input
    }catch(Exception exc){
        System.out.println("The following exception has been thrown: "+exc+"  Program aborted.");
        returnint=0;
        return;
    }
    root=(long) Math.sqrt(prime); //takes the int sqrt of the input number
    for(i=2;(i<=root&&isprime==true);i++){ //for loop to check for prime factors
        if (prime%i==0) {  //if a number divides into the number exactly, this returns true
            isprime=false;  //and so this returns false
        }
    }

    if (isprime==false) {
        primediv=prime;
        System.out.println("The number is not prime.");
        System.out.println("The factors of the number are: ");
            for(i=2;primediv>1;i++) {   //this is a loop to factorise the number if it is NOT prime
                if (primediv%i==0) {   //if it finds a factor, it prints it and checks if the factor appears twice
                    primediv=primediv/i;
                    System.out.println(i);
                    i--;
                }
            }
    } else {
        System.out.print(prime);  //the output if the number is prime
        System.out.println(" is a prime number");
    }
    try {
        do {
            System.out.println ("If you want to exit the program, press 0. To continue, press 1.");
            if (!(returnint==1 || returnint==0)) {
                System.out.println ("Invalid response. Please retry.");
            }
            returnint = Input.nextInt();    
          continue;
        } while (returnint!=1 && returnint!=0);
    } catch (Exception e) {
        System.out.println("ERROR: " + e.getMessage());
        throw e;
    } finally {   
        System.out.println("Exit. Return value was set to " + returnint);
    }   
}while (returnint==1);
}

}


共 (2) 个答案

  1. # 1 楼答案

    return语句将控件返回给调用程序。你有没有试着评论一下,看看执行情况

    首先,确定捕获异常时要执行的操作逻辑

  2. # 2 楼答案

    这个怎么样:

    int returnint = 0;
    try {
        do {
            System.out.println ("If you want to exit the program, press 0. To continue, press 1.");
            if (!(returnint==1 || returnint==0)) {
                System.out.println ("Invalid response. Please retry.");
            }
            returnint = Input.nextInt();    
          continue;
        } while (returnint!=1 && returnint!=0);
    } catch (Exception e) {
        System.out.println("ERROR: " + e.Message());
    } finally {   
        System.out.println("Exit. Return value was set to " + returnint);
    }