我有培训对象编程,我坚持测试。我的程序扫描宠物信息并在屏幕上打印(基础知识)。当我输入宠物信息时:
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 对象编程并且想完全理解它。
主.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
}
}
宠物信息.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());
}
}