Question:
Shapes Java program?
Princess Peach
2006-11-14 04:12:15 UTC
I have written a program in JCreator to create shapes but I need to amend my code using loops and if statements to reduce the number of System.out statements that i have used.

public class Shapes
{
public static void main (String args[])
{

System.out.println("*******");
System.out.println("* *");
System.out.println("* *");
System.out.println("* *");
System.out.println("*******");

System.out.println();

System.out.println("*");
System.out.println("**");
System.out.println("***");
System.out.println("****");
System.out.println("*****");
System.out.println("******");

System.out.println();

System.out.println(" *");
System.out.println(" **");
System.out.println(" ***");
System.out.println(" ****");
System.out.println(" *****");
System.out.println("******");

System.out.println();
Four answers:
Josh Falter
2006-11-14 07:43:09 UTC
I would go with a printShape() function



public class Shapes {

public static void main(String[] args)

{

printShape(new int[] {7,2,2,2,7});

printShape(new int[] {1,2,3,4,5,6});

printShape(new int[] {1,2,3,4,5,6});

}

public static void printShape(int lengths[])

{

for(int i = 0; i < lengths.length; i++)

{

for(int a = 0; a < lengths[i]; a++)

{

System.out.print("*");

}

System.out.println();

}

System.out.println();

}

}
IAPONYA3
2006-11-14 05:06:38 UTC
If I were you I would create methods for each number of asterisks required.



Then do the method calls to draw the shape you need.



Look at code below for example of which draw the first 4 lines of your System.out.println in your code.





Next time do your homework yourself. It will do you better to work it out on your own.







public class Shapes{



void draw8Asterisks( ){

for (int i=0; i<7; i++){

System.out.print("*");

}

System.out.println();

}

void draw2sterisks( ){

for(int i=0;i<2; i++){

System.out.print("*");

}

System.out.println();

}



void draw3terisks( ){

for (int i=0;i<3; i++){

System.out.print("*");

}

System.out.println();

}

/** Creates a new instance of Main */

public Main() {

}



/**

* @param args the command line arguments

*/

public static void main(String[] args) {

Shapes myshape = new Shapes();

myshape.draw8Asterisks();

myshape.draw2sterisks();

myshape.draw2sterisks();

myshape.draw8Asterisks();

// TODO code application logic here

}



}
anonymous
2006-11-14 05:10:45 UTC
this is code for ur second shape:



public class Shapes

{

public static void main (String args[])

{

/*

draws the shape

*

**

***

****

*****



*/

for (int i=1; i<=5; i++){



for (int j=1; j<=i; j++){



System.out.print("*");

}

System.out.println("");

}



}

}



to draw others u might need 3 embedded IF statements
?
2016-10-04 02:57:07 UTC
you prefer yet another loop after that one that is largely the comparable as your first one yet starts off with for (int line = linesToPrint; line < linesToPrint; line--) Oh, and oftentimes its greater desirable to initialize variables utilized in loop counters to 0 and use < fairly than <=. it works the comparable yet its a custom subject and makes it less complicated while making use of arrays and stuff. additionally, if its for an task or something it particularly is going to be marked, readability of code is crucial too so i does no longer replace it to something that makes your code tougher to study if i grew to become into you.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...