日期:2014-05-20  浏览次数:20707 次

Java代码改进
Java code
import java.util.Scanner;

class MyPetl {
public static void speak(String name) {
System.out.println(name + "其实我是猪~o~");
}

public static void show(String name, int age, String color, double weight) {
System.out.println(name + ",今年" + age + "岁,是" + color + "颜色的,体重是:"
+ weight + "公斤。");
}

public static void eat(String name, String food) {
System.out.println(name + "喜欢吃" + food + "哈哈!");
}

public static void play(String playname, String name) {
System.out.println(playname + "和他的宠物" + name + "玩得好开心啊!");
}

public static void jump(double height, String name, double weight) {
if (height > 10.0) {
System.out.println(name + "真厉害,跳得好高啊!它的体重只有" + weight + "公斤。");
} else
System.out.println(name + "太胖了,跳不动哟!它的体重竟然有" + weight + "公斤!");
}
}

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

Scanner in = new Scanner(System.in);
System.out.println("请输入你的名字:");
String playname = in.next();
System.out.println("请输入宠物的名字:");
String name = in.next();
System.out.println("请输入宠物的年龄:");
int age = in.nextInt();
System.out.println("请输入宠物的颜色:");
String color = in.next();
System.out.println("请输入宠物的体重(公斤):");
double weight = in.nextDouble();
System.out.println("请输入宠物最喜欢的食物:");
String food = in.next();
System.out.println("宠物能跳多远(米):");
double height = in.nextDouble();
MyPetl mypetl = new MyPetl();
MyPetl.show(name, age, color, weight);
MyPetl.speak(name);
MyPetl.eat(name, food);
MyPetl.jump(height, name, weight);
MyPetl.play(playname, name);
}
}


------解决方案--------------------
1)因为你写的这个东西有点过程化的思想,最好加一点注释;
2)Main函数结构不清晰,拆分成多个函数条理更好一点
------解决方案--------------------
如:
testPlay(){
Scanner in = new Scanner(System.in);
System.out.println("请输入你的名字:");
String playname = in.next();
MyPetl.play(playname, name);
}
然后,再到main函数里调用