创建列表adj = ["red", "big", "tasty"] fruits = ["apple", "banana", "cherry"] 利用for循环打印每个水果的每个形容词。结果显示如下: red apple big banana tasty cherry
时间: 2024-04-05 11:28:13 浏览: 119
你可以使用嵌套的for循环来打印每个水果的每个形容词。首先,你需要创建两个列表adj和fruits,然后使用两个嵌套的for循环来遍历这两个列表,并打印每个水果和形容词的组合。
下面是实现这个功能的代码示例:
adj = ["red", "big", "tasty"]
fruits = ["apple", "banana", "cherry"]
for a in adj:
for f in fruits:
print(a, f)
这段代码会输出以下结果:
red apple
red banana
red cherry
big apple
big banana
big cherry
tasty apple
tasty banana
tasty cherry
相关问题
请编写⼀个Java程序,创建⼀个抽象的⻝物(Food)类,以及两个⼦类: ⾹蕉(Banana)和 苹 果(Apple)。同时创建⼀个接⼝Drinkable(能喝的),这个接⼝中有⼀个抽象的⽅法String howToDrink()
当然可以。首先,我们需要创建一个抽象的`Food`类作为所有食物的基础,并声明一个`Drinkable`接口。然后,我们为香蕉和苹果这两个食物类实现这个接口。以下是Java代码示例:
```java
// 定义一个抽象的Food类
public abstract class Food {
private String name;
public Food(String name) {
this.name = name;
}
public abstract void eat();
// 获取食品名称的实例方法
public String getName() {
return name;
}
}
// 创建一个 Drinkable 接口
public interface Drinkable {
abstract String howToDrink();
}
// 实现 Drinkable 接口的 Banana 类
public class Banana extends Food implements Drinkable {
public Banana(String name) {
super(name);
}
@Override
public void eat() {
System.out.println(getName() + " is delicious!");
}
@Override
public String howToDrink() {
return "You can peel and enjoy me as a snack or blend me into a smoothie!";
}
}
// 实现 Drinkable 接口的 Apple 类
public class Apple extends Food implements Drinkable {
public Apple(String name) {
super(name);
}
@Override
public void eat() {
System.out.println(getName() + " is crunchy and tasty!");
}
@Override
public String howToDrink() {
return "I'm best when you slice and eat me raw, but you can also juice me!";
}
}
// 主程序入口
public class Main {
public static void main(String[] args) {
Banana banana = new Banana("Golden");
Apple apple = new Apple("Red");
banana.eat(); // 输出:Golden is delicious!
System.out.println(banana.howToDrink());
apple.eat(); // 输出:Red is crunchy and tasty!
System.out.println(apple.howToDrink());
}
}
```
在这个例子中,`Banana`和`Apple`都继承了`Food`类并实现了`Drinkable`接口。当你运行`main`方法时,可以看到每个食物如何食用以及它们的“喝”法。
阅读全文