How to combine these conditions in one cycle?
I tried to loop them if, but I don't understand where I need to put the loop?
public static void Run(int n) {
int l;
int c;
for (l = 1; l <= 2 * n - 1; l++) {
System.out.println("\n");
for (c = 1; c <= 2 * n - 1; c++) {
if ((l == 1) || (l == 2 * n - 1) || (c == 1) || (c == 2 * n - 1)) {
System.out.print('a');
} else if ((l == 2) || (l == 2 * n - 2) || (c == 2) || (c == 2 * n - 2)) {
System.out.print(defLettre(n, 1));
} else if ((l == 3) || (l == 2 * n - 3) || (c == 3) || (c == 2 * n - 3)) {
System.out.print(defLettre(n, 2));
} else if ((l == 4) || (l == 2 * n - 4) || (c == 4) || (c == 2 * n - 4)) {
System.out.print(defLettre(n, 3));
} else if ((l == 5) || (l == 2 * n - 5) || (c == 5) || (c == 2 * n - 5)) {
System.out.print(defLettre(n, 4));
} else {
System.out.print(" ");
}
}
}
}
}
The thing is, the bigger I get bigger if I need to enter and I don't understand how you reunite them.
EDIT:
Thanks for your reply. The program I wanted to do is this:
http://pastebin.com/dKBGjVqj (couldn't insert it correctly here).
I was able to do this, only it was confusing, because if n was like 10, I would have to enter 10 if..sse.
By the way, my program must be run under 1 s on a 1 GHz computer and must be below 8000 kb. How can I see what is working under one part? Java size for size I'm guessing.
source to share
With Java 8, I would do something like this:
public static void Run(int n) {
int l;
int c;
for (l = 1; l <= 2 * n - 1; l++) {
System.out.println("\n");
for (c = 1; c <= 2 * n - 1; c++) {
final int lf = l, cf = c;
IntPredicate pred = x -> lf == x || lf == 2*n - x || cf == x || cf == 2*n - x;
IntStream.range(1,2*n - 1).filter(pred).findFirst()
.ifPresent(x -> System.out.println(x == 1 ? "a" : defLettre(n,x)));
}
}
}
source to share
You can try this code:
public static void Run(int n) {
int l;
int c;
for (l = 1; l <= 2 * n - 1; l++) {
System.out.println("\n");
for (c = 1; c <= 2 * n - 1; c++) {
for(int k=1; k<=n; k++){
if ((l == k) || (l == 2 * n - k) || (c == k) || (c == 2 * n - k)) {
System.out.print('a');
}else {
System.out.print(" ");
}
}
}
}
}
source to share
As others have stated, I'm not entirely sure what you are trying to do. But given your requirements, I believe this solution will work for you.
Problem
To repeat your problem, you want to execute a function defLettre
when one of four conditions is met.
-
l == j
for some j -
c == j
for some j -
l == 2 * n - j
for some j -
c == 2 * n - j
for some j
With special conditions around cases where l == 1
or c == 1
.
Decision
To solve this problem, you need to use an inner loop that iterates over the variable j
I introduced in the discussion.
public static void Run(int n) {
final int bound = 2 * n;
for (int l = 1; l <= bound - 1; l++) {
System.out.println("\n");
for (int c = 1; c <= bound - 1; c++) {
boolean match = false;
for (int j = 1; j < 2 * n; j++){
if ((l == 1) || (l == bound - j) || (c == 1) || (c == bound - j)) {
if (j == 1) {
System.out.print('a');
} else {
System.out.print(defLettre(n, j - 1));
}
match = true;
break;
}
}
if (!match){
System.out.println(" ");
}
}
}
}
Discussion
Note. I figured out bound
how 2 * n
. Further, the inner large loop above j
must be between j == 1
and j == 2 * n
, because on either side of the limits, conditions will always evaluate to false.
Cautions
The logic you are describing integrates into something I have defined as j
. But the failure condition does a side effect of printing the character
. There j
may be better, more stringent constraints that differ from what I've described to you, but I don't know what they have without a better description of your algorithm.
source to share