P2 - naloga: izpis besed v okvirih; resitev Aljaz S.¶
Nalogo se da, kot velikansko vecino problemov v raculanistvu, resiti na vec nacinov;
import java.util.*;
class DN02 {
public static void main(String[] args) {
String[] l1 = new String[args.length];
String[] l2 = new String[args.length];
for(int i = 0; i < args.length; i++) {
l2[i] = String.format("%s %s %s", (i % 2 == 0 ? "*" : "+"), args[i], (i % 2 == 0 ? "*" : "+"));
String[] line = new String[l2[i].length()];
Arrays.fill(line, i % 2 == 0 ? "*" : "+");
l1[i] = String.join("", Arrays.asList(line));
}
System.out.println(String.join(" ", Arrays.asList(l1)));
System.out.println(String.join(" ", Arrays.asList(l2)));
System.out.println(String.join(" ", Arrays.asList(l1)));
}
}
import java.util.*;
class DN02 {
public static void main(String[] args) {
String div = "";
String mid = "";
for(int i = 0; i < args.length; i++) {
String c = i % 2 == 0 ? "*" : "+";
String line = c + " " + args[i] + " " + c;
mid += line + " ";
div += c.repeat(line.length()) + " ";
}
System.out.println(div + "\n" + mid + "\n" + div);
}
}
Seveda, se da tudi triktar loop-at skozi vse podane argumente, vsakic za eno vrstico. Predvidevam, da je vecina izbrala ta pristop.
Zadnja posodobitev:
April 5, 2022