Bluej/Java loop?

996 views Asked by At

I'm still new in Java, and I'm learning in Bluej for class. My class is doing a small fun program to learn, and I'm wondering... How can I make the program go through the script (or restart) itself? Sorry if you get this a lot, but I've been looking for over an hour for the answer with no success. I still need it for a few other programs as well lol

Here's the code:

import java.util.Scanner;
public class TriangleFirst
{

    public static void main (String [] args) {
          System.out.println ("\f");
          Scanner SC = new Scanner(System.in);
          int numbstars;


          System.out.println ("How large would you like your (sideways) pyramid to be? (insert a number)");
          numbstars = SC.nextInt();
        for (int i=1; i<= numbstars; i++) {
                for (int j=0; j < i; j++) {
                    System.out.print ("*");
                }
                //generate a new line
                System.out.println ("");
            }

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

                }
                //generate a new line
                System.out.println ("");
            }
    }
}
3

There are 3 answers

0
Nefari0uss On BEST ANSWER

The idea here is to do something over and over again until you are told otherwise. You are already exposed to this idea in for loops. while loops are another kind of loop to know about. A while loop is defined something like so:

  while(condition is true) {
    // do something
 }

It will execute all of the code inside it over and over again until you force it to stop via break; or the condition is false (changing a Boolean, a number goes below 0, what ever you make your condition). A more advance form of a while loop is the do while loop. A do while looks something like this:

  do {
    // do something
 } while(condition is true);

The difference here is that a do while will always execute once. After that it will evaluate the condition before deciding whether to run again or not. A while loop may never execute! It all depends on the condition.

Now, for your condition, you can do a method call. So:

do {
  // stuff
 } while (askTheUser()); //askTheUser returns a Boolean.

or even

do {
  // stuff
 } while (getNumber() > n);

(Note, while works the same way.)

No matter what the end result should be a Boolean. Your star printing and user input for number of stars can be separate method calls (and generally it's a good idea to separate logic into method calls to make debugging easier). So...

while (askUserToPlayAgain()) {
    int n = getInput();
    printStars(n);

}

should give you a good idea of how to implement it.

0
Roland Illig On

To repeat some Java code, enclose it in a while loop.

do {
    System.out.println("How large …");
    …
} while (askAgain());

The askAgain is a separate method that should look like this:

public class TriangleFirst {
    Scanner input = new Scanner(System.in);

    private boolean askAgain() {
        System.out.println("Again?");
        return input.nextLine().toLowerCase().startsWith("y");
    }
}

Since you should only ever have a single Scanner for input, remove the SC variable and replace it with input.

0
Brett Duncan On

I'm not entirely sure that I understand your question, but if all you want to do is have the program continue to run once it reaches the end, then all you need to do is insert a while loop.

import java.util.Scanner;
public class TriangleFirst {

    public static void main (String [] args) {
        while(true) {
            //your code goes here
        }
    }
}