本帖最后由 小楼一夜听春雨 于 2013-11-23 19:28 编辑
//喂养小猪的一个小程序,都使用的很简单易懂的代码,想知道代码能不能优化
import java.util.*;
public class AdminCongWu {
public static void main(String[] args){
TuXiang zhu=new TuXiang();
Scanner in=new Scanner(System.in);
System.out.println("=======欢迎喂养小猪!======");
System.out.println();
Za z=new Za();
z.print(1);
System.out.println();
System.out.println();
while(true){
System.out.println(" !) 请输入你要执行操作前的序号:");
System.out.println("1.喂小猪吃食");
System.out.println("2.给小猪洗澡");
int number=in.nextInt();
if(number==1){
z.chishi();
}else if(number==2){
z.xizao();
}else{
System.out.println("请输入正确数值!");
}
}
}
}
class TuXiang{
public void zhuzhu(){
System.out.println(" ╭︿︿︿╮");
System.out.println("{/ . . /}");
System.out.println(" ( (oo) ) ");
System.out.println(" ︶ ︶ ︶ ");
}
public void gaoxing(){
System.out.println(" ╭︿︿︿╮");
System.out.println("{/ ^ ^ /}");
System.out.println(" ( (oo) ) ");
System.out.println(" ︶ ︶ ︶ ");
}
public void zang(){
System.out.println(" ╭︿︿︿╮");
System.out.println("{/ @ @ /}");
System.out.println(" ( (oo) ) ");
System.out.println(" ︶ ︶ ︶ ");
}
}
class ShuZhi{
int baoshi=10;
int qinjie=10;
int age=1;
public void baoshidu(){
System.out.println("小猪饱食度:"+baoshi);
}
public void qinjiedu(){
System.out.println("小猪清洁度:"+qinjie);
}
public void agedu(){
System.out.println("小猪年龄:"+age);
}
}
class Za extends ShuZhi{
public void print(int shu){
TuXiang t=new TuXiang();
if(shu==1){
t.zhuzhu();
}else if(shu==2){
t.gaoxing();
}else if(shu==3){
t.zang();
}
System.out.println();
System.out.println();
baoshidu();
System.out.println();
qinjiedu();
System.out.println();
agedu();
}
public void chishi(){
Random r=new Random();
baoshi+=5;
int i=r.nextInt(4);
if(i==1){
qinjie-=3;
}
if(baoshi>20&&baoshi<25){
age++;
System.out.println("小猪长大了!");
}else if(baoshi>45&&baoshi<49){
age++;
System.out.println("小猪长大了!");
}else{
int j=r.nextInt(5);
if(j==3&&baoshi>60){
age++;
System.out.println("小猪长大了!");
}
}
System.out.println("喂食成功!");
if(age>40){
print(3);
}else{
print(2);
}
}
public void xizao(){
Random r=new Random();
qinjie+=5;
int i=r.nextInt(4);
if(i==1){
baoshi-=3;
}
if(i==1){
qinjie-=3;
}
if(qinjie>20&&qinjie<25){
age++;
System.out.println("小猪长大了!");
}else if(qinjie>45&&qinjie<49){
age++;
System.out.println("小猪长大了!");
}else{
int j=r.nextInt(5);
if(j==3&&qinjie>60){
age++;
System.out.println("小猪长大了!");
}
}
System.out.println("洗澡成功!");
if(age>40){
print(3);
}else{
print(2);
}
}
}
|