我有一個關於循環的問題。我正在創建一個計算資產折舊的程序(它是未知的),我正在循環它以便運行4次。整個代碼看起來像這樣如何輸出循環代碼運行的迭代
import java.io.*;
import java.util.*;
class Program108b{
public static void main(String[] args){
int [] numbers = {1, 2, 3, 4};
for(int x: numbers){
Scanner kb = new Scanner(System.in);
System.out.print(" Enter price ");
double price = kb.nextDouble();
System.out.print("Enter salvage value ");
double salValue = kb.nextDouble();
System.out.print("Enter estimated life in years ");
int years = kb.nextInt();
double anws = ((price- salValue)/years);
System.out.print("Here is the depreciated value: " + (Math.round((anws)*100)/100.0));
}
}
}
我試圖使它說「跑#1」,然後在「運行#2」,然後在「運行#3」,然後在「運行#4」在頂部的代碼發生某些運行時。所以,現在的產量是
Enter price 2500
Enter salvage value 350
Enter estimated life in years 23
Here is the depreciated value: 93.48 Enter price 23
Enter salvage value
32
Enter estimated life in years 3
Here is the depreciated value: -3.0 Enter price 2
Enter salvage value 32
Enter estimated life in years 3
Here is the depreciated value: -10.0 Enter price 23
Enter salvage value 2
Enter estimated life in years 3
Here is the depreciated value: 7.0
(輸入是完全隨機的,我只是想顯示輸出是什麼) 我想使輸出看起來就像這樣:
run #1
Enter price 2500
Enter salvage value 350
Enter estimated life in years 23
Here is the depreciated value: 93.48
run #2
Enter price 23
Enter salvage value
32
Enter estimated life in years 3
Here is the depreciated value: -3.0
run #3
Enter price 2
Enter salvage value 32
Enter estimated life in years 3
Here is the depreciated value: -10.0
run #4
Enter price 23
Enter salvage value 2
Enter estimated life in years 3
Here is the depreciated value: 7.0
所以我問題是 - 我如何讓它說「運行#_」,並讓它每個循環更改爲「運行#1」,「運行#2」等,直到第四次運行?另外,如何從新行開始新運行(此代碼中的「輸入價格」部分)?
不會添加一個新行。 System.out.println的確如此。或者您也可以在打印字符串中添加「\ n」以手動添加新行。這應該可以幫助你清理乾淨。 – ergonaut