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 3 of 3

Thread: Java loop help

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java loop help

    Hi I need help with this java code, I need the program to run until user enters -1 instead of 0 or 1

    [code]
    public static void main(String args[])
    {
    Scanner input = new Scanner(System.in); //New scanner for input

    System.out.println("Ange längde på de 2 lika långa sidorna (Avsluta med -1)");
    int length = input.nextInt(); //variable length with the users input

    System.out.println("Ska de räta vinkeln vara nedåt (0) eller uppåt (1)");
    int side = input.nextInt();


    if(side == 0) // if user enter 0 this loop is running
    for(int i = 1; i<=length; i++)

    {
    for(int j = 1; j<=i; j++){
    System.out.print("*");

    }
    System.out.println();
    }


    if(side == 1) // if user enter 1 this loop is running
    for(int i = length; i>=0; i--)
    {
    for(int j = 0; j<i; j++){
    System.out.print("*"); // prints * on the same line depends on loop

    }
    System.out.println(); // new line
    }

    }// end main

    [code]


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Java loop help

    Sounds like a job for a while loop or a do-while loop to me.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Mar 2012
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java loop help

    do {

    // Code

    // if (input == 1)
    // end = true;

    }
    while (!end);

    Very simple do while loop, use it to achieve your desired result.

Similar Threads

  1. loop in java
    By casperl90 in forum Loops & Control Statements
    Replies: 2
    Last Post: August 8th, 2011, 03:17 AM
  2. loop in java
    By jonnitwo in forum Loops & Control Statements
    Replies: 4
    Last Post: July 8th, 2011, 02:13 PM
  3. problems with loop in Java App
    By dmonx in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 13th, 2010, 04:13 PM
  4. for loop in java
    By kissmiss in forum Loops & Control Statements
    Replies: 1
    Last Post: December 16th, 2009, 03:47 AM
  5. JAVA for loop
    By tazjaime in forum Loops & Control Statements
    Replies: 2
    Last Post: August 18th, 2009, 07:43 PM