简体   繁体   English

为什么我的for循环不起作用

[英]why is my for loop not working

This is for a class assignment, which is: 这是用于类分配的,即:

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. 编写一个程序,要求1.描述物料,购买年份,物料成本,要折旧的年数(估计寿命)以及折旧方法。 2. Displays a depreciation schedule for the item similar to the schedule shown below: 2.显示物料的折旧时间表,类似于以下所示的时间表:

Description: Computer Year of purchase: 1994 Cost : 2000 Estimated Life: 5 Method of depreciation: Double-Declining Year Cost Dep. 描述:计算机购买年:1994成本:2000估计寿命:5折旧方法:年度成本双下降。 Amt Tot 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 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. 描述:计算机购买年:1994成本:2000估计寿命:5折旧方法:直线年成本部门。 Amt Tot 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 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

so here is my code so far, the problem(s) im having is when I input method of deprciation,my code doesnt seem to care which one or what I input, it just displays both methods of depreciation. 所以到目前为止这是我的代码,当我输入折旧方法时,我遇到的问题是,我的代码似乎并不关心我输入的是哪个折旧,而是仅显示两种折旧方法。 Next problem is that while the year correctly gets incremented, the cost, depreciation amoutn and total depreciation dont, and i have equations for them in my for loop, which makes me confused. 下一个问题是,当正确地增加年份时,成本,折旧金额和总折旧却不增加,而我在for循环中有针对它们的方程式,这使我感到困惑。 any help for advice would be appreciated. 任何帮助的建议,将不胜感激。

    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 );}


    }}

Your first problem that I notice is that you made a small mistake with your if-statements. 我注意到的第一个问题是,您在if语句中犯了一个小错误。

if (input.equals("Straight-Line"));{
    SingleDep(item, year, cost, life, input);}
if (input.equals("Double-Declining"));  
{DoubleDep(item, year, cost, life, input);}

The line above is coded incorrectly. 上面的行编码错误。 You're putting semicolons after the if-statements, which cause the code blocks below to BOTH execute. 您将分号放在if语句之后,这将导致下面的代码块同时执行。 Remove the semicolons. 删除分号。

The way to understand it is like this: 理解它的方式是这样的:

if (a > 5) {
    do stuff 
}

This code will "do stuff" if and only if the variable 'a' is greater than 5, BUT: 仅当变量“ a”大于5时,此代码才会“执行任务”,但:

if (a > 5); {
    do stuff 
}

The compiler will read that as: if (a > 5), then semicolon, meaning stop. 编译器将其读取为:如果(a> 5),则以分号表示,表示停止。 And that's your 1 line of code associated with the if-statement, an empty line. 这就是与if语句关联的1行代码,即空行。 So whether the statement is true or false, it will skip to the next executable line, which is a simple block with some "do stuff" stuff in the middle. 因此,无论该语句是对还是错,它都将跳到下一个可执行行,这是一个简单的块,中间带有一些“执行任务”内容。

{ do stuff }

Which it will execute everytime because there is actually no associated conditional to THAT particular statement block. 它将每次执行,因为实际上没有与该特定语句块相关的条件。

Kon answered the first part of your question. 回答了您问题的第一部分。

The second problem seems to be integer division. 第二个问题似乎是整数除法。

To fix it change 要解决它变化

deprec=(1/life)*cost;

to

deprec=(1.0/life)*cost;

and the division in your other method also. 还有其他方法的除法 Integer division has been explained many times on Stackoverflow. 在Stackoverflow上已经多次解释了整数除法。

1/2 == 0 because of Integer division, while 1/2 == 0由于整数除法),而

1.0/2 == 0.5 because of floating point division. 由于浮点除法,因此1.0/2 == 0.5


Also, you don't more than one Scanner object in your main method. 另外,您在main方法中最多Scanner有一个Scanner对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM