模拟三个老师给50个小朋友分苹果java
时间: 2024-11-20 14:45:08 浏览: 26
在Java中,模拟三个老师给50个小朋友分苹果可以创建一个简单的类结构,假设每个老师有不同的分配策略,我们可以这样做:
```java
public class Apple {
// 苹果数量
private int apples;
}
class Teacher {
// 教师ID
private String id;
public void distributeApples(Apple apples) {
System.out.println("Teacher " + id + " is distributing apples.");
// 分配逻辑可以根据实际情况更改
if (apples.getApples() >= 10) {
distributeEqually(apples);
} else {
distributeUnequally(apples);
}
}
private void distributeEqually(Apple apples) {
for (int i = 0; i < 50; i++) {
System.out.println("Teacher " + id + " gives an apple to student " + i);
}
}
private void distributeUnequally(Apple apples) {
System.out.println("Teacher " + id + " distributes apples unequally based on preference.");
// 这里可以进一步细化每个老师的偏好算法
}
}
public class Main {
public static void main(String[] args) {
Apple apples = new Apple();
apples.setApples(50); // 假设总共有50个苹果
Teacher teacher1 = new Teacher();
teacher1.setId("Teacher A");
teacher1.distributeApples(apples);
Teacher teacher2 = new Teacher();
teacher2.setId("Teacher B"); // 另一个老师的分配逻辑
teacher2.distributeApples(apples);
Teacher teacher3 = new Teacher();
teacher3.setId("Teacher C"); // 第三个老师的分配逻辑
teacher3.distributeApples(apples);
}
}
```
在这个例子中,我们定义了一个`Apple`类表示苹果总数,`Teacher`类有各自的分发方法(`distributeEqually`和`distributeUnequally`),并且在`main`函数中创建了三位老师实例并分别分配苹果。
阅读全文