2

I want to write a loop to print a double line (===) across the screen, one (=) at a time. I'm still really new to Java and I'm practicing loops right now. I want my result to look something like this:

     =
     ==
     ===
     ====

and so on.

This is what I have so far. . .

    int twoLines = 0;
    while (twoLines < 10)
    { System.out.println("=");
    twoLines = twoLines + 1;
    }

What do I need to do to this in order to add one "=" at a time ?

I know this is super basic to most of you, but I'm still figuring this stuff out. Thank you in advance for any advice.

Victoria22
  • 39
  • 1
  • 2
  • 7

5 Answers5

0

The key idea is to modify the string you want to print after each iteration inside the while loop. This will work:

int twoLines = 0;
String string = "=";
while (twoLines < 10)
{ 
    System.out.println(string);
    string = string + "=";
    twoLines = twoLines + 1;
}
Ling Zhong
  • 1,744
  • 14
  • 24
  • Thank you very much! I realize now that I kept trying to force string and int to work on the same line, instead of declaring them separate when I was playing around with it before getting frustrated. I obviously have a lot more learning to do. Thank you again. – Victoria22 Nov 08 '15 at 22:36
  • @Evette22 if your problem is solved then you must mark the answer as accepted. This helps other people to find what they are looking for. Happy Coding. – Doc Nov 08 '15 at 22:50
0
int twoLines = 1;

while (twoLines <= 10)
{ 
    int i = 1;
    while(i <= twoLines) {
        System.out.println("=");
        i = i + 1;
    }
    System.out.println(""); // next line
    twoLines = twoLines + 1;
}
Mohammed Aouf Zouag
  • 17,042
  • 4
  • 41
  • 67
  • Thank you, can I use this while-loop within a while-loop when writing a program that needs to repeat? I have a program that I'd like to repeat upon the users request, but I haven't quite got it down yet. Would I just place the program within the outer loop? – Victoria22 Nov 08 '15 at 22:45
  • It actually depends on how are you planning to use it. To learn more about "nested loops", you should practice this, many times so that you would get the hang of it. The outer loop specifies the number of lines to print (10) while the second loop prints the required number of '=' per line. – Mohammed Aouf Zouag Nov 08 '15 at 22:47
  • Thank you. I'll defiantly keep playing around with this. I appreciate your help. – Victoria22 Nov 08 '15 at 22:59
0

You can use two loops to achieve this. The inner loop prints the symbol "=" and the outer loop prints a new line.

    int i=0;
    while (i < 10) {
        int j=0;
        while (j < i){
            System.out.print("=");
            j++;
        }
        //print a new line
        System.out.println("\n");
        i++;
    }
AM87
  • 1
  • 1
0

Ok, have you managed to run this code and had a look at what it prints?

Currently your code is just printing "=" once per line, can you see how this statement

System.out.println("=");

never changes? Every time the loop is run, this statement is called, with the same "=" in the print statement.

We need a way to change that print statement, so that each time the loop runs, what it prints is different.

So if we store the "=" in a variable and and print that variable, we can add another "=" to that variable every time the loop runs.

int twoLines = 0;
string output = "="

while (twoLines < 10){

    // first print the output
    System.out.println(output);

    // then change the output variable, so that the next loop
    // prints a longer string
    output += "=";

    // then increment the twoLines variable
    twoLines++;
}

The line; output += "="; is a little short cut for concatenation. Concatenation is the process of adding strings together. By using the '+=' operator, I'm saying that I want to add the string "=" to the end of the string output.

The line; twoLines++; is another little shortcut for incrementing a variable. The '++' operator adds 1 to the variable.

So in short, you just want to look at how you can change the print statement to reflect the change you want to see each time the loop is run.

thomas
  • 23
  • 5
0

I'm not sure if you are looking to print the = characters on a single line, one iteration at a time. If that's the case, then you may not want to use system.out.println (as this prints a new line on every invocation).

First, please note that if the above assumption is true, then this is possibly a duplicate of the following SO question: How can I print to the same line?

That said, perhaps this is a naive solution to what you are looking for:

import java.util.concurrent.TimeUnit;
import java.lang.InterruptedException;

public class OneLine {
    public static void main(String[] args) throws InterruptedException {
        // Prints "="'s to the terminal window, one iteration at a time, 
        // on one line.
        int twoLines = 0;
        while (twoLines < 10) {
            System.out.print("=");
            twoLines = twoLines + 1;
            TimeUnit.MILLISECONDS.sleep(100);
        }
        System.out.println("");
    }
}
Community
  • 1
  • 1
Nadesri
  • 41
  • 4