2008年12月11日 星期四

simple java(十一)---application(7)

The interest rate of saving accounts varies with the balance of the account. Based on the following interest rate table, write an application that allows an user to input the balance of the account and display the interest rate.

solution:
import javax.swing.*;

public class range {

public static void main(String args[]) {

double balance,result1;

String input;

input=JOptionPane.showInputDialog("Enter the balance of account");

balance=Integer.parseInt(input);

if (balance<=1000)
result1 = 0.00;

else if(balance<=20000)
result1= 0.5;

else if(balance<=100000)
result1= 1.0;

else
result1= 1.25;
JOptionPane.showMessageDialog(null, "The interest rate is " + result1 + "%");
System.exit(0);}}
*************************
Write a program to let users input a value of month (1 to 12) and a year. According tothe input month, your program will return the number of days of the correspondingmonth.If a user input '2' to the month, your program should ask user to input a year to checkleap year.The output message of your program can return the short name of the input month, e.g.month is 2 then the output should be 'Feb'.
solution:
import javax.swing.JOptionPane;
public class MonthDays {
public static void main(String[] args) {
int month, days, year, leapday = 0;
String sMonth, sYear="";
sMonth = JOptionPane.showInputDialog("Enter a month");
month = Integer.parseInt(sMonth);
switch (month)
{
case 2:sYear = JOptionPane.showInputDialog("Enter a year");
year = Integer.parseInt(sYear);
leapday = (year % 4 == 0 && year % 100 != 0) (year % 400 == 0) ? 1: 0;
days = 28 + leapday;
break;
case 4:
case 6:
case 9:
case 11:
days = 30;
break;
default:
days = 31;
}
switch (month)
{
case 1:
sMonth = "Jan"; break;
case 2: sMonth = "Feb"; break;
case 3: sMonth = "Mar"; break;
case 4: sMonth = "Apr"; break;
case 5: sMonth = "May"; break;
case 6: sMonth = "Jun"; break;
case 7: sMonth = "Jul"; break;
case 8: sMonth = "Aug"; break;
case 9: sMonth = "Sep"; break;
case 10: sMonth = "Oct"; break;
case 11: sMonth = "Nov"; break;
case 12: sMonth = "Dec"; break;
}
if (month==2)
JOptionPane.showMessageDialog( null,"The number of days on month " + sMonth + " and year "+sYear+" is: "+days);
else
JOptionPane.showMessageDialog( null,"The number of days on month " + sMonth + " is: "+days);}}

沒有留言: