。。。这个Java程序的loop没有执行,怎么回事?# JobHunting - 待字闺中
w*2
1 楼
题目是这样的:You are required to use loops, decision making constructs and
static functions to write your program. Depreciation to a salvage value of 0
For tax purposes an item may be depreciated over a period of several years,
n. With the straight line method of depreciation, each year the item
depreciates by 1/nth of its original value. With the double-declining method
of depreciation, each year the item is depreciates by 2/nths of its value
at the beginning of the year. (in the last year it is depreciated by its
value at the begining of the year). Write a program that 1. requests a
description of the item, the year of purchase, the cost of the item, the
number of years to be depreciated(estimated life), and the method of
depreciation. 2. Displays a depreciation schedule for the item similar to
the schedule shown below:
Description: Computer
Year of purchase: 1994
Cost : 2000
Estimated Life: 5
Method of depreciation: Double-Declining
Year Cost Dep. Amt Tot Dep.
1994 2,000.00 800.00 800.00
1995 1,200.00 480.00 1280.00
1996 720.00 288.00 1568.00
1997 432.00 172.80 1740.80
1998 259.20 259.20 2000.00
Description: Computer
Year of purchase: 1994
Cost : 2000
Estimated Life: 5
Method of depreciation: Straight-Line
Year Cost Dep. Amt Tot Dep.
1994 2,000.00 400.00 400.00
1995 2,000.00 400.00 800.00
1996 2,000.00 400.00 1,200.00
1997 2,000.00 400.00 1,600.00
1998 2,000.00 400.00 2,000.00
我自己写了代码,但是loop没有执行,怎么回事?
求解,不剩感激
import java.io.Console;
import java.util.Scanner;
public class Depre {
public static void SingleDep(int year,float cost, int life )
{
int n=1;
float X,Y,Z;
for (; n <=life ;n++)
{
year=year+1;
X=cost;
Y=(1/n)*X;
Z=n*Y;
}
}
public static void DoubleDep(int year, float cost, int life)
{
int n = 1;
float X=cost,Y,Z=0;
for ( ; n <=life ;n++)
{
year=year+1;
Y=(float)((2/n)*X);
Z=Y+Z;
X=cost-Y;
}
}
public static void main(String[] args)
{
String des;
int year,cost,life;
float X,Y,Z;
Scanner input= new Scanner(System.in);
System.out.println("Description: ");
des= input.nextLine();
System.out.println("Year of purchase: ");
year= input.nextInt();
System.out.println("Cost : ");
cost= input.nextInt();
System.out.println("Estimated Life: ");
life = input.nextInt();
if ( input.equals("Single Declining") )
SingleDep(year,cost,life);
else
DoubleDep(year,cost,life );
System.out.println("Year Cost Dep. Amt Tot Dep. ");
System.out.println("year X Y Z ");
}
}
static functions to write your program. Depreciation to a salvage value of 0
For tax purposes an item may be depreciated over a period of several years,
n. With the straight line method of depreciation, each year the item
depreciates by 1/nth of its original value. With the double-declining method
of depreciation, each year the item is depreciates by 2/nths of its value
at the beginning of the year. (in the last year it is depreciated by its
value at the begining of the year). Write a program that 1. requests a
description of the item, the year of purchase, the cost of the item, the
number of years to be depreciated(estimated life), and the method of
depreciation. 2. Displays a depreciation schedule for the item similar to
the schedule shown below:
Description: Computer
Year of purchase: 1994
Cost : 2000
Estimated Life: 5
Method of depreciation: Double-Declining
Year Cost Dep. Amt Tot Dep.
1994 2,000.00 800.00 800.00
1995 1,200.00 480.00 1280.00
1996 720.00 288.00 1568.00
1997 432.00 172.80 1740.80
1998 259.20 259.20 2000.00
Description: Computer
Year of purchase: 1994
Cost : 2000
Estimated Life: 5
Method of depreciation: Straight-Line
Year Cost Dep. Amt Tot Dep.
1994 2,000.00 400.00 400.00
1995 2,000.00 400.00 800.00
1996 2,000.00 400.00 1,200.00
1997 2,000.00 400.00 1,600.00
1998 2,000.00 400.00 2,000.00
我自己写了代码,但是loop没有执行,怎么回事?
求解,不剩感激
import java.io.Console;
import java.util.Scanner;
public class Depre {
public static void SingleDep(int year,float cost, int life )
{
int n=1;
float X,Y,Z;
for (; n <=life ;n++)
{
year=year+1;
X=cost;
Y=(1/n)*X;
Z=n*Y;
}
}
public static void DoubleDep(int year, float cost, int life)
{
int n = 1;
float X=cost,Y,Z=0;
for ( ; n <=life ;n++)
{
year=year+1;
Y=(float)((2/n)*X);
Z=Y+Z;
X=cost-Y;
}
}
public static void main(String[] args)
{
String des;
int year,cost,life;
float X,Y,Z;
Scanner input= new Scanner(System.in);
System.out.println("Description: ");
des= input.nextLine();
System.out.println("Year of purchase: ");
year= input.nextInt();
System.out.println("Cost : ");
cost= input.nextInt();
System.out.println("Estimated Life: ");
life = input.nextInt();
if ( input.equals("Single Declining") )
SingleDep(year,cost,life);
else
DoubleDep(year,cost,life );
System.out.println("Year Cost Dep. Amt Tot Dep. ");
System.out.println("year X Y Z ");
}
}