我是編程新手,對於如何讓代碼打印多次感到困惑。有人可以幫助我完成該計劃嗎?用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
*/
它有助於更多地瞭解它如何工作,而不是僅僅看到它。所以謝謝你解釋一切。 – 2014-10-03 13:02:37