Reputation: 1
public class Printing {
public static void printStars(int amount) {
int i = 1;
while (i<amount) {
System.out.print("*");
i++;
if (i==amount) {
System.out.println("*");
}
}
}
public static void printTriangle(int size) {
int c = 1;
while (c<=size) {
printStars(c);
c++;
}
}
public static void main(String[] args) {
printStars(3);
System.out.println("\n---");
printSquare(4);
System.out.println("\n---");
printRectangle(5, 6);
System.out.println("\n---");
printTriangle(3);
System.out.println("\n---");
}
}
I'm currently following an online course on learning Java and in this assignment, printTriangle(3) should print 3 rows of stars, first row with 1 star, second row with 2 stars, third row with 3 stars.
I can't figure out why it only prints 2 rows of stars, first row with 2 stars and second row with 3 stars.
I edited out the parts where I defined methods printSquare and printRectangle because I figured they weren't important.
The program I'm using to code is Netbeans with TMC 1.1.7
Upvotes: 0
Views: 713
Reputation: 46
I choose nested for loop
to implement. The inner loop is to print row. Outer loop is to change line.
public static void printStars(int amount) {
for (int i = 1; i <= amount; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("*"); //to print row
}
System.out.println(""); //to change line
}
}
Upvotes: 0
Reputation: 321
Maybe my code is simple, but I can show the same result with these piece of logic.
public static void main(String[] args)
{
int i = 0; int o = 0;//initialize variables
while (i<3)//first counter -- line by line
{
while (o<i)//second counter -- describes how many @ sign
//(or in your case, use asterisk (*))
//should be printed on the same line
{
System.out.print("@");
o++;
}
o=0;
System.out.println("@");
i++;
}
}
The output looks like:
@
@@
@@@
Hope it helps!
Happy Coding!
Upvotes: 1
Reputation: 1677
Whenever the count is equal to amount we need to change to new line so that next time you use your method it starts printing stars in the next line or you can change to always insert a new line when you go into the method the first time.
Change your method to this:-
public static void printStars(int amount) {
int i = 1;
while (i<=amount) {
System.out.print("*");
if (i==amount) {
System.out.println("");
}
i++;
}
}
Upvotes: 0
Reputation: 593
Another option is to use the for
loop.
public static void printStars(int amount) {
for (int i = 1; i <= amount; i++) {
System.out.print("*");
}
System.out.println("");
}
Upvotes: 0
Reputation: 91
change your while loop to
while (i<=amount) {
if (i==amount) {
System.out.println("*");
}else{
System.out.print("*");
}
i++;
}
in print stars method
Upvotes: 0
Reputation:
Get rid of the if loop
and change the condition in the while loop
to i<=amount
. What's happening now in your while loop is, you are checking if i
is less than amount
( i < amount )
. In the first iteration you will call printStars(1) so in this case amount = 1
and i = 1
. In the while loop the comparison becomes 1<1
which is false
. The if condition
(i==amount)
is not even reached because the outer while loop itself broke, the method execution ends without printing 1 star as expected. If you change the while condition to i<=amount
then the comparison is 1<=1
the condition is true
so it will print 1 star.
public static void printStars(int amount) {
int i = 1;
while (i<=amount) {
System.out.print("*");
i++;
}
System.out.println("");
}
Upvotes: 1