Answer:
Step-by-step explanation:
import javax.swing.JOptionPane;
public class BankBalanceDoWhile {
public static void main(String[] args) {
double investmentAmount = Double.parseDouble(JOptionPane.showInputDialog("Enter the investment amount:"));
int option;
do {
option = Integer.parseInt(JOptionPane.showInputDialog("Do you wish to see the balance of your investment after exactly a year?\\" +
"Enter 1 to see the balance for the next year, or 0 to exit."));
if (option == 1) {
// Calculate balance for the next year
double balance = calculateBalance(investmentAmount);
JOptionPane.showMessageDialog(null, "Balance after one year: $" + balance);
} else if (option != 0) {
JOptionPane.showMessageDialog(null, "Invalid input. Please enter either 1 or 0.");
}
} while (option != 0);
JOptionPane.showMessageDialog(null, "Program exited. Thank you!");
}
public static double calculateBalance(double investmentAmount) {
// Perform the calculation for the next year's balance
// You can modify this method based on your specific calculation logic
return investmentAmount * 1.05; // Assuming a 5% annual interest rate
}
}
In this program, the main() method prompts the user to enter the investment amount using JOptionPane.showInputDialog(). Then, inside the do-while loop, it asks the user if they want to see the balance for the next year using JOptionPane.showInputDialog(). If the user enters 1, it calculates the balance for the next year by calling the calculateBalance() method and displays it using JOptionPane.showMessageDialog(). If the user enters any value other than 1 or 0, it displays an error message. The loop continues until the user enters 0 to exit the program.
Note: The calculateBalance() method is a placeholder for your specific calculation logic. You can modify it based on your requirements.
Make sure to save the program with the filename "BankBalanceDoWhile.java" and compile and run it using a Java compiler or IDE.