输出失败使用Java中的驱动程序/程序文件中的get.Name()

我必须制作一个应该接收宠物信息输入并以特定方式输出的程序。
通常情况下,这将是一个蛋糕,需要10分钟,但我们刚刚进入OOP,并且在确定将什么放在驱动程序中的增变器上时遇到了一些麻烦。输出失败使用Java中的驱动程序/程序文件中的get.Name()

司机:

import java.util.HashSet; 

import java.util.Set;

public class JMPets {

private String petType;

private String petName;

private int petAge;

private double petWeight;

boolean isMale;

public void setType(String petType)

{

this.petType = petType;

}

public void setName(String petName)

{

this.petName = petName;

}

public void setAge (int petAge)

{

this.petAge = petAge;

}

public void setWeight(double petWeight)

{

this.petWeight = petWeight;

}

public String getType()

{

return petType;

}

public String getName()

{

return petName;

}

public int getAge()

{

return petAge;

}

public double getWeight()

{

return petWeight;

}

public void set(String petType, String petName, int petAge,

double petWeight)

{

//WHAT DO I PUT HERE

}

}


import java.util.Scanner; 

public class JMUnit6 {

public static void main(String[] args) {

JMPets myPet1 = new JMPets();

JMPets myPet2 = new JMPets();

JMPets mypet3 = new JMPets();

Scanner stdIn = new Scanner(System.in);

System.out.println("Welcome to the Java Pet Tracker");

System.out.println("Please enter the type of Pet #1:");

String petType = stdIn.nextLine();

System.out.println("Please enter the name of Pet #1:");

String petName = stdIn.nextLine();

System.out.println("Please enter the age of " +petName+":");

int petAge = stdIn.nextInt();

System.out.println("Please enter the weight of "+petName+":");

double petWeight = stdIn.nextDouble();

System.out.println("Is "+petName+" Male?:");

boolean isMale = stdIn.nextBoolean();

myPet1.set(petType, petName, petAge, petWeight);

System.out.println(myPet1.getType());

System.out.println(myPet1.getName());

System.out.println(myPet1.getAge());

System.out.println(myPet1.getWeight());

}//end main

}//end class JMUnit6

我得到的唯一的输出为空空0 0.0。

回答:

看看你的制定者,你已经有了答案。

this.petType = petType; 

this.petName = petName;

this.petAge = petAge;

this.petWeight = petWeight;

您也可能会丢失isMale

回答:

这是一个练习,让你理解构造函数和增变器。您可以使用访问器测试您的实现。

这篇文章介绍了一些基础知识。 Java - Using Accessor and Mutator methods

The official tutorial也可以,但更重要的是 - constructors。


至于您的评论,这里有一些指导方针。

对于第一宠物,使用默认的构造,并使用适当的增变方法来设置所有变量

JMPets myPet1 = new JMPets(); // use the default constructor 

System.out.println("Please enter the type of Pet #1:");

String petType = stdIn.nextLine();

myPet1.setType(petType); // use proper mutator methods to set all variables

// TODO: stdIn.nextLine for remainder of values. Use the 'individual' set methods

对于第二PET,使用接受类型作为单个参数的构造为所有其他值使用适当的增变器方法

(这将不会编译,直到您实现此构造函数)

System.out.println("Please enter the type of Pet #2:"); 

String petType = stdIn.nextLine();

JMPets myPet2 = new JMPets(petType); // TODO: Implement this

System.out.println("Please enter the name of Pet #2:");

String petName = stdIn.nextLine();

myPet2.setName(petName); // use proper mutator methods for all other values

// TODO: stdIn.nextLine for remainder of values. Use the 'individual' set methods

对于第三宠物,用它作为前接受所有值作为参数

再次,同样的问题,一个构造函数,需要一个构造函数(见最后一行)。

System.out.println("Please enter the type of Pet #3:"); 

String petType = stdIn.nextLine();

System.out.println("Please enter the name of Pet #3:");

String petName = stdIn.nextLine();

System.out.println("Please enter the age of " +petName+":");

int petAge = stdIn.nextInt();

System.out.println("Please enter the weight of "+petName+":");

double petWeight = stdIn.nextDouble();

JMPets mypet3 = new JMPets(petType, petName, petAge, petWeight);


现在,如果你做这一切中的一个方法,你将得到一个变量已经定义的错误。

例如,

String name = "bob"; 

String name = "sally"; // <--- Error. 'name' already defined.

相反,只是重新分配name = "sally";,没有String name需要第二次

以上是 输出失败使用Java中的驱动程序/程序文件中的get.Name() 的全部内容, 来源链接: utcz.com/qa/261138.html

回到顶部