Creating Shapes with for loops in Java [closed] - java

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I'm a newbie to Java and I need to print out shapes using for loops.
1)
*
***
*****
***
*
2)
*****
****
***
**
*
*
**
***
****
******
I really would appreciate your help!
Thanks in advance!

To do this, first you need to be able to describe the pattern.
If you can reliably draw the "next" shape for the integer input, then you know the pattern.
Then you think of which parts of the pattern are counted items (3 stars, then a blank, for example). Once you identify the counted items, you make a relationship between that count and the number one typed in.
Finally, for each counted item you write a for loop that, within it's block, contains code to output one item. The control structure of the for loop should make sure that the count is exercised the correct number of times.
For each item where the count is 1, you may omit the for loop and simple print the item out directly.
--- Examples --
*
***
*****
*******
*********
***********
Thought process:
The first line is 5 spaces then a star.
The second line is 4 spaces then 3 stars.
The third line is 3 spaces then 5 starts.
I see a pattern, the first line is (5 spaces - the line number)
for (int spaces = 0; spaces < 5 - line_number; spaces++) {
print(" ");
}
That other pattern, was (counting stars) 1, 3, 5, 7, 9.
The pattern is one of differences 3-1 = 2, 5-3 = 2, 7-5=2, etc.
I need to start with one star, and add two stars per line.
for (int stars = 1; stars < 1 + 2 * line_number; stars++) {
print("*");
}
Now I need a loop for the lines
for (int line_number = 0; line_number < 5; line_number++) {
print("\n");
}
Now I'll put it all together
for (int line_number = 0; line_number < 5; line_number++) {
for (int spaces = 0; spaces < 5 - line_number; spaces++) {
print(" ");
}
for (int stars = 1; stars < 1 + 2 * line_number; stars++) {
print("*");
}
print("\n");
}

Related

Can inner and outer loops interrupt and resume one after another?

