下面是代码:
3 import java.util.Scanner;
4
5 public class AccountTest
6 {
7 public static void main(String[] args)
8 {
9 Account account1 = new Account("Jane Green", 50.00);
10 Account account2 = new Account("John Blue", -7.53);
11
12 // display initial balance of each object
13 System.out.printf("%s balance: $%.2f%n",
14 account1.getName(), account1.getBalance());
15 System.out.printf("%s balance: $%.2f%n%n",
16 account2.getName(), account2.getBalance());
17
18 // create a Scanner to obtain input from the command window
19 Scanner input = new Scanner(System.in);
20
21 System.out.print("Enter deposit amount for account1: "); // prompt
22 double depositAmount = input.nextDouble(); // obtain user input
23 System.out.printf("%nadding%.2f to account1 balance%n%n",
24 depositAmount);
25 account1.deposit(depositAmount); // add to account1’s balance
26
27 // display balances
28 System.out.printf("%s balance: $%.2f%n",
29 account1.getName(),account1.getBalance());
30 System.out.printf("%s balance: $ %n%n",
31 account2.getName(), account2.getBalance());
32
33 System.out.print("Enter deposit amount for account2: "); // prompt
34 depositAmount = input.nextDouble(); // obtain user input
35 System.out.printf("%nadding %.2f to account2 balance%n%n",
36 depositAmount);
37 account2.deposit(depositAmount); // add to account2 balance
38
39 // display balances
40 System.out.printf("%s balance: $%.2f%n",
41 account1.getName(), account1.getBalance());
42 System.out.printf("%s balance: $ %n%n",
43 account2.getName(), account2.getBalance());
44 } // end main
45 } // end class AccountTest
结果:
Jane Green balance: $50.00
John Blue balance: $0.00
Enter deposit amount for account1: 25.53
adding 25.53 to account1 balance
Jane Green balance: $75.53
John Blue balance: $0.00
Enter deposit amount for account2: 123.45
adding 123.45 to account2 balance
Jane Green balance: $75.53
John Blue balance: $123.45
我的问题是 在第29和31行,怎么知道他的balance 是$75.53 (户口$50+存了$25.23)? 里面好像没有写$50+$25.23相加的编码啊? 为什么会知道最后得出来的数是$75.53?
3 import java.util.Scanner;
4
5 public class AccountTest
6 {
7 public static void main(String[] args)
8 {
9 Account account1 = new Account("Jane Green", 50.00);
10 Account account2 = new Account("John Blue", -7.53);
11
12 // display initial balance of each object
13 System.out.printf("%s balance: $%.2f%n",
14 account1.getName(), account1.getBalance());
15 System.out.printf("%s balance: $%.2f%n%n",
16 account2.getName(), account2.getBalance());
17
18 // create a Scanner to obtain input from the command window
19 Scanner input = new Scanner(System.in);
20
21 System.out.print("Enter deposit amount for account1: "); // prompt
22 double depositAmount = input.nextDouble(); // obtain user input
23 System.out.printf("%nadding%.2f to account1 balance%n%n",
24 depositAmount);
25 account1.deposit(depositAmount); // add to account1’s balance
26
27 // display balances
28 System.out.printf("%s balance: $%.2f%n",
29 account1.getName(),account1.getBalance());
30 System.out.printf("%s balance: $ %n%n",
31 account2.getName(), account2.getBalance());
32
33 System.out.print("Enter deposit amount for account2: "); // prompt
34 depositAmount = input.nextDouble(); // obtain user input
35 System.out.printf("%nadding %.2f to account2 balance%n%n",
36 depositAmount);
37 account2.deposit(depositAmount); // add to account2 balance
38
39 // display balances
40 System.out.printf("%s balance: $%.2f%n",
41 account1.getName(), account1.getBalance());
42 System.out.printf("%s balance: $ %n%n",
43 account2.getName(), account2.getBalance());
44 } // end main
45 } // end class AccountTest
结果:
Jane Green balance: $50.00
John Blue balance: $0.00
Enter deposit amount for account1: 25.53
adding 25.53 to account1 balance
Jane Green balance: $75.53
John Blue balance: $0.00
Enter deposit amount for account2: 123.45
adding 123.45 to account2 balance
Jane Green balance: $75.53
John Blue balance: $123.45
我的问题是 在第29和31行,怎么知道他的balance 是$75.53 (户口$50+存了$25.23)? 里面好像没有写$50+$25.23相加的编码啊? 为什么会知道最后得出来的数是$75.53?