java - 无法使用 getName 从数组中获取要显示的名称
问题描述
我目前正在使用 java,但还没有掌握一切,所以请耐心等待。我一直试图让我的名字出现在输出菜单上,但我的代码中一定遗漏了一些东西。这是我的代码,它有一个驱动程序类和客户端类:
import java.util.Scanner;
//This is TestBaby.java class
public class TestBaby {
// This is main() method, program execution start from here
public static void main(String[] args) {
// This is Scanner class object, it will help to take value from end user
Scanner scan = new Scanner(System.in);
//This is array object of Baby type
Baby[] babies = new Baby[4];
//This is for loop, it iterates 4 times for holding 4 Babies data
for(int i=0; i<4; i++) {
//a. Enter details for each baby (name and age) and thus populate the Baby array
System.out.println("Enter details of " + "Baby " +(i+1));
if(i!=0)
scan.nextLine();
System.out.println("Enter name: ");
String name = scan.nextLine();
System.out.println("Enter age: ");
int age = scan.nextInt();
//This is Baby class object*/
Baby baby = new Baby();
//Set name in Baby class object whatever user entered
baby.setName(name);
//Set age in Baby class object whatever user entered
baby.setAge(age);
//Store current Baby class object into babies array
babies[i] = baby;
}//for end
//b. Output the details of each baby from the array (name and age)
int i=0;
for(Baby baby : babies) {
System.out.println("Baby name: " + baby.getName());
System.out.println("Baby age: " + baby.getAge());
i++;
}
//c. Calculate and display the average age of all babies in the array
double averageAge = averageAge(babies);
System.out.println("Average age of all babies: "+averageAge);
//d. Determine whether any two babies in the array are the same
for(i=0; i<babies.length; i++) {
for(int j=i+1; j<babies.length; j++) {
System.out.println(babies[i].getName()+ " and " + babies[j].getName() + " are equal? " + babies[i].equals(babies[j]));
}//inner for loop
}//outer for loop
}//main() end
//This is averageAge() method, it will calculate average age of Babies
public static double averageAge(Baby[] babies) {
//These are local variables of averageAge() method
int size = babies.length;
double averageAge = 0.0;
//This is for loop, it will calculate total age of babies
for(int i=0; i<size; i++) {
averageAge = averageAge + babies[i].getAge();
}//for end
//return average age to caller of this method
return averageAge/size;
}//averageAge() end
}// TestBaby end
//This is Baby.java class
public class Baby {
//These are instance variable of Baby.java class
private String name;
private int age;
//This is default constructor
public Baby() {
name = "Baby";
age = 1;
}
//This is parameterized constructor will take two parameters, a string to set the name and an integer to set the age
public Baby(String name, int age) {
this.name = name;
this.age = age;
}
//Supply methods for setting the name, setting the age, getting the name and getting the age.
public String getName() {
return name;
}
public void setName(String name) {
//The set method for the name instance variable should ensure that the input is not empty or contain whitespaces (otherwise set a default value)
if (name.trim().isEmpty()){
this.name = "Baby";
this.name = name;
}
}
public int getAge() {
return age;
}
public void setAge(int age) {
//The set method for the age instance variable should validate the input to be between 1 and 4 inclusive (otherwise set a default value).
if (age >= 1 && age <= 4) {
this.age = age;
} //if end
else {
this.age = 1;
}//else end
}// setAge() end
//Give Java code for an equals method for the Baby class.
@Override
public boolean equals(Object obj) {
//This is type casting
Baby baby = (Baby)obj;
//Compare name and age of Baby, if both are same then return true otherwise return false
if(this.age == baby.age && this.name.equalsIgnoreCase(baby.name))
return true;
return false;
}//equals() end
}// Baby end
请先尝试代码,以便您理解我的意思,以防它不清楚
解决方案
Baby.setName(String name)
仅在name
为空时设置名称。你的方法:
public void setName(String name) {
if (name.trim().isEmpty()){
this.name = "Baby";
this.name = name;
}
}
尝试这个:
public void setName(String name)
{
if (name == null)
{
// Use default from constructor
return;
}
String n = name.trim();
if (!n.isEmpty())
{
this.name = n;
}
}
推荐阅读
- python - 在 Flask 分页上刷新页面后 URL 搜索参数丢失
- argo-workflows - argo 工作流 retryStrategy 覆盖
- excel - 用于返回具有多个单元格的每一行的部分文本搜索的第一个实例的公式
- java - 如何在Java中读取外部文件
- vue.js - 使用 Vue.js 计算屏幕大小
- reactjs - 如何在反应中将openweather数据从组件渲染到子组件?
- google-sheets - 谷歌表格查询日期/时间范围
- php - 如何在带有 Laravel 8.x 的 Blade 中使用特定参数检查 Route 是否存在?
- exception - java.sql.SQLException:参数索引超出范围(1>参数数量,即0)。这是我的代码,我无法连接到数据库?
- python - 进入 3d 对象时 3d 投影中断