Given the jagged Array, we are asked to use a looping statement to display the character based on the position. Display a "*" if the position matched or a " " if it doesn't.
int arr [][] = {{0,4,8,12,13,14,15,18,19,20,21,24,28},
{0,4,7,9,12,16,18,22,25,27},
{0,1,2,3,4,6,10,12,16,18,22,26},
{0,4,6,10,12,13,14,15,18,19,20,21,26},
{0,4,6,7,8,9,10,12,18,26},
{0,4,6,10,12,18,26}};
I have created a program, but the output is not what I expected and I am now stuck.
for (int i = 0; i < arr.length; i++)
{
for (int j = 0; j < arr[i].length - 1; j++)
{
for (int spaces = 1; spaces < arr[i][j + 1]-arr[i][j]; spaces++)
{
System.out.print(" ");
}
System.out.print("*");
}
System.out.println();
}
The output was suppose to be Happy but I get:
enter image description here
It's because of your program does not compare the first j value (which is 0) with itself. Since every value equals itself you can add manually * for each line like this.
int arr [][] = {{0,4,8,12,13,14,15,18,19,20,21,24,28},
{0,4,7,9,12,16,18,22,25,27},
{0,1,2,3,4,6,10,12,16,18,22,26},
{0,4,6,10,12,13,14,15,18,19,20,21,26},
{0,4,6,7,8,9,10,12,18,26},
{0,4,6,10,12,18,26}};
for (int i = 0; i < arr.length; i++)
{
System.out.print("*");
for (int j = 0; j < arr[i].length - 1; j++)
{
for (int spaces = 1; spaces < arr[i][j + 1]-arr[i][j]; spaces++)
{
System.out.print(" ");
}
System.out.print("*");
}
System.out.println();
}
Try this.
for (int i = 0; i < arr.length; i++) {
for (int j = 0, p = -1; j < arr[i].length; p = arr[i][j++])
System.out.print(" ".repeat(arr[i][j] - p - 1) + "*");
System.out.println();
}
output:
* * * **** **** * *
* * * * * * * * * *
***** * * * * * * *
* * * * **** **** *
* * ***** * * *
* * * * * * *
I'm trying to write a square shape of asterisk around border using the https://stackoverflow.com/a/34209565 answer.
But I cannot get it working at all.
Here is the code I'm trying.
int _i = 10;
int _j = 10;
String[][] array = new String[_i][_j];
for (int i = 0; i < _i; i++) {
System.out.println();
for (int j = 0; j < _j; j++) {
if(i==0 || j == 0 || i == _i - 1|| j == _j - 1){
array[i][j] = "*";
System.out.print(array[i][j]);
}
}
}
The output I'm getting is:
**********
**
**
**
**
**
**
**
**
**********
I tried running tthe code from the answer but it produces a one line of asterisk. Something from the code in the answer has been omitted.
The code provdied in the answer is:
int _i = 10;
int _j = 10;
String[][] array = new String[_i][_j];
for (int i = 0; i < _i; i++) {
for (int j = 0; j < _j; j++) {
if(i==0 || j == 0 || i == _i-1|| j == _j-1){
array[i][j] = "*";
}
}
}
And the output in the answer is:
**********
* *
* *
* *
* *
* *
* *
* *
* *
**********
You need an else to print a space when the entry is not a star. Also, I would move the println() to the end of the loop (instead of the beginning). Assuming you actually want to fill the array too, populate it with a space as well. Like,
for (int j = 0; j < _j; j++) {
if (i == 0 || j == 0 || i == _i - 1 || j == _j - 1) {
array[i][j] = "*";
} else {
array[i][j] = " ";
}
System.out.print(array[i][j]);
}
System.out.println();
Outputs
**********
* *
* *
* *
* *
* *
* *
* *
* *
**********
You can use the repeat method, so the code is less verbose, also enclosing the generation in a method you can make it dynamic bu setting two arguments: number of lines and number of columns:
class Test
{
public static void main(String[] args)
{
System.out.println(createBox(10, 10));
System.out.println(createBox(10, 5));
}
public static String createBox(int qtaRig, int qtaCol)
{
String result = "";
for(int r = 1; r <= qtaRig; r++)
{
if(r == 1 || r == qtaRig)
{
result += "*".repeat(qtaCol);
}
else
{
result += "*" + " ".repeat(qtaCol-2) + "*";
}
result += "\n";
}
return result;
}
}
Result:
**********
* *
* *
* *
* *
* *
* *
* *
* *
**********
*****
* *
* *
* *
* *
* *
* *
* *
* *
*****
I am supposed to complete the inside of a method
public static void printXinSquare(int width) {
}
width being the number of rows so that it would create
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
I've tried making four separate pieces of triangles, and attach them somehow, but that didn't work.
I also tried to create a for loop for spaces and then add a for loop for the stars, but I'm confused and have no idea as to how to do that.
To do this, as I said before, I tried to make separate trianges.
public static void printXinSquare(int width) {
for (int i = 1; i <= width/2+1; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
for (int i = width/2+2; i <= width; i++) {
for (int j = width+1-i; j >= 1; j--) {
System.out.print("*");
}
System.out.println();
}
System.out.println();
}
This creates a triangle pointing to the right.
public static void printXinSquare(int width) {
for (int line = width/2+1; line >1; line--) {
for (int i = 1; i <= (line - 1); i++) {
System.out.print(" ");
}
for (int i = 1; i <= (width - 2 * line); i++) {
System.out.print("*");
}
System.out.println();
}
This creates a triangle pointing upwards.
This is what I have tried so far, but I don't think this way works.
I think I should create for loops that take into account the spaces,
but I don't know how to do that as the spaces are in diagonal direction.
Any help to complete this method would be greatly appreciated :)
This should print in with an input width of 10 the exact picture you have in your question.
public static void printXinSquare(int width)
{
for (int k = 0; k < width; k++) {
for (int j = 0; j < width; j++) {
if (k == j || k == width - j - 1) {
System.out.print(" ");
}
else {
System.out.print("* ");
}
}
System.out.println();
}
}
It prints * with a blank space every time to ensure it is properly spaced out like that picture, and prints 2 spaces instead in two different cases, each of which account for a single diagonal line. The two cases are as follows:
if the row count is equal to the current column count, there should be a space instead of an asterisk. This will create the first diagonal line going from top left to bottom right.
if the row count is equal to the width minus the current column count minus 1, it also should be a space rather an asterisk. This will create the second diagonal line going the opposite direction.
Try something like this:
for (int i = 0; i < width; i++) {
for (int j = 0; j < width; j++) {
if (i == j || i+j == width) {
System.out.print(" ");
} else {
System.out.print("*");
}
}
System.out.println("");
}
In this case you would be printing '*' when it is not in the triangle, and ' ' otherwise. The inside triangle is the condition (i==j || i+j==width)
I'm trying to print a pyramid of stars using nested while loops. I know I am able to achieve this using for loops but I want to do it with while loop instead. This is my code so far:
public class WhileNest
{
public static void main(String[]args)
{
int rows = 5, i = 1, j = 1;
while(i <= rows)
{
while(j <= i)
{
System.out.print("*");
j++;
}
System.out.print("\n");
i++;
}
}
}
The output has to be like this:
*
**
***
****
*****
But my output is this:
*
*
*
*
*
Any help is appreciated, thanks.
you have to reset j like this:
public class test {
public static void main(String[] args) {
int rows = 5, i = 1, j = 1;
while (i <= rows) {
while (j <= i) {
System.out.print("*");
j++;
}
System.out.print("\n");
i++;
j = 1;
}
}
}
You have forgotten to assign 1 to j at the end of the outer while loop.
public class WhileNest {
public static void main(String[] args) {
int rows = 5, i = 1, j = 1;
while (i <= rows) {
while (j <= i) {
System.out.print("*");
j++;
}
System.out.print("\n");
i++;
j = 1;
}
}
}
int rows = 5, i = 1, j = 1;
while(i <= rows)
{
while(j <= i)
{
System.out.print("*");
j++;
}
j=1;
System.out.println();
i++;
}
You are not reassigning j to start from beginning. Your i and j were always staying same. Re-initialize j to 1 after the inner while loop as shown. It will work
Pyramid using two for loops:
String STAR = "*";
String SPACE = " ";
int SIZE = 10;
for(int i=0;i<SIZE;i++) {
int start = SIZE-i;
int end = (SIZE*2) - SIZE + i;
for(int j = 0; j<SIZE*2; j++) {
if(j>=start && j<=end && j%2 == i%2) {
System.out.print(STAR);
} else {
System.out.print(SPACE);
}
}
System.out.println();
}
output:
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * *
public static void main(String[] args) {
for(int i=0;i<10;i++){
for(int k=0;k<i;k++){
System.out.print("*");
}
System.out.println();
}
}
*Instead of initializing "j" at the beginning, include it in the first while loop that'll do the work.( * will be printed at the beginning of each row)
public class WhileNest
{
public static void main(String[]args)
{
int rows = 5, i = 1;
while(i <= rows)
{
int j = 1;
while(j <= i)
{
System.out.print("*");
j++;
}
System.out.print("\n");
i++;
}
}
}
Hi there i want to create a triangle using java loops. But what i need is only the borders. I've tried using this syntax but it always shows error. any advice?
int lines = 5;
int c = 2*lines;
for (int i = lines-1; i>=0; i--)
{
for (int j = i; j < lines; j++)
{
System.out.print(" ");
}
for (int k = 1; k <= c; k++)
{
if (k % 2 == 0)
{
System.out.print(" ");
}
else
{
if (k == 0 || k == lines - 1) {
System.out.print("*");
}
}
}
System.out.print("\n");
c -= 2;
}
can you kindly help me . thanks
There's a lot of things wrong with your code...
for (int k = 1; k <= c; k++)
// and then:
if (k == 0 || k == lines - 1) {
k will never be 0
if (k % 2 == 0)
// and then
else
{
if (k == 0 || k == lines - 1) {
a even if k was 0, then 0%2 == 0, so k==0 can never occur
As of right now, your app only prints spaces.
If you want to draw a triangle, the (possibly) simplest way would be to do
public static void main(String[] args) {
int lines = 6;
int cols = lines+1;
for (int line =0; line < lines-1; line++) {
for (int col = 0; col<cols; col++) {
if (col == 0 || col == line) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
for (int col=0; col<cols; col++) {
System.out.print("*");
}
System.out.println();
}
Triangle.java
public class Triangle {
private int lines;
public Triangle(int lines) {
this.lines = lines;
display();
}
public void display() {
int spaces = lines - 1;
int last;
int min = 1;
int max;
for (int i = 0; i < lines; i++) {
last = 2 * i + 1;
max = spaces + last;
for (int j = min; j <= max; j++) {
if (j == spaces + 1 || j == max || i == lines - 1)
System.out.print('*');
else
System.out.print(' ');
}
System.out.println();
spaces--;
}
}
public static void main(String[] args) {
new Triangle(3);
new Triangle(4);
new Triangle(5);
new Triangle(6);
}
}
Output
*
* *
*****
*
* *
* *
*******
*
* *
* *
* *
*********
*
* *
* *
* *
* *
***********
Ex:
*
* *
* *
* *
*********
Code:
int numberOfRows = 5;
for(int i=0; i<numberOfRows; i++)
{
for(int a=0; a<=numberOfRows; a++)
{
int x = numberOfRows - i;
if (a > x) {
if (x==(a-1) || i==(numberOfRows-1)) {
System.out.print("*");
} else {
System.out.print(" ");
}
} else {
System.out.print(" ");
}
}
for(int b=0; b<=i; b++)
{
if(b==i || i==(numberOfRows-1)) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.print("\n");
}
I have used two methods and repeated those two methods using for loop.
public class OpenWord {
int i;
String k = " ";
public void space(){
System.out.println("*"+k+"*");
}
public void star(){
System.out.println("*");
}
public void trainagle(){
star();
for ( i=1; i<=10;i++){
space();
k =k+" ";
if(i==10){
k=k.replace(" ", "*");
space();
}
}
}
public static void main(String args[]) {
OpenWord a = new OpenWord();
a.trainagle();
}
}
Output:
*
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
******************