求java实现,类似分苹果算法实现 怎么用java画个苹果
- 用JAVA编:有一篮水果,里面有梨和苹果,如何分出两种水果的个数(最好用for循环,新手题)
- java算法,例子N个苹果放到20个袋子中,操作时间为4个小时,其中每个袋子的各苹果放入时间间隙大于30分钟
- 用java语言编程出用“ * ”形成的苹果图案
- 用Java语言求:有一篮子苹果,两个一取余一,三个一取余二,四个一取余三,五个一取刚好不剩,编写
用JAVA编:有一篮水果,里面有梨和苹果,如何分出两种水果的个数(最好用for循环,新手题)
class Fruit{}
class Apple extends Fruit{}
class Pear extends Fruit{}
public class Test0403 {
public static void main(String[] args) {
Fruit[] fruits = {new Apple(),new Apple(),new Apple(),new Pear(),new Pear()};
int appleCount = 0;
int pearCount = 0;
for(Fruit fruit : fruits){
if(fruit instanceof Apple){
appleCount++;
}else if(fruit instanceof Pear){
pearCount++;
}
}
System.out.println("苹果数量:" + appleCount);
System.out.println("梨的数量:" + pearCount);
}
}
java算法,例子N个苹果放到20个袋子中,操作时间为4个小时,其中每个袋子的各苹果放入时间间隙大于30分钟
需要使用多线程
package .xuyh.design;
import java.util.Scanner;
/**
* N个苹果放到20个袋子中,操作时间为4个小时,其中每个袋子的各苹果放入时间间隙大于30分钟
* @author XuYanhang
*
*/
public class AppleOperation implements Runnable{
public static final int ONE_SECOND = 1000;
private final int[] packages;
public final int operationTime;
public final int minSpacetTime;
private int appleCount;
private boolean timeEnd = false;
public AppleOperation(int appleCount,int packCount,int operationTime,int minSpacetTime){
this.appleCount = appleCount;
packages = new int[packCount];
this.operationTime = operationTime;
this.minSpacetTime = minSpacetTime;
}
public int getAppleCount(){
return appleCount;
}
public int getPackCount(){
return packages.length;
}
public int getOperationTime(){
return operationTime;
}
public boolean hasEnd(){
return timeEnd || appleCount == 0;
}
public synchronized void setTimeEnd(){
timeEnd = true;
}
public void run() {
int pack = Integer.parseInt(Thread.currentThread().getName());
while(appleCount>0){
synchronized(this){
if(hasEnd()){
break;
}
appleCount--;
packages[pack]++;
System.out.println("一个苹果放入了第"+(pack+1)+"个袋子中,还剩"+appleCount+"个苹果");
}
for(int i = 0 ; i < minSpacetTime ; i++){
synchronized(this){
if(hasEnd()){
break;
}
}
try {
Thread.sleep(ONE_SECOND);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
synchronized(this){
System.out.println("第"+(pack+1)+"个袋子最终有"+packages[pack]+"个苹果");
}
}
//Test main method
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int appleCount = -1;
do{
System.out.print("N个苹果放到20个袋子中,操作时间为4个小时,其中每个袋子的各苹果放入时间间隙大于30分钟。\n"
+ "输入苹果总数N:");
String string = scanner.next();
if(null != string && string.matches("^\\d{1,8}$")){
appleCount = Integer.parseInt(string);
}
}while(appleCount == -1);
scanner.close();
AppleOperation operation = new AppleOperation(appleCount,20,4*60*60,30*60);
new WaitThread(operation).start();
for (int i = 0; i < operation.getPackCount(); i++) {
Thread thread = new Thread(operation,""+i);
thread.start();
}
}
static class WaitThread extends Thread{
private AppleOperation operation;
public WaitThread(AppleOperation operation){
this.operation = operation;
}
public void run(){
try {
Thread.sleep(operation.getOperationTime()*ONE_SECOND);
} catch (InterruptedException e) {
e.printStackTrace();
}
operation.setTimeEnd();
System.out.println("时间到,苹果装袋操作结束,最后为装袋的苹果个数:"+operation.getAppleCount());
}
}
}
这里我们额外启动了21个线程,其中一个是计4小时的定时的,其他20个是向每个袋子中放苹果的线程。
用java语言编程出用“ * ”形成的苹果图案
展开全部
public static void main(String[] args) {
String[] strs = new String[] {
" *",
" *",
" *** * ***",
" *************",
" ***************",
" ***************",
" *************",
" ***** *****" };
for (String s : strs) {
System.out.println(s);
}
}
用Java语言求:有一篮子苹果,两个一取余一,三个一取余二,四个一取余三,五个一取刚好不剩,编写
public class test {
public static void main(String args[]) {
for (int i = 5;; i += 5) {
if (i % 4 == 3 && i % 3 == 2 && i % 2 == 1) {
System.out.print(i);
break;
}
}
}
}
答案是35