I want to write the method PrintIsFun (int n), which prints the word Java n times. If n is negative, let the method print Error: negative n
My current code only works good for 1, if number is 2 it all becomes messy:
public static void main(String[] args){
PrintIsFun(2);
}
public static void PrintIsFun(int n) {
if (n < 0) {
System.out.println("Error: negative n");
} else {
for (int i = 0; i < n; i ) {
System.out.print(" J a v v a ");
System.out.print(" J a a v v a a ");
System.out.print("J J aaaaa V V aaaaa ");
System.out.print(" JJ a a V a a ");
}
}
}
Desired output if my n for example is 2
CodePudding user response:
The solution. For loop for each row and few spaces in print statement and voila.
public static void PrintIsFun(int n) {
if (n < 0) {
System.out.println("Error: negative n");
}
else {
//first row
for (int i = 0; i < n; i ) {
System.out.print(" J a v v a ");
}
System.out.println();
//second row
for (int i = 0; i < n; i ) {
System.out.print(" J a a v v a a ");
}
System.out.println();
//three row
for (int i = 0; i < n; i ) {
System.out.print(" J J aaaaa V V aaaaa ");
}
System.out.println();
//fourth row
for (int i = 0; i < n; i ) {
System.out.print(" JJ a a V a a");
}
System.out.println();
}
}
CodePudding user response:
to print your desired output. I think you have to change your code like this below.
public static void PrintIsFun(int n) {
if (n < 0) {
System.out.println("Error: negative n");
} else {
for (int i = 0; i < n; i ) {
System.out.print(" J a v v a ");
}
System.out.println();
for (int i = 0; i < n; i ) {
System.out.print(" J a a v v a a ");
}
System.out.println();
for (int i = 0; i < n; i ) {
System.out.print("J J aaaaa V V aaaaa ");
}
System.out.println();
for (int i = 0; i < n; i ) {
System.out.print(" JJ a a V a a ");
}
System.out.println();
}
it seems code isn't good which I wrote. Try to make your own code
CodePudding user response:
If you want to print the ASCII-art on the same level you should use a for
cycle on every print: by doing this way, at the second iteration of the cycle you will print on the 5th line, you can't come back to the 1st