Patterns using loop in java or blueJ
How to print the follwoing pattern in JAVA
1. *
* * *
* * * * *
* * * * * * *
2. *
* *
* *
* * * * * * *
3. *
* *
* * *
* * * *
* * * * *
4. B L U E J
B L U E
B L U
开发者_运维问答 B L
B
5. B
L B
U L B
E U L B
J E U L B
For the triangular patterns, you can visualize them as a grid, like so:
0 1 2 3 4 5 6 7 8 9 ...
[ ][ ][ ][ ][ ][ ][*]
[ ][ ][ ][ ][*][ ][*][ ][*]
[ ][ ][*][ ][*][ ][*][ ][*][ ][*]
[*][ ][*][ ][*][ ][*][ ][*][ ][*][ ][*]
Notice there is a pattern on the positions that will have an space or an asterisk. Translate that into code.
On the last two, you can easily solve that using String.substring()
, but chances are you won't be able to use that.
In that case, think of each new line as adding a new character to the previous one. There are several ways to know which character you should add next; one way would be using a string like BLUEJ
and get a new character every iteration, which can be done using String.charAt()
You'll need two for loops, an outer loop for the rows, an inner loop for the columns. Next, you need a function, that takes a row and column value and calculates from those values whether you have to plot a star or not. The basic alogrithm goes like this (for the stars):
Plotter plotter = new FilledTreePlotter();
for (row = 0; row < maxRows; row++) {
for (column = 0; column < maxColumns; column++ {
System.out.print(plotter.getCellContent(row,column));
}
System.out.print("\n");
}
I've added some OO falvour to the basic algorithm. You'll need an additional interface
public interface Plotter {
public char getCellContent(int row, int column);
}
and now you can implement a class that, for instance, can plot the filled triangle:
public FilledTreePlotter implements Plotter {
public char getCellContent(int row, int column) {
if ( ... ) {
// your magic says, that this is a star
return ('*');
} else {
return (' ');
}
}
}
Here is a demo implementation for another task, which will just plot a diagonal line:
public DiagonalPlotter implements Plotter {
public char getCellContent(int row, int column) {
if ( row == column ) {
return ('*');
} else {
return (' ');
}
}
}
You need to study the patterns and try to work out how the number of spaces and stars changes with each line.
Let's look at the first one:
* (line 0)
*** (line 1)
***** (line 2)
******* (line 3)
line 0: 3 spaces, 1 star
line 1: 2 spaces, 3 stars
line 2: 1 space , 5 stars
line 3: 0 spaces, 7 stars
Can you spot any patterns?
With each line, the number of spaces decrease by 1 and the number of stars increase by 2.
You can also see that for each line the:
line number + number of spaces = 3
and
number of stars = (2 * line number) + 1
This tells us how the number of spaces and stars in each line varies with line number. We can code it up like this:
int numLines = 4 ;
for(int i = 0 ; i < numLines ; i++){
//print spaces
for(int j = 0 ; j < 3-i ; j++){
System.out.print(' ');
}
//print stars
for(int j = 0 ; j < 2*i + 1; j++ ){
System.out.print('*');
}
//print a new line
System.out.println();
}
See if you can work out the rest yourself.
you could print the fourth pattern using nested for lop in the following way (this is just the logic part):
String str="BLUEJ";
int i,j;
char c;
for(i=5-1;i>=0;i--)
{
for(j=0;j<=i;j++)
{
c=charAt(j);
System.out.print(c+" ");
}
System.out.println();
}
}
}
the charAt(j) is a function used for strings, using which we can take out any character of the given string from the specified position(j).
you could print the fifth pattern in the following way:
int i,j;
char p;
String st="BLUEJ";
for(i=0;i<5;i++)
{
for(j=i;j>=0;j--)
{
p=st.charAt(j);
System.out.print(p+" ");
}
System.out.println();
}
hope the solution was helpful.
ALL THE BEST!
For Question 4
/**
* Write a description of class idkk here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class idkk
{
public static void main()
{
String word="BLUEJ";
int lth=word.length();
System.out.println(word);
System.out.println("BLUE");
System.out.println("BLU");
System.out.println("BL");
System.out.println("B");
}
}
精彩评论