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:
* * * **** **** * *
* * * * * * * * * *
***** * * * * * * *
* * * * **** **** *
* * ***** * * *
* * * * * * *
Can someone help me solve the bellow?
I have attached two codes. Both should be identical from my comparison. One of them prints almost normal Xmas Tree (Code 2/Result Code 2) and the other one (Code 1/Result Code 1) prints only the right side of the tree. I do not understand why and how is this possible....
Also, can someone explain to me the process of how the code works? I understand it only a little bit. Still learning and I copied Code 2 and tried to replicated manually with Code 1.
Code 1:
public class XmasTree {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("How tall do you want it?");
int height = input.nextInt();
for (int i = 0; i < height; i++) {
for (int j = 0; j < height -i; j++);{
System.out.print(" ");
}
for (int k = 0; k <= i; k++) {
System.out.print("* ");
}
System.out.println();
}
for (int i = 0; i <= height; i++){
for (int j = 0; j <= height; j++){
System.out.print(" ");
}
for (int k = 1; k < 2; k++){
System.out.print("*");
}
System.out.println();
}
input.close();
}
}
Code 2:
public class XmasTree2 {
public static void main(String[]args){
Scanner input = new Scanner(System.in);
System.out.println("How tall do you want your tree to be?");
int height = input.nextInt();
for(int i = 0; i < height; i++){
for(int j = 0; j < height - i; j++){
System.out.print(" ");
}
for(int k = 0; k <= i; k++){
System.out.print("* ");
}
System.out.println();
}
for(int i = 0; i <= height; i++){
for(int j = 0; j <= height; j++){
System.out.print(" ");
}
for(int k = 1; k < 2; k++){
System.out.print("*");
}
System.out.println();
}
input.close();
}
}
Result Code 1:
How tall do you want it?
4
*
* *
* * *
* * * *
*
*
*
*
*
Result Code 2:
How tall do you want your tree to be?
4
*
* *
* * *
* * * *
*
*
*
*
*
You have an extra ; after your second for-loop.
Do you want to know how I discovered that? I put your code in an IDE and let it auto-format it for me. That way the mistake becomes clear.
This is why correct formatting is so important.
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 //
So I have to print the following pattern by accepting a value of n
Input : 7
Output has to be this :
*
**
***
****
*****
******
*******
******
*****
****
***
**
*
code:
public static void printPattern(int n)
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=i;j++)
{
System.out.println("*");
}
System.out.println("\n");
}
for (int a = n-1; a >= 1; a--)
{
for (int b = 1; b <= a; b++)
{
System.out.print("*");
}
System.out.println("\n");
}
}
But for some reason it prints this pattern(say n=8):
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*******
******
*****
****
***
**
*
What is the mistake here?
Use System.out.print instead of System.out.println in the first for loop, the latter one always appends a newline character at the end which is what you are trying to do manually.
And either do System.out.print("\n"); OR System.out.println(""); to add the newline after the inner loop iteration.
System.out.println already adds a line break at the end, therefore System.out.println("\n") adds two line breaks.
The code can be condensed to a single double-for loop. The following routine accepts a single parameter which defines the maximum number of '*' on a single line:
/**
* #param width
* the maximum width of the pattern.
*/
public static void print(int width) {
for (int i = 1; i < 2 * width; ++i) {
for (int j = 0; j < width - Math.abs(width - i); ++j)
System.out.print("*");
System.out.println();
}
}
public static void main(String[] args) {
print(4);
}
Output:
*
**
***
****
***
**
*
first for loop should be like this,
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.print("\n");
}
System.out.println();
always move the cursor to a new line
in your code when we use System.out.println("\n"); this will move cursor to 2 lines
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
for (int a = n - 1; a >= 1; a--) {
for (int b = 1; b <= a; b++) {
System.out.print("*");
}
System.out.println();
}
By changing println to print you won't go to another line after each * and remove the \n to not skip 2 lines because println it's already there
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