一道初学者的JAVA计算题?

来源:百度知道 编辑:UC知道 时间:2024/06/15 12:28:01
题目是:
山上有一口缸可以装50升水,现在有15升水。老和尚叫小和尚下山挑水,每次可以挑5升。问:小和尚要挑几次水才可以把水缸挑满?
通过编程解决问题。
public class Test{
public static void main(String[] args){

int can = 50; //需要挑这么多水
int now = 15; //现有这么多水
int count = 0;//需挑的次数
while(now < 50){
count++;
now = now + 5;
}
System.out.println("终于挑满啦!");
System.out.println("共挑水" + count + "趟");
}
}

public class Test{
public static void main(String[] args){

int can = 50;
int now = 15;
int count;
while(now < 50){
System.out.println("还是没满呢!");
count++;
now = now + 5;
}
count++;
System.out.println("终于挑满啦!");
System.out.println("共挑水" + count + "趟");

}
}

x = (50 -15)/5;

....

多加点条件:比如 桶上有个洞,挑一次会漏掉0.2升水。另,每调一次 洞会扩大,会漏掉更多的水。
这样用 编程思想,才有意义。

public class HH {
public static void main(String[] args)
{
int count = (50-15)/5;
System.out.print(count);

}
}