Java错误值初始化

时间:2013-07-14 17:10:12

标签: java initialization

我有训练对象编程,我坚持测试。 我的程序扫描宠物信息并在屏幕上打印(基础知识)。 当我输入宠物信息时:

name - Jimbo (min 3 max 16 letters)
type - 2 ( dog)
gender - 1 (male)

我得到了这个输出:

Pet info:
Your pet name is Jimbo, pet type is dog, and gender is male.

它写下一切正常,但是当我输入这个:

name - Ji
type - 8 (out of choices)
gender - 8 (out of choices)

我得到了这个输出:

Pet info:
Your pet name is ji, pet type is unknown type, and gender is unknown gender.

而不是ji我应该得到单词"wrong name length",但它仍会打印ji。 我糊涂了。我已经开始了java对象编程,想要完全理解它。

Main.java

import java.util.Scanner;

public class Main {
    public static void main(String[] args){
        Scanner key_scan = new Scanner (System.in); 


        System.out.println("Enter your pet name(min 3 letters, max 16 letters): ");// select your pet name
        String pet_name = key_scan.nextLine();

        System.out.println("Enter your pet type number 1(cat), 2(dog), 3(mouse), 4(fish): ");// select your pet type
        int pet_type = key_scan.nextInt();

        System.out.println("Enter your pet gender 1(male), 2(female): ");// select your pet gender
        int pet_gender = key_scan.nextInt();

        petInfo obj_petInfo = new petInfo(pet_name, pet_type, pet_gender);

        System.out.printf("Pet info:\n%s", obj_petInfo.formatPetInfo());

        key_scan.close(); // close scanner key_scan
    }
}

petInfo.java

public class petInfo {
    private String pet_name;
    private int pet_type;
    private int pet_gender;

    public petInfo(){
        this("Unknown", 0, 0);  
    }

    public petInfo(String pet_name_t, int pet_type_t, int pet_gender_t){
        pet_name = pet_name_t;// string name
        pet_type = pet_type_t;// int type
        pet_gender = pet_gender_t;// int gender
    }
////////////////////////////////set methods 
    public void setPet_name(String pet_name){
        pet_name = ((pet_name.length() >= 3 && pet_name.length() <= 16)? pet_name : "wrong name length");
    }
///////////////////////////////// pet type  
    public void makePet_type_cat(int pet_type){
        pet_type = 1;
    }

    public void makePet_type_dog(int pet_type){
        pet_type = 2;
    }

    public void makePet_type_mouse(int pet_type){
        pet_type = 3;
    }

    public void makePet_type_fish(int pet_type){
        pet_type = 4;
    }
////////////////////////////// pet gender   
    public void makePet_gender_male(int pet_gender){
        pet_gender = 1;
    }

    public void makePet_gender_female(int pet_gender){
        pet_gender = 2;
    }

    boolean pet_is_male(){
        return this.pet_gender == 1;
    }

    boolean pet_is_female(){
        return this.pet_gender == 2;
    }

//////////////////////////////// pet type check
    boolean pet_is_cat(){
        return this.pet_type == 1;
    }

    boolean pet_is_dog(){
        return this.pet_type == 2;
    }

    boolean pet_is_mouse(){
        return this.pet_type == 3;
    }

    boolean pet_is_fish(){
        return this.pet_type == 4;
    }
/////////////////////////////////////get methods
    public String getPet_name(){
        return pet_name;
    }

    String getPet_type(){
    if (this.pet_is_cat())
            return "cat";
        else if(this.pet_is_dog())
            return "dog";
        else if(this.pet_is_mouse())
            return "mouse";
        else if(this.pet_is_fish())
            return "fish";
        else
            return "unknown type";  
    }   

    String getPet_gender(){
        if(this.pet_is_male())
            return "male";
        else if (this.pet_is_female())
            return "female";
        return "unknown gender";
    }

    String formatPetInfo(){
        return String.format("Your pet name is %s, pet type is %s, and gender is %s.", getPet_name(), this.getPet_type(), this.getPet_gender());
    }
}

3 个答案:

答案 0 :(得分:3)

问题是尽管setPetName对名称进行了验证,但构造函数执行“直接”赋值而不进行验证。

更改构造函数,如下所示:

public petInfo(String pet_name_t, int pet_type_t, int pet_gender_t){
    setPet_name(pet_name_t);// string name
    pet_type = pet_type_t;// int type
    pet_gender = pet_gender_t;// int gender
}

这将解决问题。

请注意,您的代码违反了Java命名约定:而不是调用setter方法setPet_name,命名约定建议setPetName。以下命名约定对于确保代码可读性非常重要,因此应以类似的方式更改其他方法名称。

答案 1 :(得分:2)

您还没有在任何基本上进行验证的地方调用您的setPet_name。 name的值通过以下构造函数调用分配:

    petInfo obj_petInfo = new petInfo(pet_name, pet_type, pet_gender);

调用以下构造函数并指定名称而不进行任何验证:

public petInfo(String pet_name_t, int pet_type_t, int pet_gender_t){
    pet_name = pet_name_t;// string name
    pet_type = pet_type_t;// int type
    pet_gender = pet_gender_t;// int gender
}

您可以在构造函数中调用您的setter来分配值而不是直接赋值:

public petInfo(String pet_name_t, int pet_type_t, int pet_gender_t){
    setPet_name(pet_name_t);// string name
    pet_type = pet_type_t;// int type
    pet_gender = pet_gender_t;// int gender
}

答案 2 :(得分:0)

如果您想使用仍然可能的约定,Java有一个关键字this引用当前对象,因此您可以使用this,如:

public petInfo(String pet_name_t, int pet_type_t, int pet_gender_t){
    this.pet_name = pet_name_t;// pet_name_t refers to local variable get from parameterlist but this.pet_name_t refers to global variable of class what you defined as "private String pet_name;"
    this.pet_type = pet_type_t;// int type
    this.pet_gender = pet_gender_t;// int gender
}