If you add thousands separators, you get error messages. In
Java, you can't use your country's standard thousands separators
to represent numbers in the code.
If you add underscores, the code runs correctly. In Java, you
can use underscores as thousands separators to represent numbers
in the code.
If you add a currency symbol, you get an error message. In
Java, you can't use currency symbols to represent numbers in the
code.
When you change print to println, the
output becomes
You have $
1000050.22
in your account.
To make the program display "Now you have even more ..."
public class Millionaire {
public static void main(String[] args) {
double amountInAccount;
amountInAccount = 50.22;
amountInAccount = amountInAccount + 1000000.00;
System.out.print("You have $");
System.out.print(amountInAccount);
System.out.println(" in your account.");
System.out.println("Now you have even more! You have 2000000.00 in your account.");
}
}
public class Main {
public static void main(String[] args) {
int years;
int anniversaries;
years = 4;
anniversaries = years / 4;
System.out.println("Number of anniversaries: " + anniversaries);
years = 7;
anniversaries = years / 4;
System.out.println("Number of anniversaries: " + anniversaries);
years = 8;
anniversaries = years / 4;
System.out.println("Number of anniversaries: " + anniversaries);
}
}
Consider the last six statements in the main method:
System.out.println(i); // The value of i is 8
i++; // The value of i becomes 9
i = i++ + ++i; // See the "Statements and Expressions" sidebar...
9 11
// As an expression, the value of i++ is 9. As a statement, i++ adds 1 to i, making i be 10.
// Then, because i has become 10,
// As an expression, the value of ++i is 11. As a statement, ++1 adds 1 to i, but that doesn't matter because it's only temporary.
// As an expression, the value of i++ + ++i is 9 + 11, which is 20. So i is assigned the value 20.
System.out.println(i); // Prints 20
i = i++ + i++;
20 21
// As an expression, the value of the leftmost is i++ is 20. As a statement, the leftmost i++ adds 1 to i, making i be 21.
// Then, because i has become 21,
// As an expression, the value of the rightmost i++ is 21. As a statement, the rightmost i++ adds 1 to i, but that doesn't matter because it's only temporary.
// As an expression, the value of i++ + i++ is 20 + 21, which is 41. So i as assigned the value 41.
System.out.println(i); // Prints 41