Java 打印空数组
当我在程序中添加一个新行程,并选择打印所有行程时,它会输出一个空信息,但索引号是正确的 当我添加两次行程时: 我期望的是: 它的输出: 这是我的密码Java 打印空数组,java,Java,当我在程序中添加一个新行程,并选择打印所有行程时,它会输出一个空信息,但索引号是正确的 当我添加两次行程时: 我期望的是: 它的输出: 这是我的密码 有很多话要说。我会尽量集中精力在基本问题上 使用单个扫描仪并更换: System.out.println("Name:"); String Name = textinput.nextLine(); System.out.println("MobileNo:"); String MobileNo = inpu
有很多话要说。我会尽量集中精力在基本问题上 使用单个扫描仪并更换:
System.out.println("Name:");
String Name = textinput.nextLine();
System.out.println("MobileNo:");
String MobileNo = input.next();
作者:
使用两个扫描仪很容易出错,我只使用nextLine,因为next和nextLine不应该与同一个扫描仪混合使用,以防止混合使用时产生一些副作用
在PrintTripInfo方法中,使该方法不是静态的,并删除您声明的Trip实例,因为您在此处调用该方法时已经处于Trip实例的上下文中:
for(int t=0; t<Trip.totalReservations; t++){
trip[t].printTripInfo();
}
你应该这样写:
public static void printTripInfo(){
Trip M=new Trip();
M.getcustomerName();
M.getmobileNo();
System.out.println("Customer Name:"+customerName);
System.out.println("Phone Number:"+mobileNo);
}
public void printTripInfo(){
System.out.println("Customer Name:"+customerName);
System.out.println("Phone Number:"+mobileNo);
}
到
但是,当您尝试运行带有上述更改的代码时,它根本无法编译。因为您正试图从非静态方法访问静态字段customerName和mobileNo。因此,您还需要将customerName和mobileNo字段更改为非静态字段。您在这里介绍了很多代码。请把这个减少到一个数字。但我强烈建议您:开始遵循Java命名约定;重新考虑使你所有的旅行场地保持静止。你明白静态是什么意思吗?我想这可能就是问题所在……请帮个忙,学习如何使用调试器。越快越好。好吧,我做到了,并且使它们都是静态的,但是还有一个问题,那就是只打印最后的数组信息,这就是为什么我使它们成为非静态的,因为它起作用了!!!谢谢但它并没有改变扫描仪,因为我必须创建:you's Welcome他还需要将静态字段customerName和mobileNo更改为非静态字段,否则当他尝试添加多个条目时,他在数组中的旧条目将被更改。因此,在最后,他将获得所有具有相同customerName和mobileNo值的条目。当然。我忘了提了。谢谢你!!!!!很好的解释!!很乐意帮忙
System.out.println("Name:");
String Name = textinput.nextLine();
System.out.println("MobileNo:");
String MobileNo = textinput.nextLine();
for(int t=0; t<Trip.totalReservations; t++){
trip[t].printTripInfo();
}
public static void printTripInfo(){
Trip M=new Trip();
M.getcustomerName();
M.getmobileNo();
System.out.println("Customer Name:"+customerName);
System.out.println("Phone Number:"+mobileNo);
}
public void printTripInfo(){
System.out.println("Customer Name:"+customerName);
System.out.println("Phone Number:"+mobileNo);
}
public static void printAll() {
for(int t=0; t<Trip.totalReservations; t++){
trip[t].printTripInfo();
}
}
public static void printTripInfo(){
Trip M=new Trip();
M.getcustomerName();
M.getmobileNo();
System.out.println("Customer Name:"+customerName);
System.out.println("Phone Number:"+mobileNo);
}
public void printTripInfo(){
System.out.println("Customer Name:"+ this.getcustomerName());
System.out.println("Phone Number:"+ this.getmobileNo());
}