Вот версия, отличная от jQuery, которая работает во всех основных браузерах в IE 5, чтобы продемонстрировать, насколько небезопасна жизнь без jQuery.
Демо: http://jsfiddle.net/ timdown / aHW9J /
Код:
var el = document.getElementById("foo"), child = el.firstChild, nextChild;
while (child) {
nextChild = child.nextSibling;
if (child.nodeType == 3) {
el.removeChild(child);
}
child = nextChild;
}
Это можно сделать очень просто с помощью счетчика:
public static void printFirstEvenNumber(int total) {
for (int i = 0, j = 0; j < total; i += 2, j++)
System.out.println("Even number " + i + ", count is " + j);
}
Демо
printFirstEvenNumber(4);
Even number 0, count is 0
Even number 2, count is 1
Even number 4, count is 2
Even number 6, count is 3
это решение
public static void main(String[] args) {
int count = 0;
for (int i=1; i<=10; i++) {
if (i%2==0) {
System.out.println("My Name is Nikki\r\nI am learning Core Java");
count++;
}
if (count == 4) {
break;
}
}
}