Enigmatic
Enigmatic

Reputation: 4148

Nested loop to produce specific output

I've just started to learn java and am attempting to produce to following output:

$££$$$££££$$$$$

My current attempt stands as followed:

for (i = 1; i < 3; i++) {
    System.out.print("$£");
    for (j = 1; j < i + 2; ++j) {
        System.out.print("$");

Having had some experience in Python, I'm struggling to get my head around the syntax of nested loops using Java. I receive the following output:

$£$$$£$$$

Upvotes: 2

Views: 497

Answers (5)

Youcef LAIDANI
Youcef LAIDANI

Reputation: 59978

You can use one loop, just If you know that there are many ways to repeat a String in Java, so If you are using Java 8 you can use :

Java 8

int i = 1;
for (i = 1; i < 4; i+=2) {
    System.out.print(String.join("", Collections.nCopies(i, "$")));
    System.out.print(String.join("", Collections.nCopies(i + 1, "£")));
}
System.out.print(String.join("", Collections.nCopies(i, "$")));

Java 11

If you are using Java 11 you can use the String::repeat:

int i = 1;
for (i = 1; i < 4; i+=2) {
    System.out.print("$".repeat(i));
    System.out.print("£".repeat(i + 1));
}
System.out.print("$".repeat(i));

Simple Java code

Or you can create your own repeat method using a loop, your code can be :

public static void main(String[] args) {
    int i = 1;
    for (i = 1; i < 4; i+=2) {
        System.out.print(repeatString("$", i));
        System.out.print(repeatString("£", i));
    }
    System.out.print(repeatString("$", i));
}

public static String repeatString(String s, int nbr) {
    StringBuilder result = new StringBuilder("");
    for (int i = 0; i < nbr; i++) {
        result.append(s);
    }
    return result.toString();
}

Ouputs

$££$$$££££$$$$$

Upvotes: 1

dly
dly

Reputation: 1088

You could do something like this when you want to easily change the characters and/or amount of times they appear:

    char odd = '$';
    char even = '£';
    int amount = 6;

    for (int i = 1; i <= amount; i++) {
        for (int j = 0; j < i; j++) {
            System.out.print(i % 2 == 0 ? even : odd);
        }
    }

Output:

$££$$$££££$$$$$££££££

Upvotes: 1

Ivar
Ivar

Reputation: 6828

You can put the characters you want to print, in an array and when you loop over it you can use the index in combination with the remainder operator to switch between the characters. Then you just need to have a nested loop that repeats the printed character the amount of times the current index is.

final String[] characters = {"£", "$"};
for (int i = 1; i <= 5; i++)
    for (int j = 0; j < i; j++)
        System.out.print(characters[i % characters.length]);

In Java 11 you could even ditch the nested loop and replace it by the String#repeat(int) to repeat the string:

final String[] characters = {"£", "$"};
for (int i = 1; i <= 5; i++)
    System.out.print(characters[i % characters.length].repeat(i));

Upvotes: 1

libik
libik

Reputation: 23029

You should think about it as "how would human do it" and then try to use the same approach in a code.

So what human do:

  • Write 1x $
  • Write 2x £
  • Write 3x $
  • Write 4x £

So the pattern is clear. What you say to human? Hey follow this approach, each time increase the number of same characters printed and switch them each time.

lets do it in code now

let howMuchTimes = 5;
const charA = '$';
const charB = '£';
let actualChar = charA;
let output = '';

for (let i=0; i < howMuchTimes; i++){    
  for (let j=0; j < i+1; j++) {
    output += actualChar;
  }
    
  if (actualChar === charA) {
    actualChar = charB;
  } else {
    actualChar = charA;
  }
}

console.log(output);

(its in javascript so you can execute it right here, but there is no much difference in this code in Java)

Upvotes: 1

Syed Hamza Hassan
Syed Hamza Hassan

Reputation: 710

Updated Your code below.

char temp = '$';
for (int i = 1; i < 6; i++) {
    for (int j = 0; j < i; ++j) {
        System.out.print(temp);
    }
    if (temp == '$') {
        temp = '£';
    }
    else {
        temp = '$';
    }
}

Try this, I have updated your code. Feel free to ask if anything is not clear.

Output:

$££$$$££££$$$$$

Try Here

Upvotes: 2

Related Questions