我的任務需要顯示輸出的行號。教授建議我用計數器來做,因爲看到Java沒有簡單的方法來打印出當前的行號,我只是按照建議創建了一個計數器。下面的代碼如下:count incrementer is wrong
//Count Increment
for (count = 1; count<= 5; count++)
{
}
//Display information
System.out.println(count + "." + " " + "Street:"+ " " + streetName + " " + "#" + streetNumber);
System.out.println(count + "." + " " + "Total Rooms:"+ " " + numofRooms);
System.out.println(count + "." + " " + "Total Area:"+ " " + totalSqFt + " sq.ft");
System.out.println(count + "." + " " + "The price per Sq. Ft is " + "$" + priceperSqFt);
System.out.println(count + "." + " " + "The estimated property value is "+ "$" + estimatedPropertyvalue);
但是,輸出開始於六行計數器爲證明此:
6. Street: park avenue #44
6. Total Rooms: 5
6. Total Area: 2500.0 sq.ft
6. The price per Sq. Ft is $120.4
6. The estimated property value is $301000.0
去掉括號沒有幫助的。我怎樣才能得到行數正確的狀態1,2,3,4,5?
如有需要,請諮詢澄清!謝謝。
嗯,也許你應該從1開始,增量計數器*後*每一行打印? (這個帖子是個玩笑嗎?) –
@HotLicks:你評論中的「笑話」部分對SO相當陌生的人不太歡迎。也許你可以更有禮貌一點? –
Java是一種*程序*語言,也就是說執行語句並且按順序*更改值。所以當你爲(count ...){...}'執行時,它會在它之後執行(除非有其他的控制流程來改變序列,所以當你到達那個關閉的時候)已經增加到了6. –