2014-10-01 65 views
-3

我是編程新手,對於如何讓代碼打印多次感到困惑。有人可以幫助我完成該計劃嗎?用FOR或WHILE循環掙扎

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

回答

1

你要重複4次....所以乾脆封裝邏輯在for循環中

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

這是如何工作:

第1步:當for循環遇到最初,for循環中的3個代碼段的第一部分是運行的,如果你在這裏聲明一個變量,它的作用域只在循環中。

步驟2:for循環代碼的第二部分運行,如果計算結果爲false,則評估爲「真」或「假」,for循環退出。

第3步:for循環中的代碼是在變量(int this counter)可用的情況下在循環中可用(在您的情況下,我不認爲您需要循環中的變量來執行4次迭代)

第4步:for循環代碼的第3部分運行。

第5步:重複步驟2-5,直到第2步計算結果爲假

希望有所幫助。

+0

它有助於更​​多地瞭解它如何工作,而不是僅僅看到它。所以謝謝你解釋一切。 – 2014-10-03 13:02:37

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

是否有幫助?

+0

非常感謝,這對我們有很大的幫助。我只在上個月開始編程,對我來說循環是相當新的,所以謝謝你的解釋,它幫助我更好地理解for-loops。 – 2014-10-03 13:00:55

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莫拉萊斯盧卡斯。

+0

非常感謝。我是一名視覺學習者,所以看看如何去做,對我更好地理解循環的概念很有幫助。感謝您的幫助。 – 2014-10-03 13:04:39

+0

非常歡迎,我很樂意幫助你! ;-) – 2014-10-03 13:23:04