يبدوا أني حليت السؤال في الأعلى بغير المطلوب في السؤال ,, الحل على كلام السؤال :
الحل :
كود:
public class first {
public static void main(String[] args) {
int n = Integer.parseInt(args[0].toString());
String box = "1";
int now = 0,total = 0;
int i = 1;
while ( (i-1) < n) {
if (i == 1) {now = 1;total = 1;i++;} else {
now = i;
total += i;
box += " + " + now;
i++;
}
System.out.println(box + " = " + total);
}
}
}
تجربة البرنامج ,, ( الـ args[0] يساوي 7 ) .
النتيجة :
كود:
run:
1 = 1
1 + 2 = 3
1 + 2 + 3 = 6
1 + 2 + 3 + 4 = 10
1 + 2 + 3 + 4 + 5 = 15
1 + 2 + 3 + 4 + 5 + 6 = 21
1 + 2 + 3 + 4 + 5 + 6 + 7 = 28
BUILD SUCCESSFUL (total time: 0 seconds)