HW2.12
控制台:
1 import java.util.Scanner; 2 3 public class Solution 4 { 5 public static void main(String[] args) 6 { 7 Scanner input = new Scanner(System.in); 8 9 System.out.print("Enter balance and interest rate(e.g., 3 for 3%): "); 10 double balance = input.nextDouble(); 11 double interestRate = input.nextDouble(); 12 13 input.close(); 14 15 double interest = balance * interestRate / 1200; 16 17 System.out.println("The interest is " + interest); 18 } 19 }
对话框:
1 import javax.swing.JOptionPane; 2 3 public class Solution 4 { 5 public static void main(String[] args) 6 { 7 String balanceString = JOptionPane.showInputDialog(null, "Enter balance", 8 "Balance", JOptionPane.QUESTION_MESSAGE); 9 double balance = Double.parseDouble(balanceString); 10 11 String interestRateString = JOptionPane.showInputDialog(null, "Enter interest rate", 12 "Interest Rate", JOptionPane.QUESTION_MESSAGE); 13 double interestRate = Double.parseDouble(interestRateString); 14 15 double interest = balance * interestRate / 1200; 16 String output = "The interest is " + interest; 17 18 JOptionPane.showMessageDialog(null, output); 19 } 20 }