Abd*_*imi 2 java console for-loop
这是一个类赋值,它是:
编写一个程序,该程序要求描述项目,购买年份,项目成本,折旧年限(估计寿命)以及折旧方法.2.显示项目的折旧计划,类似于下面显示的计划:
说明:电脑购买年份:1994成本:2000预计寿命:5折旧方法:双倍降低年度成本部分.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
说明:电脑购买年份:1994成本:2000预计寿命:5折旧方法:直线年费用深度.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
所以这里是我的代码到目前为止,我所遇到的问题是当我输入deprciation的方法时,我的代码似乎并不关心我输入的是哪一个,它只显示两种折旧方法.接下来的问题是,当年正确增加时,成本,折旧和总折旧不会,并且我在for循环中有他们的等式,这让我感到困惑.任何建议的帮助将不胜感激.
package proj3;
import java.io.Console;
import java.util.Scanner;
public class depre {
public static void main(String [] args){
Scanner sc = new Scanner(System.in);
System.out.print("Enter item description: ");
String item=sc.nextLine();
Scanner sc1 = new Scanner(System.in);
System.out.print("What year was it purchased? ");
int year =sc1.nextInt();
Scanner sc2 = new Scanner(System.in);
System.out.print("How much did it cost? ");
int cost = sc2.nextInt();
Scanner sc3=new Scanner(System.in);
System.out.print("What is its estimated life? ");
int life=sc3.nextInt();
Scanner sc4= new Scanner(System.in);
System.out.print("What is the method of depreciation? Straight-Line or Double-Declining? ");
String input = sc4.nextLine();
if (input.equals("Straight-Line"));{
SingleDep(item, year, cost, life, input);}
if (input.equals("Double-Declining"));
{DoubleDep(item, year, cost, life, input);}
}
public static void SingleDep(String item, int year, int cost, int life, String input)
{
float price=cost;
float deprec;
int age;
float totaldeprec=0f;
System.out.printf("Year Cost Dep. Amt Tot Dep.\n");
for (int i = 0; i < life; i = i+1) {
age = year + i;
deprec=(1/life)*cost;
totaldeprec=deprec+totaldeprec;
price=(price-deprec);
System.out.printf("%2d %7.2f %7.2f %7.2f\n", age,price,deprec,totaldeprec);
}
}
public static void DoubleDep(String item, int year, int cost, int life, String input)
{ double price = cost;
double deprec;
int age;
double totaldeprec=0;
System.out.printf("Year Cost Dep. Amt Tot Dep.\n");
for (int i = 0; i < life; i = i+1) {
age = year + i;
deprec = (2/life) * cost;
totaldeprec =totaldeprec+ deprec;
price = price-deprec;
System.out.printf("%2d %7.2f %7.2f %7.2f\n", age,price, deprec, totaldeprec );}
}}
Run Code Online (Sandbox Code Playgroud)
我注意到你的第一个问题是你的if语句犯了一个小错误.
if (input.equals("Straight-Line"));{
SingleDep(item, year, cost, life, input);}
if (input.equals("Double-Declining"));
{DoubleDep(item, year, cost, life, input);}
Run Code Online (Sandbox Code Playgroud)
上面的行编码不正确.你在if语句之后加上分号,这会导致下面的代码块被执行.删除分号.
理解它的方法是这样的:
if (a > 5) {
do stuff
}
Run Code Online (Sandbox Code Playgroud)
当且仅当变量'a'大于5时,此代码才会"填充",但是:
if (a > 5); {
do stuff
}
Run Code Online (Sandbox Code Playgroud)
编译器将读取为:if(a> 5),然后是分号,表示停止.这是与if语句关联的一行代码,一行为空.因此,无论语句是真还是假,它都会跳到下一个可执行行,这是一个简单的块,中间有一些"做东西".
{ do stuff }
Run Code Online (Sandbox Code Playgroud)
它将每次执行,因为实际上没有与特定语句块相关的条件.
| 归档时间: |
|
| 查看次数: |
419 次 |
| 最近记录: |