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 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("* ");
}
i want to create a NxN matrix that takes N from the command line matrix such that the
element in row i and column j (1 ≤ i, j ≤ N) is a "*" (a star) if i and j are relatively
prime (ie, GCD(i, j) = 1) and " " (a space) otherwise. The row numbers should be
printed at the end of each row.
public static boolean[][] relativelyPrime(int N) {
boolean[][] array = new boolean [N+1][N+1];
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array.length; j++) {
if ( 1 <= i && i <= N && 1 <= j && j <= N) {
if (gcd(i,j) == 1){
return array;
}
}
}
}
return array;
}
public static void main(String[] args) {
int N = Integer.parseInt(args[0]);
boolean[][] matrix = relativelyPrime(N);
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix.length: j++) {
if (1 <= i && i <= N && 1 <= j && j <= N) {
if (gcd(i, j) == 1) { System.out.println("*" + i);
}
else { System.out.println(" " + i);
}
}
}
}
i am trying to create a matrix that looks like this:
* * * * * * * * * * 1
* * * * * 2
* * * * * * * 3
* * * * * 4
* * * * * * * * 5
* * * 6
* * * * * * * * * 7
* * * * * 8
* * * * * * * 9
* * * * 10
but im getting
*1
*1
*1
*1
*1
*1
*1
*1
*1
*1
*2
2
*2
2
.
.
.
how do i put this in a matrix
You should use System.out.print instead of System.out.println like this:
public static void main(String[] args) {
int N = 11;
boolean[][] matrix = relativelyPrime(N);
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix.length; j++) {
if (1 <= i && i <= N && 1 <= j && j <= N) {
if (gcd(i, j) == 1) { System.out.print("*");
}
else { System.out.print(" ");
}
}
}
System.out.println(i);
}
}
In this case, it will give you zero at the first line, to eliminate this use additional if statement:
}
if(i!=0)
System.out.println(i);
}
My code works I just want to know what to do to get it to actually look like a diamond. Everything is just left justified like this:
*
* *
* * *
* * *
* *
*
But I want it to look like this:
*
* *
* * *
* * *
* *
*
public static void s3(int num, int len)
{
for(i = 1; i<=num; i++)
System.out.print(" " + "*");
System.out.print(" ");
System.out.println();
if(num < len)
s3(num + 1, len);
for (j = 1; j<= num; j++)
System.out.print(" " + "*");
System.out.print(" ");
System.out.println();
}
}
should I use print f to format or what? I've had a long day of frustrating programming and just need some help. Thanks!
The solution is not really a beauty, but it's 4am here and that's all I can provide for now ;)
About recursion you are very welcome to think how you can do it on your own.
public static void s3(int len) {
for (int i = 0; i <= len; i++) {
for (int j = len; j > i; j--) {
System.out.print(" ");
}
for (int j = 0; j < len - (len - i); j++) {
System.out.print(" *");
}
System.out.println("");
}
for (int i = 0; i <= len; i++) {
for (int j = 0; j < (i); j++) {
System.out.print(" ");
}
for (int j = len; j > i; j--) {
System.out.print(" *");
}
System.out.println("");
}
}