所以,我正在尝试继续我的长颈鹿计划(不要问)。我希望用户为他们的长颈鹿命名,这应该是长颈鹿的名字。例如:
System.out.println("Name your giraffe.");
类似的东西:
Giraffes.somenewvariabletype scannerexample.nextLine() = Giraffes.Giraffe(parameter, parameter)
答案 0 :(得分:5)
也许你想要这样的东西
public class Giraffe{
String name;
public Giraffe(String name){
this.name = name; // this set the name for your Giraffe object
}
public getName(){ // returns the name of the Giraffe object
return name;
}
}
然后你可以像这样使用它
Scanner scanner = new Scanner(System.in);
System.out.println("Name your giraffe.");
String name = scanner.nextLine(); // get user input for name
Giraffe giraffe = new Giraffe(name); // use the constructor above to set name
System.out.println(giraffe.getName()); // prints name of giraffe