Java 挣扎于FOR或WHILE循环

Java 挣扎于FOR或WHILE循环,java,loops,for-loop,while-loop,computer-science,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 f

我是编程新手,对如何让代码多次打印感到困惑。有人能帮我做这个项目吗

/**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
*/

您希望迭代4次。。。。因此,只需将逻辑封装在for循环中

for(int counter=0; counter< 4; counter++) {
   // logic goes here
}
for(int计数器=0;计数器<4;计数器++){
//逻辑就在这里
}
工作原理:

步骤1:最初遇到for循环时,将运行for循环内3个代码部分的第一部分,如果在此处声明变量,则其作用域仅在循环内

步骤2:运行for循环代码的第2部分,如果其计算结果为false,则将其计算结果为“true”或“false”。for循环退出

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

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

步骤5:重复步骤2-5,直到步骤2的计算结果为false

希望这能有所帮助。

看看这个:

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);
}
publicstaticvoidmain(字符串[]args){

对于(int i=1;i,您似乎需要执行以下操作:

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");
        }
    }
}
导入java.io.BufferedReader;
导入java.io.IOException;
导入java.io.InputStreamReader;
公开课范例{
公共静态void main(字符串[]args)引发IOException{
BufferedReader userin=新的BufferedReader(新的InputStreamReader(System.in));
字符串输入数据;
双倍价格;
双重抢救价值;
整数年;
int numberOfSteps=4;
对于(int i=0;i
希望有帮助


克莱门西奥·莫拉莱斯·卢卡斯。

非常感谢,这很有帮助。我上个月才开始编程,循环对我来说是相当新的,所以谢谢你的解释,它帮助我更好地理解循环。它比仅仅看到它有助于更好地理解它的工作原理。所以感谢你解释一切。非常感谢你我是一名视觉学习者,因此了解如何做有助于我更好地理解循环的概念。谢谢你的帮助。