晚上改了一个实例但觉得还是觉得代码偏多了,希望各位高手改进一下,我是个初学者!
代码如下:
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);
}
}
代码这么简单,已经基本上没有什么需要改的了
不用改了,这样挺好