Reputation: 363
So I'm trying to fill an array with the character * at certain places to get patterns. The array's size (rows and columns) are the same and is decided by user input. Has to be odd and between 3 and 11, so for example if they put in a 5 it makes a 5 by 5 array. Anyways, I'm trying to reverse the output I got from
-----------
*
*
*
*
*
----------- to get
-----------
*
*
*
*
*
----------- but instead I get
-----------
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
----------- I used 5 as the size example here in case that helps
The problem is my loops don't seem to be working right, least I think. Here is the code
public static void main (String [] args) {
int dimension = findDimension();
char [] [] array2d = new char [dimension] [dimension];
char star = '*';
array2d = leftDiagonal(star, dimension);
print(array2d);
array2d = rightDiagonal(star, dimension);
System.out.println();
print(array2d);
}
public static int findDimension() {
int dimension = 0;
Scanner keybd = new Scanner(System.in);
do {
System.out.print("Enter an odd integer between 3 and 11 please: ");
dimension = keybd.nextInt();
} while (dimension%2 == 0);
return dimension;
}
public static void print(char [] [] arrayParam) {
for (int hyphen = 0; hyphen < (arrayParam.length*2)+1; hyphen++) {
System.out.print("-");
}
System.out.println();
for(char[] row : arrayParam)
{
for(char c : row)
System.out.print(" " + c);
System.out.printf("\n");
}
for (int hyphen = 0; hyphen < (arrayParam.length*2)+1; hyphen++) {
System.out.print("-");
}
}
public static char [] [] leftDiagonal(char starParam, int dimenParam) {
char [] [] leftD = new char [dimenParam] [dimenParam];
for (int i = 0; i < dimenParam; i++){
for (int j = 0; j < dimenParam; j++) {
if (i == j)
leftD[i][j] = starParam;
else
leftD[i][j] = ' ';
}
}
return leftD;
}
I think the problem is specifically here though as It's what decides what's saved in the array
public static char [] [] rightDiagonal(char starParam, int dimenParam) {
char [] [] rightD = new char [dimenParam] [dimenParam];
for (int i = 0; i < dimenParam; i++){
for (int j = 0; j < dimenParam; j++) {
rightD[i][j] = ' ';
// I fill all the element spaces with blanks first then put in the *
// If there's another way to do It I'd like to know
}
}
for (int i = 0; i < dimenParam; i++){
for (int j = rightD.length-1; j >= 0; j--) {
rightD[i][j] = starParam;
}
}
return rightD;
}
Upvotes: 0
Views: 81
Reputation: 2208
Your rightDiagonal
method should be similar to your leftDiagonal
method. The only difference is not to check for i==j
but for i==length-j
public static char [] [] rightDiagonal(char starParam, int dimenParam) {
char [] [] rightD = new char [dimenParam] [dimenParam];
for (int i = 0; i < dimenParam; i++){
for (int j = 0; j < dimenParam; j++) {
if (i == (dimenParam-1-j) )
rightD[i][j] = starParam;
else
rightD[i][j] = ' ';
}
}
return rightD;
}
Upvotes: 1
Reputation: 43
From what i understood u want to split your array in the vertical mid and then mirror it on the other side:
* |
* |
* |
* |
*|
to
*|
* |
* |
* |
* |
So you need to divide the width of our array by 2 and then subtract the index from the * from the new width
public static void swap_array() {
int x = your_size = 4; // 4 because thats a lot easier to understad than 5
String your_array[][] = new String[x][x];
for(int i = 0; i < your_array.length; i++) {
your_array[i][i] = "*";
}
// Now you have your start array
int new_length = your_array.length / 2; // For the mid
for(int j = 0; j < your_array.length; j++) { // Because of the 2 Dimensions
for(int k = 0; k < your_array.length; k++) {
if(your_array.equals("*")) {
your_array[j][k] = your_array[j][k - new_length];
}
break; // Because you only got one *, so we can cut the rest then
}
}
// Output
for(int l = 0; l < your_array.length; l++) {
for(int m = 0; m < your_array.length; m++) {
System.out.println(your_array[l][m]);
}
}
}
//This hasnt been tested in IDE. Its been done from brain on the fly. Do not copy paste
Upvotes: 1
Reputation: 16920
Change your rightDiagonal
method to
public static char [] [] rightDiagonal(char starParam, int dimenParam) {
char [] [] rightD = new char [dimenParam] [dimenParam];
for (int i = 0; i < dimenParam; i++){
for (int j = 0; j < dimenParam; j++) {
rightD[i][j] = ' ';
// I fill all the element spaces with blanks first then put in the *
// If there's another way to do It I'd like to know
}
}
for (int i = 0; i < dimenParam; i++){
for (int j = rightD.length-1; j >= 0; j--) {
if(i + j == rightD.length - 1) {
rightD[i][j] = starParam;
} else {
rightD[i][j] = ' ';
}
}
}
return rightD;
}
The most important part here is condition :
if(i + j == rightD.length - 1) {
rightD[i][j] = starParam;
}
Upvotes: 1