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'm trying to create a program that depends on user input for integers between 2 and 10.
If the user entered four, this should be the output:
****
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
****
I want an input of 4 to output four stars in the first line to make a horizontal edge, and then a 4-star diagonal. Then four stars along a vertical “edge”, before repeating the diagonal and horizontal edge.
So I can draw the first line, last and middle lines right but my diagonals won't even show the spaces for some reason!
****
**
**
**
**
* *
* *
* *
* *
**
**
**
**
****
This is my code (I'm a beginner but I've been trying to fix this a lot and I really need some help):
int num = 0;
System.out.println("Enter a value between 2 and 10.");
num = keyNum.nextInt();
while (num < 2 || num > 10) {
System.out.println("Enter a valid number please.");
num = keyNum.nextInt();
}
for (int a = 0; a < num + 1; a++)
{
System.out.print(" ");
}
for (int b = 0; b < num; b++)
{
System.out.print("*");
}
for (int c = 0; c < num; c++)
{
System.out.println("");
for (int d = num; d < 1; d--)
{
System.out.print(" ");
}
System.out.print("*");
for (int e = (num * 3) - 2; e < num; e++)
{
System.out.print(" ");
}
System.out.print("*");
}
for (int f = 0; f < num; f++)
{
System.out.println("");
System.out.print("*");
for (int g = 0; g < num * 3; g++)
{
System.out.print(" ");
}
System.out.print("*");
}
for (int h = 0; h < num; h++)
{
System.out.println("");
for (int i = num; i < 1; i--)
{
System.out.print(" ");
}
System.out.print("*");
for (int j = (num * 3) - 2; j < num; j++)
{
System.out.print(" ");
}
System.out.print("*");
}
System.out.println("");
for (int k = 0; k < num + 1; k++)
{
System.out.print(" ");
}
for (int l = 0; l < num; l++)
{
System.out.print("*");
}
Any kind of help would be appreciated! Thank you.
This should work
Just changed couple of for loop conditions
while (num < 2 || num > 10) {
System.out.println("Enter a valid number please.");
num = keyNum.nextInt();
}
for (int a = 0; a < num + 1; a++)
{
System.out.print(" ");
}
for (int b = 0; b < num; b++)
{
System.out.print("*");
}
for (int c = 0; c < num; c++)
{
System.out.println("");
for (int d = num; d > c; d--)
{
System.out.print(" ");
}
System.out.print("*");
for (int e = num * 2; e < (num * 3) + (c *2); e++)
{
System.out.print(" ");
}
System.out.print("*");
}
for (int f = 0; f < num; f++)
{
System.out.println("");
System.out.print("*");
for (int g = 0; g < num * 3; g++)
{
System.out.print(" ");
}
System.out.print("*");
}
for (int h = 1; h <= num; h++)
{
System.out.println("");
for (int i = 0; i < h; i++)
{
System.out.print(" ");
}
System.out.print("*");
for (int j = 0; j < (num * 3) - (h * 2) ; j++)
{
System.out.print(" ");
}
System.out.print("*");
}
System.out.println("");
for (int k = 0; k < num + 1; k++)
{
System.out.print(" ");
}
for (int l = 0; l < num; l++)
{
System.out.print("*");
}
I have developed a code to print a diamond in java. The code prints a diamond using * and "o" and the code is:
System.out.println("Diamond Height: " + DIAMOND_SIZE);
System.out.println("Output for: For Loop");
int noOfRows = DIAMOND_SIZE;
//Getting midRow of the diamond
int midRow = (noOfRows)/2;
//Initializing row with 1
int row = 1;
//Printing upper half of the diamond
for (int i = midRow; i > 0; i--)
{
//Printing i spaces at the beginning of each row
for (int j = 1; j <= i; j++) {
System.out.print(" ");
}
//Printing j *'s at the end of each row
for (int j = 1; j <= row; j++) {
System.out.print("* ");
}
System.out.println();
//Incrementing the row
row++;
}
//Printing lower half of the diamond
for (int i = 0; i <= midRow; i++) {
//Printing i spaces at the beginning of each row
for (int j = 1; j <= i; j++) {
System.out.print(" ");
}
//Printing j *'s at the end of each row
int mid = (row+1) / 2;
for (int j = row; j > 0; j--) {
if(i==0 && j==mid) {
System.out.print("o ");
}
else {
System.out.print("* ");
}
}
System.out.println();
//Decrementing the row
row--;
}
}
The result I get from this is:
Diamond Height: 5
*
* *
* o *
* *
*
Diamond Height: 3
*
* o
*
But Im trying to get the following results:
Diamond Height: 5
*
* * *
* * o * *
* * *
*
Diamond Height: 3
*
* o *
*
What am I doing wrong? I have tried several things but nothing seems to help, Please help.
You can modify your inner loop logic when printing lower half of the diamond.
//Printing j *'s at the end of each row
int mid = (row+1) / 2;
for (int j = row; j > 0; j--)
{
if(i==0 && j==mid) System.out.print("o ");
else System.out.print("* ");
}
Good day guys! So I'm self learning java right now. And one of the exercises I'm answering now is creating a program Pyramid.java that takes an input N and prints a
pyramid whose each side is of length N, like the one below: My problem is that everytime I put an input on the command line, the 3 asterisk in the middle don't appear.
*
* *
* * *
* * * *
* * * * *
Here's my code
public class D2Q6 {
public static void main(String[] args) {
int N = Integer.parseInt(args[0]);
int k = 1;
for(int i = 1; i <= N; i++) {
for(int j = 0; j < N-i; j++)
System.out.print(" ");
System.out.print("*");
if(i > 1) {
if(i == N) {
for(int j = 0; j < i-1; j++)
System.out.print(" *");
}
else {
for(int j = 0; j < k; j++)
System.out.print(" ");
k = k+2;
System.out.print("*");
}
}
System.out.println();
}
}
I presume you are trying to execute your program through eclipse or some editor with its own console. The issue might not be with the program, but with the font (type and size) used in console.
Try running the program in DOS prompt, and you might be able to see your desired outcome.
public class PrintPyramidStar
{
public static void main(String args[])
{
int c = 1;
for (int i = 1; i <= 8; i++)
{
for (int j = i; j < 8; j++)
{
System.out.print(" ");
}
for (int k = 1; k <= c; k++)
{
if (k % 2 == 0)
System.out.print(" ");
else
System.out.print("*");
}
System.out.println();
c += 2;
}
}
}
Above is a random star program I picked using search. Running the same via eclipse console gave me skewed triangle, wherein running the same via windows command prompt gave me a perfect triangle.
I stepped through your program in Eclipse and thought the problem might be here. Currently this is printing a blank space.
else {
for(int j = 0; j < k; j++)
System.out.print(" ");
k = k+2;
System.out.print("*");
}
If you change it to the below, it prints solid asterisk then an extra one at the end;
else {
for(int j = 0; j < k; j++)
System.out.print("*");
k = k+2;
System.out.print("*");
}
However I assume that you are going for a '* * *' kind of thing, but hopefully that narrows it for you.
I would start by creating two methods, one to get the spaces and one to get the asterisks. Something like,
private static String getNSpaces(int n) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < n; i++) {
sb.append(' ');
}
return sb.toString();
}
private static String getNAsterisks(int n) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < n; i++) {
if (i != 0) {
sb.append(' ');
}
sb.append('*');
}
return sb.toString();
}
Then your main method can be as simple as -
for (int i = 1; i <= N; i++) {
System.out.print(getNSpaces(N - i));
System.out.println(getNAsterisks(i));
}
When I run the above with N equal to 5 I get
*
* *
* * *
* * * *
* * * * *
I thought I would post an alternative, brief approach to this common programming exercise.
public static void main(String args[]) {
System.out.println(pyramid(Integer.parseInt(args[0])));
}
public static String pyramid(int size) {
return line(size, size);
}
public static String line(int max, int size) {
return size < 1 ? "" : line(max, size - 1) + copies(" ", max - size) + copies("* ", size) + '\n';
}
public static String copies(String s, int size) {
return size < 1 ? "" : String.formatîui("%0" + size + "d", 0).replace("0", s);
}
See live demo
This uses recursion and breaks the code up into meaningful methods that each have their own task that is accomplished in one line.
This is not a high performance solution, but would be fast enough for moderate sizes. It is written for readability and method cohesion.