操作员'=';无法应用于java
嗨,我的if语句无法正常工作 if(foods.iterator().next()!=膳食.午餐) 在这行中,我得到一个错误,上面写着运算符“!=”无法应用于“comp1110.exam.Q1MealPlan.Food”、“comp1110.exam.Q1MealPlan.Food”操作员'=';无法应用于java,java,if-statement,syntax-error,Java,If Statement,Syntax Error,嗨,我的if语句无法正常工作 if(foods.iterator().next()!=膳食.午餐) 在这行中,我得到一个错误,上面写着运算符“!=”无法应用于“comp1110.exam.Q1MealPlan.Food”、“comp1110.exam.Q1MealPlan.Food” public static Set<List<Food>> getAllMealPlans(Set<Food> foods, int numMeals) { S
public static Set<List<Food>> getAllMealPlans(Set<Food> foods, int numMeals) {
Set<List<Food>> set = new HashSet<>();
List<Food> aList = new ArrayList<Food>();
System.out.println(aList);
System.out.println(foods.iterator().next().meal);
if (foods.iterator().next().meal == Meal.BREAKFAST){
aList.add(foods.iterator().next());
set.remove(foods.iterator().next());
if (foods.iterator().next() != Meal.LUNCH){
}
}
公共静态设置getAllMealPlans(设置食物、整数){
Set=newhashset();
列表列表=新的ArrayList();
System.out.println(列表);
System.out.println(foods.iterator().next().mean);
if(foods.iterator().next().fine==膳食.早餐){
add(foods.iterator().next());
set.remove(foods.iterator().next());
if(foods.iterator().next()!=膳食.午餐){
}
}
你忘了加餐了
这里有:if(foods.iterator().next().feet==feet.breaken
)
这是错误的:if(foods.iterator().next()!=fine.午餐)
所以只需将其更改为
if(foods.iterator().next().mean!=mean.午餐)
我创建了一个测试类,可以帮助您解决问题,您的代码中有几个问题
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
public class Test {
public static void main(String[] args) {
new Test().test();
}
void test() {
Set<Food> foods = new HashSet<>();
foods.add(new Food("a", Meal.BREAKFAST));
foods.add(new Food("b", Meal.BREAKFAST));
foods.add(new Food("c", Meal.LUNCH));
Set<List<Food>> mealPlans = getAllMealPlans(foods, 0);
mealPlans.forEach(f -> System.out.println(Arrays.toString(f.toArray())));
System.out.println("--------------------");
//I think what you might want is a Map from Meal to List of Foods
Map<Meal, List<Food>> mealPlans2 = getAllMealPlansMap(foods);
mealPlans2.entrySet().forEach(f ->{
System.out.println("Meal: "+f.getKey());
System.out.println(Arrays.toString(f.getValue().toArray()));
System.out.println("");
});
}
public static Set<List<Food>> getAllMealPlans(Set<Food> foods, int numMeals) { //numMeals unused?
Set<List<Food>> set = new HashSet<>();
Iterator<Food> iterator = foods.iterator(); // only instantiate the iterator once(!)
while (iterator.hasNext()) {
Food food = iterator.next(); //only call .next once(!)
List<Food> aList = new ArrayList<Food>();
if (food.meal == Meal.BREAKFAST) {
//set.remove(foods.iterator().next()); //why remove it from the set?.. and by foods.iterator() you create a NEW Iterator which is starting at index 0 again .. and check that you are calling .remove with the correct type
aList.add(food);
if (food.meal != Meal.LUNCH) {
//
}
}
if(!aList.isEmpty()) {//only add the list if its not empty
set.add(aList);
}
}
return set;
}
public static Map<Meal,List<Food>> getAllMealPlansMap(Set<Food> foods) {
return foods.stream().collect(Collectors.groupingBy(f -> f.meal));
}
enum Meal {
LUNCH, BREAKFAST
}
class Food {
String name;
Meal meal;
public Food(String name, Meal meal) {
super();
this.name = name;
this.meal = meal;
}
@Override
public String toString() {
return name + " " + meal;
}
}
}
import java.util.ArrayList;
导入java.util.array;
导入java.util.HashSet;
导入java.util.Iterator;
导入java.util.List;
导入java.util.Map;
导入java.util.Set;
导入java.util.stream.collector;
公开课考试{
公共静态void main(字符串[]args){
新测试().Test();
}
无效测试(){
Set foods=newhashset();
食物。添加(新食物(“a”,一餐。早餐));
食物。添加(新食物(“b”,膳食。早餐));
食品。添加(新食品(“c”,膳食。午餐));
设置mealPlans=getAllMealPlans(食物,0);
mealplan.forEach(f->System.out.println(Arrays.toString(f.toArray()));
System.out.println(“------------------”);
//我想你可能需要一张从一餐到食物清单的地图
Map mealPlans2=getAllMealPlansMap(食品);
mealPlans2.entrySet().forEach(f->{
System.out.println(“膳食:+f.getKey());
System.out.println(Arrays.toString(f.getValue().toArray());
System.out.println(“”);
});
}
公共静态集合getAllMealPlans(集合食物,整数){//numMeals未使用?
Set=newhashset();
迭代器迭代器=foods.Iterator();//只实例化迭代器一次(!)
while(iterator.hasNext()){
Food=iterator.next();//只调用.next一次(!)
列表列表=新的ArrayList();
if(food.mean==用餐.早餐){
//set.remove(foods.iterator().next());//为什么要将其从集合中删除?。然后通过foods.iterator()创建一个新的迭代器,该迭代器再次从索引0开始..并检查您是否正在调用。使用正确的类型删除
添加(食物);
如果(食物.膳食!=膳食.午餐){
//
}
}
如果(!aList.isEmpty()){//仅在列表不为空时添加列表
集合。添加(列表);
}
}
返回集;
}
公共静态映射getAllMealPlansMap(设置){
return foods.stream().collect(collector.groupby(f->f.fine));
}
枚举餐{
午餐、早餐
}
等级食品{
字符串名;
膳食;
公共食品(字串名称、膳食){
超级();
this.name=名称;
这顿饭;
}
@凌驾
公共字符串toString(){
返回姓名+“”+用餐;
}
}
}
很可能,根据问题行上方的if
语句,您可能指的是foods.iterator().next().meat
而不是foods.iterator().next()
。不要将此作为答案,因为这有点猜测。问题可能需要更多细节。还要添加.equals()
在处理对象时使用布尔运算符。@micpap25您是正确的,但在这种特定情况下(对于枚举),这没有什么区别