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 am trying to make this pattern in java:
* * * * * * * *
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
This is the code which I have written:
package practise;
import java.util.Scanner;
public class PatternsUsingLoop {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for(int j=1;j<=n;j++) {
for(int k=n;k>=1;k--) {
System.out.print("* ");
}
System.out.println();
}
}
}
please tell me the error
There are many ways to do it also to fix this issue. One of the simplest fixes is to replace the inner loop with the following:
for (int k = (n - j) + 1; k >= 1; k--)
Since you want to print n number of *s in the first line, n - 1 number of *s in the second line and so on, the value of k must be initialized to n when j is 1, n - 1 when j is 2 and so on.
Some of the other ways to do this requirement are as follows:
A.
for (int j = n; j >= 1; j--) {
for (int k = j; k >= 1; k--) {
System.out.print("* ");
}
System.out.println();
}
B.
for (int j = n; j >= 1; j--) {
for (int k = 1; k <= j; k++) {
System.out.print("* ");
}
System.out.println();
}
...and many more
You don't decrease the finish condition in inner loop. This will works:
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for(int row=0;row<n;row++) {
for (int column = 0; column < n-row; column++) {
System.out.print("* ");
}
System.out.println();
}
The problem is that you haven't tried debugging your code.
To debug your code try printing out the variables that you are using at the beginning of each line to see if they are what you expect. You have (hopefully) thought about the code, and what you think each variable will be on each line, so you've missed something, you have a bug.
For example try this:
package practise;
import java.util.Scanner;
public class PatternsUsingLoop {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for(int j=1;j<=n;j++) {
System.out.print("j: " + j + " n: " + n);
for(int k=n;k>=1;k--) {
System.out.print("* ");
}
System.out.println();
}
}
}
When you run that you get
j: 1 n: 5* * * * *
j: 2 n: 5* * * * *
j: 3 n: 5* * * * *
j: 4 n: 5* * * * *
j: 5 n: 5* * * * *
Which shows what a couple of the other answers are pointing out, that n doesn't change so the output is always 5 stars.
Now you can figure out the answer. (Hint: start j from 0 and use k=n-j)
Its a very small mistake that you are doing , the first loop is correct but in the inner loop you are printing n stars again (from n to 1).
You need to print (n - i + 1) stars for every loop.
Use this change :
...
...
int n = sc.nextInt();
for(int j=1;j<=n;j++) {
for(int k=n-j+1;k>=1;k--) {
System.out.print("* ");
}
System.out.println();
}
...
...
//... means same code //
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 have to make a loop that will read the user's input (let's say his input is 5) so the output should be as the following:-
*
**
***
****
*****
****
***
**
*
int x;
//input size of triangle from 1-20
Scanner scanner = new Scanner (System.in);
System.out.println("Enter size of triangle from 1 to 20: ");
x = scanner.nextInt ();
for( int i = 0; i <= x; i++){
for(int j=0; j < i; j++){
System.out.print( " *");
}
System.out.println(" ");
I just don't know how to finish it by decreasing the pattern so I can form a full triangle.
example using ternary operator .increase j if x grater than i decrease otherwise
int x = 5, j=0;
for (int i = 0; i <= x*2; i++) {
j= (x>i)? ++j:--j; // u can use if else also
for (int y = 0; y < j; y++) {
System.out.print(" *");
}
System.out.println("");
}
output>>
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
The following code prints the triangle, assuming the user enters 10
for (int i = 0; i < 10; i++){
for (int j = 0;j < i;j++ )
System.out.print('x');
System.out.println();
}
for (int i = 10; i > 0; i--){
for (int j = 0; j < i;j++)
System.out.print('x');
System.out.println();
}
In your code, you exclude the second for loop in which i is decremented.
import java.util.Scanner;
public class Triangle {
public static void printLine(int n) {
for(int i = 0; i < n; i++) System.out.print("*");
System.out.print("\n");
}
public static void main(String[] args) {
Scanner scanner = new Scanner (System.in);
System.out.println("Enter size of triangle");
int x = scanner.nextInt ();
for(int i = 0; i < x; i++) {
printLine(i);
}
for(int i = x; i > 0; i--) {
printLine(i);
}
}
}
The output :
Enter size of triangle
5
*
**
***
****
*****
****
***
**
*
I got a problem to create a rhombus, my code here:
package random;
public class asd {
public static void main(String args[]) {
for (int j = 1; j <= 4; j++) {
for (int kong = 4 - j; kong >= 1; kong--) {
System.out.print(" ");
}
for (int xing = 1; xing <= 2 * j - 1; xing++) {
System.out.print("*");
}
System.out.println();
}
for (int a = 1; a <= 3; a++) {
for (int b = 1; b <= a; b++) {
System.out.print(" ");
}
for (int c = 5; c >= 1; c -= 2) { // <==== here
System.out.print("*");
}
System.out.println();
}
}
}
However, the output is:
*
***
*****
*******
***
***
***
I think the problem is in the code which I highlighted.
java-11
By using String#repeat, introduced as part of Java-11, you can do it with a single loop.
public class Main {
public static void main(String[] args) {
int size = 9;
int midRowNum = size / 2 + 1;
for (int i = 1 - midRowNum; i < midRowNum; i++) {
System.out.println(" ".repeat(Math.abs(i)) + "*".repeat((midRowNum - Math.abs(i)) * 2 - 1));
}
}
}
Output:
*
***
*****
*******
*********
*******
*****
***
*
By increasing the amount of space by one character, you can also print a variant of the shape:
public class Main {
public static void main(String[] args) {
int size = 9;
int midRowNum = size / 2 + 1;
for (int i = 1 - midRowNum; i < midRowNum; i++) {
System.out.println(" ".repeat(Math.abs(i)) + "* ".repeat((midRowNum - Math.abs(i)) * 2 - 1));
}
}
}
Output:
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
You are right in indicating the possible problematic line. Surprised that you did it right in first half:
for (int c = 5; c >= 2 * a - 1; c -= 1) { // <==== here
System.out.print("*");
Using Math.abs will make it a lot easier:
import java.util.Scanner;
public class MakeDiamond {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("Let's Creat Diamonds");
System.out.println("If number increases Diamonds gets bigger. " +
"Please input number lager than 1 : ");
int user_input = sc.nextInt(); //gets user's input
System.out.println("");
int x = user_input;
int front_space = -5;
for (int i = 0; i < 2 * user_input + 1; i++) {
for (int a = front_space; a < Math.abs(i - user_input); a++) {
System.out.print(" ");
}
if (i < user_input + 1) {
for (int b = 0; b < 2 * i + 1; b++) {
System.out.print("* ");
}
} else if (i > user_input) {
for (int c = 0; c < 2 * x - 1; c++) {
System.out.print("* ");
}
x--;
}
System.out.print('\n');
}
System.out.println("\nRun Again? 1 = Run, 2 = Exit : ");
int restart = sc.nextInt();
System.out.println("");
if (restart == 2) {
System.out.println("Exit the Program.");
System.exit(0);
sc.close();
}
}
}
}
You can simplify your code by using two nested for loops and one if else statement.
int n = 4;
for (int i = -n; i <= n; i++) {
for (int j = -n; j <= n; j++)
if (Math.abs(i) + Math.abs(j) <= n)
System.out.print("*");
else
System.out.print(" ");
System.out.println();
}
Output:
*
***
*****
*******
*********
*******
*****
***
*
See also: Output an ASCII diamond shape using loops