gpt4 book ai didi

java - 构造函数和用户输入遇到问题

转载 作者:行者123 更新时间:2023-12-02 02:31:57 24 4
gpt4 key购买 nike

我正在做一个小项目,但遇到了麻烦。它与创建类、构造函数等有关。对于类,所有数据字段都必须是私有(private)的。我还必须有两个构造函数,一个是默认的,一个是参数化的。这是类(class):

public class PetInfo {

private String petName = "na";
private boolean petType = true;
private String petBreed = "na";
private double petAge = 0;
private double petWeight = 0;
private String ownerName = "na";

public PetInfo(){}

public PetInfo(String name, boolean type, String breed, double age, double weight, String owner){
this.petName = name;
this.petType = type;
this.petBreed = breed;
this.petAge = age;
this.petWeight = weight;
this.ownerName = owner;
}

public String getName (){
return petName;
}

public void setName(String name){
petName = name;
}

public boolean getType(){
return petType;
}

public void setType(boolean type){
petType = type;
}

public String getBreed(){
return petBreed;
}

public void setBreed(String breed){
petBreed = breed;
}

public double getAge(){
return petAge;
}

public void setAge(double age){
petAge = age;
}

public double getWeight(){
return petWeight;
}

public void setWeight(double weight){
petWeight = weight;
}

public String getOwner(){
return ownerName;
}

public void setOwner(String owner){
ownerName = owner;
}
}

这是我的主要功能:

import java.util.Scanner;
public class Pp1_C00019540 {

public static void main(String[] args) {
PetInfo[] info = new PetInfo[5];
collectInfo(info);
}
public static void collectInfo(PetInfo[] info){
Scanner input = new Scanner(System.in);
for(int i = 0; i < info.length;i++){
System.out.print("Enter pet name: ");
}
}
}

所以它打印“输入宠物名称:”,但它不会让我输入名称。我尝试这样做:

    info[i] = new PetInfo(input.nextLine());

但它告诉我“构造函数 PetInfo.PetInfo(String, boolean, String, double,double, String) 不适用。实际参数和形式参数的长度不同。”我的类(class)有什么问题我没有发现吗?我测试了它,它似乎工作正常。

我并不是在寻找明确的答案,我很可能自己就能找到答案。我只是不确定发生了什么,尤其是当我向构造函数传递正确的参数时,这似乎会起作用。

最佳答案

基本上,您的代码尝试调用将单个字符串作为输入的 PetInfo 构造函数。但根据您拥有的代码,不存在这样的构造函数。您只有 PetInfo 的大型多参数构造函数。在调用构造函数之前,您需要多次调用扫描器进行输入。请参阅下面的代码:

private static void collectInfo(PetInfo[] info) {
Scanner input = new Scanner(System.in);
try {
for (int i = 0; i < info.length; i++) {
System.out.print("Enter pet name: ");
String petName = input.nextLine();
System.out.print("Enter pet type: ");
boolean petType = input.nextBoolean();
input.nextLine();
System.out.print("Enter pet breed: ");
String petBreed = input.nextLine();
System.out.print("Enter pet age: ");
double petAge = input.nextDouble();
input.nextLine();
System.out.print("Enter pet weight: ");
double petWeight = input.nextDouble();
input.nextLine();
System.out.print("Enter pet owner: ");
String petOwner = input.nextLine();
info[i] = new PetInfo(petName, petType, petBreed, petAge, petWeight, petOwner);
}
}
finally {
input.close();
}
}

希望上面的代码能够很好地说明我正在谈论的内容。另外,不要忘记在调用 nextBoolean()nextDouble() 后调用 input.nextLine()。最后,不要忘记关闭您的输入扫描器以避免资源泄漏。

希望有帮助。

关于java - 构造函数和用户输入遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46945599/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com