Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: how do i loop this simple java code?

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default how do i loop this simple java code?

    for example if i input 2 it would go straight to option 2 and then if i press 1 it would go to option 1. from there i want to be able to go to any other option without the program stopping. help would be greatly appreciated


    Scanner sc = new Scanner(System.in);
    Account chequingAcc = new ChequingAccount();
    System.out.println("Please enter your Unique Number");
    chequingAcc.setuniqueString(sc.nextLine(…
    System.out.println("verefied, what would you like to do?");
    System.out.println("to check balance press 1");
    System.out.println("to debit account press 2");
    System.out.println("to credit account press 3");
    System.out.println("to overdraft account press 4");
    System.out.println("to exit press 5");
    int k = sc.nextInt();
    if(k<=0 ||k>5){
    System.out.println("invalid ");
    k =sc.nextInt();
    }

    while( k==1){
    System.out.println("--------------------…
    System.out.println("Chequing Account current balance is $" + chequingAcc.getbalance());
    System.out.println("what you like do now?");
    System.out.println("to debit account press 2");
    System.out.println("to credit account press 3");
    System.out.println("to overdraft account press 4");
    System.out.println("to exit press 5");
    k =sc.nextInt();


    while (k==2){
    System.out.println("--------------------…
    System.out.println("enter ammount to debit to Chequing Account");
    chequingAcc.setDebit(sc.nextDouble());
    System.out.println("Chequing Account current balance is $" + chequingAcc.getbalance());
    System.out.println("what you like do now?");
    System.out.println("to check balance press 1");;
    System.out.println("to credit account press 3");
    System.out.println("to overdraft account press 4");
    System.out.println("to exit press 5");
    k =sc.nextInt();

    while(k==3){
    System.out.println("--------------------…
    System.out.println("enter the ammount to credit to Chequing Account");
    chequingAcc.setCredit(sc.nextDouble());
    System.out.println("Chequing Account current balance is $" + chequingAcc.getbalance());
    System.out.println("what you like do now?");
    System.out.println("to check balance press 1");;
    System.out.println("to debit account press 2");
    System.out.println("to overdraft account press 4");
    System.out.println("to exit press 5");
    k =sc.nextInt();

    while(k==4){
    System.out.println("--------------------…
    System.out.println("enter the ammount to overDraft to Chequing Account");
    chequingAcc.setoverDraft(sc.nextDouble()…
    System.out.println("Chequing Account current balance is $" + chequingAcc.getbalance());
    System.out.println("what you like do now?");
    System.out.println("to check balance press 1");;
    System.out.println("to debit account press 2");
    System.out.println("to credit account press 3");
    System.out.println("to exit press 5");
    k =sc.nextInt();

    while(k==5){
    System.out.println("thank for using the bank");
    System.exit(0);


    }
    }
    }
    }





    }
    }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: how do i loop this simple java code?

    Put the code you want to repeat inside of a loop. Since time to end the loop will be determined by user input, use a while loop with a condition that tests what the user entered to see if if should continue looping.

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.

    Be sure to properly format the code so that all statements do NOT start in the first column. Nested statements should be indented.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Why do I get an exception in this simple loop?
    By jean28 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 31st, 2012, 05:24 AM
  2. Help!, newbie, interested in Java, Very simple code, completely stuck!
    By munkybunky in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 30th, 2011, 07:25 AM
  3. Java uberNoob, requesting help with simple loop issue
    By miketeezie in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 21st, 2011, 09:13 PM
  4. boolean value in a simple loop (do-while)
    By chronoz13 in forum Loops & Control Statements
    Replies: 5
    Last Post: October 18th, 2009, 12:05 AM
  5. Simple graphics practice on previous Java code
    By amrawad_85 in forum AWT / Java Swing
    Replies: 5
    Last Post: June 19th, 2009, 10:30 AM