在FOR或WHILE循环中挣扎

时间:2014-10-01 13:29:44

标签: java loops for-loop while-loop computer-science

我是编程新手,对如何多次打印代码感到困惑。有人可以帮我解决这个问题吗?

/**Description: Write a program to compute the yearly depreciation for an item whose
 purchase price, salvage value, and expected years of service are entered by the user. 
 Construct the program     so it will run four times before it terminates*/

在此之后,我不知道如何让程序多次打印。

{
    public static void main(String[] args)throws IOException
    {

BufferedReader userin = new BufferedReader (new InputStreamReader(System.in));
        String inputData;

        double price;
        double salvageValue; 
        int years;

        System.out.println("Run #1");
        System.out.print("Enter Price ");
        inputData = userin.readLine();
        price = Double.parseDouble( inputData );
        System.out.print("Enter Salvage Value ");
        inputData = userin.readLine();
        salvageValue = Double.parseDouble( inputData );
        System.out.print("Enter Estimated Life in years ");
        inputData = userin.readLine();
        years = Integer.parseInt( inputData );
        double depreciation = (price - salvageValue) / years;
        double depreciationRounded = Math.round(depreciation * 100.0) / 100.0;
        System.out.println("Annual Depreciation " + depreciationRounded + "\n");
    }
}

/*Sample Output:

Run #1

Enter Price 250.00

Enter Salvage Value 35.00

Enter Estimated Life in years 8

Annual Depreciation 26.88
*/

3 个答案:

答案 0 :(得分:1)

你想要迭代4次....所以简单地将逻辑封装在for循环中

for(int counter=0; counter< 4; counter++) {
   // logic goes here
}

这是如何运作的:

步骤1:当最初遇到for循环时,运行for循环中3个代码段的第一部分,如果在此声明变量,则它的范围仅在循环内。

步骤2:运行for循环代码的第二部分并计算为&#34; true&#34;或&#34;假&#34;如果它的计算结果为false,则退出for循环。

步骤3:for循环中的代码是在变量(int this counter)中执行的,如果需要在循环中可用(在你的情况下,我认为你不需要循环中的变量来强制执行4迭代)

步骤4:运行for循环代码的第3部分。

步骤5:重复步骤2-5,直到步骤2评估为假

希望有所帮助。

答案 1 :(得分:0)

看看这个:

public static void main(String[] args) {

    for (int i = 1; i <= 4; i++) {

        BufferedReader userin = new BufferedReader(new InputStreamReader(System.in));
        String inputData;

        double price;
        double salvageValue;
        int years;

        System.out.println("Run #" + i);
        System.out.print("Enter Price ");
        try {
            inputData = userin.readLine();
            price = Double.parseDouble(inputData);
            System.out.print("Enter Salvage Value ");
            inputData = userin.readLine();
            salvageValue = Double.parseDouble(inputData);
            System.out.print("Enter Estimated Life in years ");
            inputData = userin.readLine();
            years = Integer.parseInt(inputData);
            double depreciation = (price - salvageValue) / years;
            double depreciationRounded = Math.round(depreciation * 100.0) / 100.0;
            System.out.println("Annual Depreciation " + depreciationRounded + "\n");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    System.out.println("program termination");
    System.exit(0);
}

有用吗?

答案 2 :(得分:0)

看起来您需要执行以下操作:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Example {

    public static void main(String[] args) throws IOException {

        BufferedReader userin = new BufferedReader(new InputStreamReader(System.in));
        String inputData;

        double price;
        double salvageValue;
        int years;

        int numberOfSteps = 4;
        for (int i = 0; i < numberOfSteps; i++) {

            System.out.println("Run #"+i);
            System.out.print("Enter Price ");
            inputData = userin.readLine();
            price = Double.parseDouble(inputData);
            System.out.print("Enter Salvage Value ");
            inputData = userin.readLine();
            salvageValue = Double.parseDouble(inputData);
            System.out.print("Enter Estimated Life in years ");
            inputData = userin.readLine();
            years = Integer.parseInt(inputData);
            double depreciation = (price - salvageValue) / years;
            double depreciationRounded = Math.round(depreciation * 100.0) / 100.0;
            System.out.println("Annual Depreciation " + depreciationRounded
                    + "\n");
        }
    }
}

希望它有所帮助,

Clemencio Morales Lucas。