我正在进行MOOC java课程,而且我仍然坚持练习76.每当我提交答案时,它会告诉我将每餐打印成单独的一行。我该怎么做呢?
主要课程
dispatchTouchEvent
菜单类
ACTION_DOWN
输出
public class Main {
public static void main(String[] args) {
Menu exactum = new Menu();
exactum.addMeal("Fish fingers with sour cream sauce");
exactum.addMeal("Vegetable casserole with salad cheese");
exactum.addMeal("Chicken and nacho salad");
exactum.printMeals();
exactum.clearMenu();
exactum.printMeals();
}
}
答案 0 :(得分:2)
只需遍历列表并单独打印每个项目:
public void printMeals() {
for(String meal : meals) {
System.out.println(meal);
}
}
答案 1 :(得分:0)
在 Java 8
中public void printMeals() {
this.meals.stream().forEach(System.out::println);
}
答案 2 :(得分:0)
import java.util.ArrayList;
public class Menu {
private ArrayList<String> meals;
public Menu() {
this.meals = new ArrayList<String>();
}
public void addMeal(String meal) {
if (!meals.contains(meal)) {
meals.add(meal);
}
}
public void printMeals() {
if (meals.isEmpty()){
System.out.println("No Meal Object Found !");
return;
}
for(String meal : meals) {
System.out.println(meal);
}
}
public void clearMenu(){
meals.removeAll(meals);
}
}