我无法弄清楚如何使这个代码工作,我必须创建一个类具有年龄和名称的getter和setter的类,然后是一个名为Kennel的驱动程序类,其主要方法实例化并更新几个狗对象。
它应该打印出名称和年龄,但它只是名字。
这就是我目前所拥有的。
public class Dog{
private String dogname;
private int dogage;
public Dog(int dogAge2, String dogName2){
this.dogage = dogAge2;
this.dogname = dogName2;
}
public void setName(String name) {
}
public String getName() {
return dogname;
}
public void setAge(int age) {
this.dogage = age;
}
public int getAge() {
return dogage;
}
public int getPersonYears(){
return this.dogage*7;
}
}
然后是Kennel Class
import java.util.Scanner;
public class Kennel{
public static void main (String[] args)
{
int age;
String name;
Scanner scan = new Scanner (System.in);
System.out.println ( " Spot "); //Name of Dog
name = scan.nextLine();
System.out.println (" 5 " ); // Age of Dog
age= scan.nextInt();
Dog dog = new Dog(age, name);
System.out.println("In human years the dog is: " + dog.getPersonYears());
}
}
答案 0 :(得分:0)
你应该像这样写下正确的信息:
System.out.println ( "Enter name. "); //Name of Dog
...
System.out.println ("Enter age. " ); // Age of Dog
...
}
答案 1 :(得分:0)
我假设您正在尝试scan
您自己的输入。
System.out.println ( " Spot "); // this only prints the string
name = scan.nextLine(); // this is waiting for you to enter input and assings it to "name"
System.out.println (" 5 " ); // same here
age= scan.nextInt(); // and here
您会在打印后看到需要输入将分配给变量的输入。您无法在输出的打印件上使用scan
。