Java
Practice Java fundamentals, object-oriented code, and collections.
Variables and Output
简单Declare local values and print a summary.
public class Main {
public static void main(String[] args) {
String name = "Mira";
int score = 128;
System.out.printf("%s: %d points%n", name, score);
}
}Enhanced For Loop
中等Traverse a list and calculate its total.
import java.util.List;
public class Main {
public static void main(String[] args) {
List<Integer> values = List.of(4, 8, 15, 16);
int total = 0;
for (int value : values) {
total += value;
}
System.out.println(total);
}
}Counter Class
中等Create a class with a constructor and method.
public class Counter {
private int value;
public Counter(int start) {
value = start;
}
public int increment() {
value += 1;
return value;
}
}