当前位置:编程学习 > JAVA >>

java编程:山上有一口缸可以装50升水,现在有15升。老和尚叫小和尚下山挑水,每次挑5升,要挑几次

答案://缸 int pot=50; //剩余 int rest =pot-15; for(int i=0;i<=pot/5;i++){ if(rest -5*i==0){ system.out.println("小和尚下山挑水 的次数"+i); } }
其他:int all = 50 - 15;//需要挑的水的总量
int times = 0;   //次数
for(int i = 0; i * 5 < all ;i++){
     times++;
}
 System.out.print(times); /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication1;

/**
 *
 * @author Administrator
 */
public class JavaApplication1 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
       He_shang he_shang= new He_shang(); 
       Gang gang = new Gang();
       int i=he_shang.tiao_shui(gang);
       System.out.println("需要挑水"+i+"次");
    }
}

class He_shang {
    int max = 5;
    int sum=0;
    public int tiao_shui(Gang gang) {
        while (gang.now < gang.max) {
            sum=sum+1;
            gang.now=gang.now+max;
        }
        return sum;
    }
}

class Gang {

    int max = 50;
    int now = 15;

    public int getMax() {
        return max;
    }

    public void setMax(int max) {
        this.max = max;
    }

    public int getNow() {
        return now;
    }

    public void setNow(int now) {
        this.now = now;
    }
} int gang = 50;
int yuanyou = 15;
int n = 5;
for(t = 0; t < 20; t++)
(gang -yuanyou) > t*n;
system.out.println("小和尚下山挑水的次数"+t); public class 第二题 {
 public static void main(String[] args) {
  int pot =50;
  int rest = pot-15;
  for(int i=0;i<=rest/5;i++)
   if(rest-5*i==0){
   
   System.out.println("小和尚挑了"+i);
 
   }
 }
} 

上一个:求教运行一段JAVA程序用的是WTK JDK和EClipse 或者出钱请人帮我运行下 可以出钱 QQ416564930 坐等!!!
下一个:Java程序设计

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,