Results 1 to 2 of 2

Thread: While LOOP

Threaded View

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Feb 2002
    Posts
    159

    While LOOP

    i wrote this piece of code

    Code:
    public class CheckBankAccount{
     public static void main(String[] argv) {
        
        int pinNo, userTries, counter;
        float cashAmount;
    
        //  Start of an endless loop since cashpoints never sleep.
        do {
    
            // Users name should appear
            BankAccount b = new BankAccount("James",1234);
            System.out.println(b.getName());
            
            // set the try counter to zero
            counter = 0;
            
            // loop to check max. 3 times for correct PIN starts here. 
            do {
    
                //ask user for PIN. 
                 System.out.println("Please enter your PIN");
       
                 // user enters PIN into the pinNo variable. 
                 pinNo = UserInput.readInt();
    
                 if (pinNo == 9999){
                    System.out.println ("bye");
                    System.exit(0);
                 }
                 
                 
                      
                 // increment counter for tries
                 ++ counter;
                 
            // end of the 'do' loop. Use a while here that checks if the PIN is incorrect AND
            // the counter for tries is smaller than 3
            } while(     (pinNo != 1234)   &&   (counter < 3)    );
                                          
                 
                 
            // this part of the program only executes if the counter for tries is smaller than 3
            // Use an if statement here that checks that.
            if (counter < 3) {
    
                // Ask how much the user wants to withdraw. 
                System.out.println("How much do you want to withdraw?");
    
                // User enters amount.
                cashAmount = UserInput.readFloat();
    
                // Print message confirming transaction: "You have withdrawn xxx Pounds" 
                System.out.println(" has withdrawn " +cashAmount+ " Pounds");
    
                // end of the if statement
            }
        // end of the endless 'do'-'while' loop. 
        } while (true);
    
        
        
     } // end of main
    } // end class
    I’m not sure on how to approach this problem

    if the user inputs the wrong PIN this message should appear

    "Invalid PIN, please try again"

    if the user inputs the PIN incorrect 3 times this message should appear.

    "3 x invalid PIN: aborting"

    and exit the program using the System.exit(0) function

    any ideas???
    Last edited by NOTSOSURE; Feb 1st, 2003 at 03:22 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width