JAVA

Monday, October 24, 2005

Homework 10-17

17Project 2.03 (1st ed.)


import javax.swing.JOptionPane;

public class Project2_03{

public static void main(String[] args){

String PurchaseString =
JOptionPane.showInputDialog("Enter purchase of price:");
double p = Double.parseDouble(PurchaseString);
String SalvageString =
JOptionPane.showInputDialog("Enter salvage value:");
double s = Double.parseDouble(SalvageString);
String YearsOfServiceString =
JOptionPane.showInputDialog("Enter expected number of years of service:");
double y = Double.parseDouble(YearsOfServiceString);
double d = (p-s)/y;
JOptionPane.showMessageDialog(null,"The yearly depreciation =" +d);
System.exit(0);
}
}
代表圖




Project 2.05 (1st ed.)


public class Project2_05{

public static void main(String[] args){

String PriceString=JOptionPane.showInputDialog("Enter price of item\n"+"(from 25 cents to a dollar, in 5-cent increments):");
int price=Integer.parseInt(PriceString);

System.out.println("You bought an item for "+price+" cents and gave me a dollar,\n"+"so your change is");

int change = 100 - price;
int quarter=0,dime=0,nickel=0;

if(change >= 25) {
quarter = change / 25;
change = change-25*quarter;
}
if(change>=10 && change <25){
dime = change/10;
change = change-10*dime;
}
if(change>=5 && change <10){
nickel = change/5;
change = change-5*nickel;
}
if(change<0){
System.out.println("Error");
}

System.out.println(quarter+" quarters,");
System.out.println(dime+" dimes,and");
System.out.println(nickel+" nickels.");
System.exit(0);
}
}
代表圖1
代表圖2

0 Comments:

Post a Comment

<< Home