[英]why is my for loop not working
這是用於類分配的,即:
編寫一個程序,要求1.描述物料,購買年份,物料成本,要折舊的年數(估計壽命)以及折舊方法。 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
所以到目前為止這是我的代碼,當我輸入折舊方法時,我遇到的問題是,我的代碼似乎並不關心我輸入的是哪個折舊,而是僅顯示兩種折舊方法。 下一個問題是,當正確地增加年份時,成本,折舊金額和總折舊卻不增加,而我在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 );}
}}
我注意到的第一個問題是,您在if語句中犯了一個小錯誤。
if (input.equals("Straight-Line"));{
SingleDep(item, year, cost, life, input);}
if (input.equals("Double-Declining"));
{DoubleDep(item, year, cost, life, input);}
上面的行編碼錯誤。 您將分號放在if語句之后,這將導致下面的代碼塊同時執行。 刪除分號。
理解它的方式是這樣的:
if (a > 5) {
do stuff
}
僅當變量“ a”大於5時,此代碼才會“執行任務”,但:
if (a > 5); {
do stuff
}
編譯器將其讀取為:如果(a> 5),則以分號表示,表示停止。 這就是與if語句關聯的1行代碼,即空行。 因此,無論該語句是對還是錯,它都將跳到下一個可執行行,這是一個簡單的塊,中間帶有一些“執行任務”內容。
{ do stuff }
它將每次執行,因為實際上沒有與該特定語句塊相關的條件。
康回答了您問題的第一部分。
第二個問題似乎是整數除法。
要解決它變化
deprec=(1/life)*cost;
至
deprec=(1.0/life)*cost;
還有其他方法的除法 在Stackoverflow上已經多次解釋了整數除法。
1/2 == 0
由於整數除法),而
由於浮點除法,因此1.0/2 == 0.5
。
另外,您在main
方法中最多Scanner
有一個Scanner
對象。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.