首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java相关 >

Java代码改善

2012-08-16 
Java代码改进Java codeimport java.util.Scannerclass MyPetl {public static void speak(String name) {

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 + "公斤。");} elseSystem.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函数里调用

热点排行