I have an existing loop that draws the labels and textboxes in two horizontal lines (Line 1 as Label, Line 2 as textboxes). At that time, the loop was based on the definite requirement that there will not be more than 12 elements of each type. This was written using two loops (one loop representing a label, another as a text box):
Draw labels
for (int i = 2, i<hdrlabel.length; i++){
coldef.add(new ColDefType("",hdrLabel[i],"varchar",false,15,"HDR"+String.valueOf(i),1,i==2?"%-25s":"",6,"LABEL",true,""));
}
Draw Text Boxes
LinkedHashmap<String,String> row = dbVal.get(i)
for (int j = 1, i<hdrlabel.length-1; i++){
coldef.add(new ColDefType(j==1?row.get(hdrLabel[j]:"",row.get(hdrLabel[j+1],"decimal5",false,15,row.get(hdrLabel[0])+String.valueOf(j),i+2),j==1?"%-25s":"",6,"TXTFLD",true,""));
}
Now, as to take account of number of days, the number of elements (in hdrLabel.length-2) is now increased to a max of 31 for each component type. Due to spacing issues for mobile and tablet viewing, it was determined that it's best if we draw up to 12 elements per line. What I am looking for is that if the number of elements of each type is more than 12, it should be drawn like:
Line 1: Labels 1-12
Line 2: Text Box 1-12
Line 3: Labels 13-24
Line 4: Text Box 13-24
Line 5: Labels 25-31
Line 6: Text Box 25-31
If the number of elements is between 15 to 24, boxes and labels in lines 5 and 6 are not required to be drawn.
Is there any way to use only two loops where we pause one loop when either reaches the 12 / 24 element, run the other loop and then resume the former loop?
I could not find a much more leaner way to do this, as the closest I could get is to break it to several for loops, but it's definitely not efficient if given of the dynamic number of max elements:
Line 1 - (for i=2, i<14, i++) - break at 13
Line 2 - (for j=1, j<13,j++) - break at 12
Line 3 - (for i=14, i<26, i++) - break at 26
Line 4 - (for j=13, i<25, i++) - break at 25
Line 5 - (for i=26, i<hdrLabel.length, i++) - break at 31
Line 6 - (for j=25, i<hdrLabel.length-1, i++) - break at 31
Not sure if this is what you want since it used 3 loops, but at least it works (assuming you want loop 1 to always happen first)
int i_start = 2;
int j_start = 1;
int i_terminate = 32;
int j_terminate = 32;
int next_i, next_j;
while (true) {
next_i = Math.min(i_start + 12, i_terminate);
next_j = Math.min(j_start + 12, j_terminate);
for (int i = i_start; i < next_i; ++i) {
// do something
}
for (int j = j_start; j < next_j; ++j) {
// do something
}
i_start = next_i;
j_start = next_j;
if (i_start == i_terminate && j_start == j_terminate) {
break;
}
}

How to iterate through an ArrayList of integer? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I have an ArrayList of Integer of {1, 3, 4, 5 , 6}
how do I iterate through the ArrayList so that i can print out the values in this manner in each loop?
1 and 3
1 and 4
1 and 5
1 and 6
3 and 4
3 and 5
3 and 6
4 and 5
4 and 6
5 and 6
I have tried doing a foreach loop, followed by a for loop and remove one object after the next but I encounter the ConcurrentModificationException. any help will be much appreciated!
I think this is what you want
- Iterate and remove first element
To avoid ConcurrentModificationException while iterating and removing element use standard old-fashioned for loops instead of foreach.
do
{
for (int j = 1; j < list.size(); j++)
{
System.out.println(list.get(0) + " and " + list.get(j));
}
list.remove(0);
}
while (list.size() != 0);
This code should work:
for (int i = 0; i < list.size() - 1; i++) {
for (int j = i + 1; j < list.size(); j++) {
System.out.println(list.get(i) + " and " + list.get(j));
}
}

Java printing patterns [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'm new in Java.
I have next code:
public static void main(String[] args) {
for(int k = 10; k > 0; k--)
{
for(int l=0; l < k-1; l++)
{
System.out.print(' ');
}
for(int n=10; n > k-1; n--)
{
System.out.print('*');
}
System.out.println();
}
}
It prints this:
*
**
***
****
*****
******
*******
********
*********
**********
But I want to print it with empty inside like this:
*
**
* *
* *
* *
* *
* *
* *
* *
**********
Can anyone to explane me how to do it.
I understand that this is not a place where solve homework tasks. But can somebody tell me the algorithm for solving the problem in words.
I do not need a ready solution because I want to understand and solve it by myself. So how I can put spaces inside?
Hint: In the second loop you should print a '*' for the first and last values of n, not all of them. For the other positions, print a ' '.
Hint: in the second inner for loop, you could have an if statement where you check for n's greatest and lowest values, and k's lowest value, then print a '*', otherwise print a ' '. Like so:
for(int n = 10; n > k - 1; n--){
if(n == 10 || n == k || k == 1){
System.out.print('*');
} else {
System.out.print(' ');
}
}

Jaya create a star pattern [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I tried to made a star pattern but I did not know how to do it, I know how to made start pattern when the start or the spaces are continuous, but when it is a start and spaces like the pattern that I show below, I do not how to do it.
* *
* *
*
* *
* *
You need to find the relationship between the corners of the cross/star. Take this star for example of size 5.
0 1 2 3 4
0 * *
1 * *
2 *
3 * *
4 * *
In a cross in the diagonal from (0,0) to (4,4), the indices are the same (in the code this means row == col).
Also, you can notice that in the diagonal from (0,4) to (4,0) indices always sum up to 4, which is size - 1 (in the code this is row + col == size - 1).
Therefore in the code, you will need to loop through rows and then through columns. Each time you have to check if the above conditions are fulfilled.
Code:
class Main {
public static void main(String[] args) {
printCross(5); // Vertical size of cross
}
public static void printCross(int size) {
for (int row = 0; row < size; row++) {
for (int col = 0; col < size; col++) {
if (row == col || row + col == size - 1) {
System.out.print('*');
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
}

Java Graphical design loops

for(int i = 1; i < 12; i++)
{
for(int k=11; k > i; k--)
{
System.out.print("*");
}
System.out.print("\n");
}
I have the code above which displays a design like this:
**********
*********
********
*******
******
*****
****
***
**
*
I am wanting to swap it so that it looks like this:
**********
*********
********
*******
******
*****
****
***
**
*
I know that I need to use a loop and System.out.print(" ") in some way to leave spaces.
Whats the best approach to use? I created two separate loops, but with the next line commands this wont work in two loops. How would I integrate that into one loop?
For the second "picture": you'll have to print a variable number of spaces before the "*". That can be accomplished by using another loop before the loop that prints the "*", and knowing that every time you print a line, the number of spaces printed is incremented by one, and the number of asterisks is decremented by one.
EDIT :
Here's a hint, to get you started. Fill-in the blanks (and remove the comments):
int delta = /*fill*/;
for (int i = 0; i < /*fill*/; i++) {
for (int j = 0; j < /*fill*/; j++) {
System.out.print(" ");
}
for (int j = 0; j < /*fill*/; j++) {
System.out.print("*");
}
delta += /*fill*/;
System.out.print("\n");
}
Use String.format to left pad your string with spaces.
Do it using a 2-D array!!! Its very simple and efficient that way!!
For the second result:- Put '*' in places if row_no <= column_no (the half matrix above diagonal plus the diagonal elements) else put a space " " in other places. Run this in loops for row and column. Then print the 2-D array row wise.

Categories

Resources