Results 1 to 6 of 6
  1. #1
    Zebra's Avatar
    Zebra is offline Member
    Join Date
    Apr 2008
    Location
    Louisville, Indiana/Kentucky
    Posts
    64
    Rep Power
    0

    Default [SOLVED] Need help with Loops...please!

    Hey thanks for your time. Can you please help me with my program. Originally this code asks the user to input a password until he/she gets it correct. But I am suppose to change the code to make it where the user gets only 3 tries to get it correct. Can somebody please help me and tell me what I am suppose to do. Thanks!

    import java.util.Scanner;
    class Example3 {
    public static void main (String[] args)
    {
    Scanner scan = new Scanner(System.in);
    String password = "";
    while(!(password.equals("branes")))
    {
    System.out.println("What is the password");
    password = scan.nextLine();
    }
    System.out.println("Welcome!");
    }
    }

  2. #2
    Zebra's Avatar
    Zebra is offline Member
    Join Date
    Apr 2008
    Location
    Louisville, Indiana/Kentucky
    Posts
    64
    Rep Power
    0

    Default

    Sorry about this guys I have already figured this one out...thanks anyways! Mods you can close this if you want.

  3. #3
    DonCash's Avatar
    DonCash is offline Moderator
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    240
    Rep Power
    10

    Default

    Hey Zebra, fancy posting your solution just incase it helps others in the future?
    Did this post help you? Please me! :cool:

  4. #4
    Zebra's Avatar
    Zebra is offline Member
    Join Date
    Apr 2008
    Location
    Louisville, Indiana/Kentucky
    Posts
    64
    Rep Power
    0

    Default

    BLUE= Change to whatever you want

    Java Code:
    import java.util.Scanner;
    class Loop1 {
    
        public static void main (String[] args)
    {
    
            int count = 1;
    
            Scanner scan = new Scanner(System.in);
            String password = "";
    
            boolean foundpassword = false;
            while(count <= 3)
            {
                System.out.println("What is the password");
                password = scan.nextLine();
                if(password.equals("[COLOR="Navy"]Zebra is cool[/COLOR]"))
                {
                    count = 4;
                    foundpassword = true;
                }
                else
                {
                    count = count + 1;
                }
            }
    
                if(foundpassword == true)
                {
                    System.out.println("[COLOR="Navy"]Welcome![/COLOR]");
                }
                else
                {
                    System.out.println("[COLOR="Navy"]Sorry...you failed![/COLOR]");
    
                }
            }
        }
    Last edited by DonCash; 04-09-2008 at 04:18 PM.

  5. #5
    DonCash's Avatar
    DonCash is offline Moderator
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    240
    Rep Power
    10

    Default

    Nice one!

    I have done it for you this time but in future please add the [code] [ /code] tags around your code. It makes it much easier to reads and avoids errors.
    Did this post help you? Please me! :cool:

  6. #6
    Zebra's Avatar
    Zebra is offline Member
    Join Date
    Apr 2008
    Location
    Louisville, Indiana/Kentucky
    Posts
    64
    Rep Power
    0

Similar Threads

  1. [SOLVED] Need help with Slope (Loops)
    By Zebra in forum New To Java
    Replies: 9
    Last Post: 04-18-2008, 03:39 AM
  2. Question about loops
    By BHCluster in forum New To Java
    Replies: 4
    Last Post: 04-16-2008, 05:40 PM
  3. Loops (while do etc)
    By manupr in forum New To Java
    Replies: 1
    Last Post: 01-15-2008, 04:59 AM
  4. Nested loops?
    By gabriel in forum New To Java
    Replies: 4
    Last Post: 08-06-2007, 04:51 PM
  5. Help me: loops in java
    By silvia in forum New To Java
    Replies: 3
    Last Post: 07-19-2007, 06:47 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
  •