名稱:

星期一, 10月 24, 2005

Homework 2.5 2.7

import javax.swing.JOptionPane;
public class Homework2.5
{
public static void main(String[] args)
{
String purchasePrice=JOptionPane.showInputDialog("Enter the purchase price:");
double P=Double.parseDouble(purchasePrice);
String salvageValue=JOptionPane.showInputDialog("Enter the salvage value:");
double S=Double.parseDouble(salvageValue);
String numberOfYears=JOptionPane.showInputDialog("Enter the number of years:");
double Y=Double.parseDouble(numberOfYears);
double D=(P-S)/Y;
System.out.println("The yearly depreciation for the item = "+D);
}
}

Homework 2.7

import javax.swing.JOptionPane;
public class Homework 2.5
{
public static void main(String[] args)
{
String priceOfItem=JOptionPane.showInputDialog("Enter price of item\n (from 25 cents to a dollar, in 5-cent increment):");
int price=Integer.parseInt(priceOfItem);
System.out.println("You bought an item for "+price+" cents and gave me a dollar,\n so your change is");
int q=0,d=0,n=0;
int change=100-price;
if(change>=25)
{
q=change/25;
change=change%25;
}
if(change>=10&&change<25)
{
d=change/10;
change=change%10;
}
if(change>=5&&change<10)
{
n=change/5;
change=change%5;
}
System.out.println(q+" quarters,\n"+d+" dimes, and\n"+n+" nickels.");
}
}

0 Comments:

張貼留言

<< Home