英文:
Should I use AND or OR when combining two negative conditions?
问题
三种车辆类型分别是Car(汽车)、Motorcycle(摩托车)和Bicycle(自行车)。三种状态分别是Available(可用)、Reserved(预定)和Sold(已售)。
我想打印所有不是Car且不是Sold的车辆信息,即不应打印已售的汽车。换句话说,打印所有Motorcycle或Bicycle的信息,无论其状态是Available、Reserved还是Sold。如果是Car,只要它是Available或Reserved,也要打印。
车辆信息如下:
- Car - Sold
- Car - Available
- Car - Reserved
- Bicycle - Sold
- Bicycle - Available
- Bicycle - Reserved
- Motorcycle - Sold
- Motorcycle - Available
- Motorcycle - Reserved
我期望以下代码打印除了编号1(Car - Sold)之外的所有内容:
我的代码:
for _, v := range results {
if v.Type != "Car" && v.Status != "Sold" { // && 不起作用,但 || 起作用
resp = append(resp, &VehicleInfo {
ID: v.Id,
Brand: v.Brand,
Type: v.Type,
Status: v.Sold,
})
}
}
fmt.Println(resp)
当我使用AND(&&)时,Println的结果非常奇怪,它输出了5、6、8、9。然而,当我改用OR(||)时,它打印出了我想要的结果,即除了1(已售汽车)之外的所有内容,即所有Motorcycle(任何状态)、所有Bicycle(任何状态)和所有Car(只要是Available或Reserved)的列表。
问题出在哪里?我原以为使用AND(&&)是正确的答案,但事实并非如此。
英文:
Three vehicle types are Car, Motorcycle and Bicycle. Three statuses are Available, Reserved and Sold.
I want to print the information of all vehicles that are not Car and are not Sold, i.e. sold cars should not be printed. In other words, print information of everything that is Motorcycle or Bicycle, with any status among Available, Reserved and Sold. If it is a Car, still print as long as it is Available or Reserved.
The vehicles are:
- Car - Sold
- Car - Available
- Car - Reserved
- Bicycle - Sold
- Bicycle - Available
- Bicycle - Reserved
- Motorcycle - Sold
- Motorcycle - Available
- Motorcycle - Reserved
I expect the following code to print everything except number 1 (Car - Sold)
My code:
for _, v := range results {
if v.Type != "Car" && v.Status != "Sold" { // && does not work but || works
resp = append(resp, &VehicleInfo {
ID: v.Id,
Brand: v.Brand,
Type: v.Type,
Status: v.Sold,
})
}
}
fmt.Println(resp)
When I use AND (&&), Println result is very strange, it outputs 5, 6, 8, 9. However, when I switch to OR (||), it prints exactly what I want, which is everything except 1 (sold car), which is a list of all Motorcycle (any status), all Bicycle (any status) and all Car that is either Available or Reserved.
What is the problem here? I thought using AND (&&) was the right answer, but it is not.
答案1
得分: 0
你的问题陈述不清楚。陈述中说:
> 我想打印所有不是Car且不是Sold的车辆的信息...
但问题陈述的其余部分说:
> ...也就是说,已售出的汽车不应该被打印。换句话说,打印所有属于Motorcycle或Bicycle的信息,无论其状态是Available、Reserved还是Sold。如果是Car,只要它是Available或Reserved,也要打印。
这说明你想要过滤掉已售出的汽车。
最简单的方法是这样的:
for _, v := range results {
isSoldCar := v.Type == "Car" && v.Status == "Sold"
if isSoldCar {
continue
}
resp = append(resp, &VehicleInfo {
ID: v.Id,
Brand: v.Brand,
Type: v.Type,
Status: v.Sold,
})
}
或者这样:
for _, v := range results {
isSoldCar := v.Type == "Car" && v.Status == "Sold"
if !isSoldCar {
resp = append(resp, &VehicleInfo {
ID: v.Id,
Brand: v.Brand,
Type: v.Type,
Status: v.Sold,
})
}
}
英文:
Your problem statement is... unclear. The statement
> I want to print the information of all vehicles that are not Car and are not Sold,...
But the remainder of the problem statement:
> ... i.e. sold cars should not be printed. In other words, print information of everything that is Motorcycle or Bicycle, with any status among Available, Reserved and Sold. If it is a Car, still print as long as it is Available or Reserved.
Indicates that you want to filter out (exclude) cars that have been sold.
The easiest way is something like this:
for _, v := range results {
isSoldCar := v.Type == "Car" && v.Status == "Sold"
if isSoldCar {
continue
}
resp = append(resp, &VehicleInfo {
ID: v.Id,
Brand: v.Brand,
Type: v.Type,
Status: v.Sold,
})
}
or this:
for _, v := range results {
isSoldCar := v.Type == "Car" && v.Status == "Sold"
if !isSoldCar {
resp = append(resp, &VehicleInfo {
ID: v.Id,
Brand: v.Brand,
Type: v.Type,
Status: v.Sold,
})
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论