有 Java 编程相关的问题?

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

java无法确定用户何时为我的自定义异常输入数字小于0的问题

我似乎无法理解这一点。如果用户输入了无效的余额,我如何重新命令他们再次输入余额,并继续我的程序

  //EDITED STILL WONT WORK PROPERLY
 boolean again;
    while(again = true) 
    {
        try {

            // pass object to printwriter and pw to write to the file
            pw = new PrintWriter(fw);

            System.out.print("Input beginnning balance: ");
            balance = input.nextDouble();
            again = false;
            // pass user input to object
            AccountWithException acctException = new AccountWithException(fullName, balance, id, RATE);
            again = false;

            System.out.println(acctException.toString());

            // copy object to created file
            pw.println(acctException.toString());
            again = false; 

        // custom exception
        } catch (InvalidBalanceException e) {
            System.out.println(e.getMessage());
        } catch(FileNotFoundException e) {
            System.out.println(e.getMessage());
        } finally {
            pw.close();

共 (3) 个答案

  1. # 1 楼答案

    一个问题是你的while循环。这项作业对评估没有帮助。 此外,如果抛出异常,它将触发catch块

     boolean again = true;
        while(again) // check for true instead of making assignment 
        {
            try {
    
                // pass object to printwriter and pw to write to the file
                pw = new PrintWriter(fw);
    
                System.out.print("Input beginnning balance: ");
                balance = input.nextDouble();
                // pass user input to object
                
                // throw an exception to trigger catch block
                // according to catch block, should be an InvalidBalanceException
                if(balance < 0) throw new AccountWithException(fullName, balance, id, RATE);
                again = false; // this line isn't run if it fails to exception
    
            // custom exception
            } catch (InvalidBalanceException e) {
                System.out.println(e.getMessage());
    
                // copy object to created file
                pw.println(e.toString());
            } catch(FileNotFoundException e) {
                System.out.println(e.getMessage());
            } finally {
                pw.close();
    
  2. # 2 楼答案

    do{
        //take user input
        //check for invalid balance
        try{
            if(balance is invalid){
                throw new InvalidBlanceException();
            }
        } catch(InvalidBalanceException e) {
            //user input again reprompt
        }
    }while(end of file)
    
  3. # 3 楼答案

    您可以抛出Invalidbalanceexception,然后像这样在catch块中捕获它

    try {
    
            // pass object to printwriter and pw to write to the file
            pw = new PrintWriter(fw);
    
            // pass user input to object
            AccountWithException acctException = new AccountWithException(fullName, balance, id, RATE);
    
            System.out.println(acctException.toString());
    
            // copy object to created file
            pw.println(acctException.toString());
    throw new InvalidBalanceException ();
    
            // custom exception if balance < 0
        } catch (InvalidBalanceException e) {
            System.out.println(e.getMessage());
            System.out.println("Re-enter balance: ");
            balance = input.nextDouble();
        } catch(FileNotFoundException e) {
            System.out.println(e.getMessage());
        } finally {
            System.out.println("Text file closed, program complete...");
            pw.close();
        }