英文:
Return updated value in another method
问题
我有两种方法,在其中一个方法中更新变量的值,并且我想在第二个方法中调用更新后的值。
在我的 bookFirstClassSeats
方法中,我从可用座位数中减去了乘客人数。
numOfFirstClassSeats -= numOfFirstClassAdults;
我还为此定义了以下的 getter 和 setter 方法。
public int getNumOfFirstClassSeats() {
return numOfFirstClassSeats;
}
public void setNumOfFirstClassSeats(int numOfFirstClassSeats) {
this.numOfFirstClassSeats = numOfFirstClassSeats;
}
所以在第一个方法中,我调用了 get
和 set
方法。
public void bookFirstClassSeats() {
...
// 从可用座位数中减去乘客人数。
numOfFirstClassSeats -= numOfFirstClassAdults;
System.out.println("Available Seats: " + getNumOfFirstClassSeats());
}
System.out.println()
打印了正确的更新后的值,46。
在我的第二个方法中,我想要显示可用座位的数量,如下所示。
public void printAvailableSeats() {
System.out.println("First Class: " + getNumOfFirstClassSeats());
}
问题: 当我运行此功能时,我得到了相同的原始值,48。我理解为什么我没有得到更新后的值,但我还没有弄清楚如何调用它。
编辑: 为了澄清,这两个方法都在同一个类中。我还有另一个名为 BookingSystem.java
的类,我使用它来运行 main
方法,并从另一个类中调用上述的方法。
以下是我的 main
方法代码:
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String input;
do {
System.out.println("\n~ BOOKING SYSTEM ~");
System.out.println("------------------");
System.out.println("A. Book Seats");
System.out.println("B. View Available Seats");
System.out.println("X. Exit\n");
System.out.print("Select an option: ");
input = sc.nextLine();
if (input.length() > 1)
System.out.println("ERROR: You can only enter a single character!");
else {
input = input.toUpperCase();
switch(input) {
case "A":
new BookingReceipt().bookSeats();
break;
case "B":
new BookingReceipt().printAvailableSeats();
break;
case "X":
System.out.println("INFO: You have exited the booking system.");
break;
default:
System.out.println("ERROR: Invalid input!");
}
}
} while (input.equals("X") == false);
sc.close();
}
英文:
I have two methods where I update the value of a variable in one method, and I want to call the updated value in the second method.
// Instance variables
private int numOfFirstClassSeats = 48;
...
In my bookFirstClassSeats
method, I subtract the number of passengers from the number of seats available.
numOfFirstClassSeats -= numOfFirstClassAdults;
I also have the following getter and setter methods for it.
public int getNumOfFirstClassSeats() {
return numOfFirstClassSeats;
}
public void setNumOfFirstClassSeats(int numOfFirstClassSeats) {
this.numOfFirstClassSeats = numOfFirstClassSeats;
}
So in the first method, I have called the get
and set
method.
public void bookFirstClassSeats() {
...
// Subtract the number of passengers from the number of seats available.
numOfFirstClassSeats -= numOfFirstClassAdults;
System.out.println("Available Seats: " + getNumOfFirstClassSeats());
}
The System.out.println()
prints the correct, updated value, 46.
In my second method, I'd like to display the number of available seats as shown below.
public void printAvailableSeats() {
System.out.println("First Class: " + getNumOfFirstClassSeats();
}
Problem: When I run this feature I get the same original value, 48. I understand why I'm not getting the updated value, but I haven't figured out yet how to call it.
EDIT: To clarify, both of these methods are in the same class. I have another class called BookingSystem.java
, and I use it to run the main
method and call the methods above from another class.
Here is my main
method code:
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String input;
do {
System.out.println("\n~ BOOKING SYSTEM ~");
System.out.println("------------------");
System.out.println("A. Book Seats");
System.out.println("B. View Available Seats");
System.out.println("X. Exit\n");
System.out.print("Select an option: ");
input = sc.nextLine();
if (input.length() > 1)
System.out.println("ERROR: You can only enter a single character!");
else {
input = input.toUpperCase();
switch(input) {
case "A":
new BookingReceipt().bookSeats();
break;
case "B":
new BookingReceipt().printAvailableSeats();
break;
case "X":
System.out.println("INFO: You have exited the booking system.");
break;
default:
System.out.println("ERROR: Invalid input!");
}
}
} while (input.equals("X") == false);
sc.close();
}
答案1
得分: 0
好的,以下是翻译好的部分:
public void bookFirstClassSeats() {
...
// 从可用座位数中减去乘客人数。
numOfFirstClassSeats -= numOfFirstClassAdults;
setNumOfFirstClassSeats(numOfFirstClassSeats);
System.out.println("可用座位数:" + getNumOfFirstClassSeats());
}
请注意,面向对象的思维方式比死记硬背的getter和setter更进一步。拥有numOfFIrstClassSeats
字段的任何类也可能应该有book
方法。
英文:
well, you made that setter; call it!
For example:
public void bookFirstClassSeats() {
...
// Subtract the number of passengers from the number of seats available.
numOfFirstClassSeats -= numOfFirstClassAdults;
setNumOfFirstClassSeats(numOfFirstClassSeats);
System.out.println("Available Seats: " + getNumOfFirstClassSeats());
}
Note the object oriented mindset goes a little further than rote getters and setters. Whatever class has the numOfFIrstClassSeats
field should probably have the book
method, too.
答案2
得分: 0
我怀疑你正在创建一个包含字段 private int numOfFirstClassSeats = 48;
的类的新对象。因此,你正在获取默认值。你应该使用同一个对象,而不是创建一个新的,或者你可以将该类设置为单例模式,如果这是问题的话。如果你能分享一下你主调用方法的定义,问题会更清楚。
正如我怀疑的那样,你正在创建一个新的对象,只需创建一个 BookingReceipt
对象即可。
在循环开始之前创建 BookingReceipt。
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String input;
BookingReceipt receipt = new BookingReceipt();
do {
System.out.println("\n~ BOOKING SYSTEM ~");
System.out.println("------------------");
System.out.println("A. Book Seats");
System.out.println("B. View Available Seats");
System.out.println("X. Exit\n");
System.out.print("Select an option: ");
input = sc.nextLine();
if (input.length() > 1)
System.out.println("ERROR: You can only enter a single character!");
else {
input = input.toUpperCase();
switch(input) {
case "A":
receipt.bookSeats();
break;
case "B":
receipt.printAvailableSeats();
break;
case "X":
System.out.println("INFO: You have exited the booking system.");
break;
default:
System.out.println("ERROR: Invalid input!");
}
}
} while (input.equals("X") == false);
sc.close();
}
英文:
I suspect you are creating a fresh object of the class containing field private int numOfFirstClassSeats = 48;
. Hence, you're getting the default value. You should use the same object rather creating a new one or you can make that class singleton if this the problem. It would be clear if you can share your main calling method's definition.
As I suspected, you are creating fresh object just create BookingReceipt
object once.
Create BookingReceipt before do loop starts.
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String input;
BookingReceipt receipt = new BookingReceipt();
do {
System.out.println("\n~ BOOKING SYSTEM ~");
System.out.println("------------------");
System.out.println("A. Book Seats");
System.out.println("B. View Available Seats");
System.out.println("X. Exit\n");
System.out.print("Select an option: ");
input = sc.nextLine();
if (input.length() > 1)
System.out.println("ERROR: You can only enter a single character!");
else {
input = input.toUpperCase();
switch(input) {
case "A":
receipt.bookSeats();
break;
case "B":
receipt.printAvailableSeats();
break;
case "X":
System.out.println("INFO: You have exited the booking system.");
break;
default:
System.out.println("ERROR: Invalid input!");
}
}
} while (input.equals("X") == false);
sc.close();
}
专注分享java语言的经验与见解,让所有开发者获益!
评论