java给小朋友分苹果怎么写·
时间: 2024-05-27 22:12:29 浏览: 73
欢迎提问!关于Java给小朋友分苹果的问题,可以使用以下代码实现:
import java.util.Scanner;
public class DivideApples {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("请输入小朋友的个数:");
int n = sc.nextInt();
System.out.print("请输入苹果的个数:");
int m = sc.nextInt();
int result = m / n; //平均分配的苹果数
int remainder = m % n; //剩余的苹果数
System.out.println("每个小朋友分配了" + result + "个苹果");
System.out.println("还剩" + remainder + "个苹果");
}
}
以上代码可以让用户输入小朋友的个数和苹果的个数,然后计算出平均分配的苹果数和剩余的苹果数,并输出结果。希望可以帮到您!
相关问题
模拟三个老师给50个小朋友分苹果java
在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`函数中创建了三位老师实例并分别分配苹果。
java模拟三个老师同时给50个小朋友发苹果,每个老师相当于一个线程,每个小朋友发一个苹果,按照小朋友的顺序有序分发,利用多线程模拟老师发苹果过程。
可以使用Java中的线程池来模拟三个老师的发苹果过程,每个线程负责发苹果给若干个小朋友。以下是实现的代码示例:
```java
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class AppleTeacher implements Runnable {
private int start;
private int end;
private String name;
public AppleTeacher(int start, int end, String name) {
this.start = start;
this.end = end;
this.name = name;
}
@Override
public void run() {
for (int i = start; i <= end; i++) {
System.out.println(name + "老师给第" + i + "个小朋友发苹果");
}
}
public static void main(String[] args) {
int totalStudents = 50;
int studentsPerTeacher = totalStudents / 3;
ExecutorService executorService = Executors.newFixedThreadPool(3);
executorService.submit(new AppleTeacher(1, studentsPerTeacher, "A"));
executorService.submit(new AppleTeacher(studentsPerTeacher + 1, studentsPerTeacher * 2, "B"));
executorService.submit(new AppleTeacher(studentsPerTeacher * 2 + 1, totalStudents, "C"));
executorService.shutdown();
}
}
```
在上面的代码中,我们定义了一个AppleTeacher类实现了Runnable接口,并在其中实现了老师发苹果的逻辑。在主函数中,我们首先计算出每个老师需要发给的小朋友的编号范围,然后创建一个线程池,并将三个老师的任务提交到线程池中。最后,我们调用shutdown()方法关闭线程池。
运行上面的代码,可以看到三个老师依次给50个小朋友发苹果,每个小朋友都按照顺序接收到了一个苹果。
阅读全文