JAVA一問題

It is difficult to make a budget that spans several years, because prices are not stableIf your company needs 200 pencils per year, you cannot simply use this year\'s priceas the cost of pencils two years from now. Because of inflation, the cost is likely tobe higher than it is today>Write a program to gauge the expected cost of an item ina specified number of years. The program asks for the cost of the item, the numberof years from now that the item will be purchased, and the rate of inflation. The program should then convert the percent to a fraction, such as 0.056, and should use a loop to estimate the price adjusted for inflation.煩請高手幫忙解答

2 個解答

評分
  • 1 0 年前
    最佳解答

    先將題目重謄一次 It is difficult to make a budget that spans several years, because prices are not stable. If your company needs 200 pencils per year, you cannot simply use this year's price as the cost of pencils two years from now. Because of inflation, the cost is likely to be higher than it is today. Write a program to gauge the expected cost of an item in a specified number of years. The program asks for the cost of the item, the number of years from now that the item will be purchased, and the rate of inflation. The program should then convert the percent to a fraction, such as 0.056, and should use a loop to estimate the price adjusted for inflation. 衡量橫跨多個年度的預算是件相當困難的事,因為價格不是固定的。假設你的公司每年需要200支鉛筆,你不能單就今年的價格來當做兩年後的鉛筆的成本。考量到通貨膨脹之故,兩年後的成本會比今年的稍微高些。 請實作一個程式,評估一物品於某年後的成本。程式首先詢問該物品的成本、於幾年後購買以及通貨膨脹率。程式必須將百分率轉換為小數,如0.056;必須利用迴圈來計算通膨調整後的價格。以下是我的做法,僅供參考。import java.io.*;public class ExpectedCost { public static void main(String[] args) throws Exception {  BufferedReader br = new BufferedReader(new InputStreamReader(System.in));  System.out.print("Cost of item: ");  double cost = Double.parseDouble(br.readLine());  System.out.print("Purchase year (from now): ");  int year = Integer.parseInt(br.readLine());  System.out.print("Inflation rate(%): ");  float rate = Float.parseFloat(br.readLine());  rate /= 100;    double exCost = cost;  for (int i = 0; i < year; i++) { // 利用迴圈來計算通膨調整後價格   exCost *= 1+rate;  }    System.out.println("After " + year + " year(s), the expected cost is " + exCost + " adjusted by inflation rate " + rate); }}

  • 1 0 年前

    這題不需要高手吧

    只要 cost * (1+inflation)^years 就解出來了

還有問題?馬上發問,尋求解答。