小試身手小範例:
迴圈練習,把下列網址所提供的歌詞利用迴圈寫出,是首很有趣的童謠。
http://www.99-bottles-of-beer.net/lyrics.html
http://www.youtube.com/watch?v=3KnpZYkTWno
程式碼
public class test {
public static void main(String[] args){
int nBeerNum = 99;
String sWord = "bottles";
while( nBeerNum > 0)
{
System.out.print( nBeerNum + " " + sWord + " of beer on the wall, " );
System.out.println( nBeerNum + " " + sWord + " of beer.");
System.out.print( "Take one down and pass it around, ");
nBeerNum--;
if(nBeerNum == 1 )
{
sWord = "bottle";
}
else if( nBeerNum == 0 )
{
System.out.println( "no more bottles of beer on the wall. \n");
System.out.print( "No more bottles of beer on the wall, ");
System.out.println("no more bottles of beer. ");
System.out.print("Go to the store and buy some more, ");
System.out.print("99 bottles of beer on the wall.");
break;
}
System.out.println( nBeerNum + " " + sWord + " of beer on the wall. \n");
}
}
}
運行畫面(僅擷取部分)
文章標籤
全站熱搜
留言列表