使用for迴圈 #include <iostream> #include <cstdlib> using namespace std;
int main()
{
//定義一個整數變數
int i = 1 , j = 1 , product = 1;
for( i = 1 ; i < 10 ; i++){
//i * j = product
for( j = 1 ; j < 10 ; j++){
product = i * j ;
cout << " " << i;
cout << "*" << j ;
//如果 product < 10 則多加1個空格!!!
if( product < 10 ) cout << "= " << product ;
else cout << "=" << product ;
}
cout << endl;
}
return 0;
system("pause");
}
---------------------------------------------------------
使用do-while迴圈(包一個for迴圈)
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
//定義一個整數變數
int i = 1 , j = 1 , product = 1;
do{
//i * j = product
for( j = 1 ; j < 10 ; j++){
product = i * j ;
cout << " " << i;
cout << "*" << j ;
//如果 product < 10 多加1個空格
if( product < 10 ) cout << "= " << product ;
else cout << "=" << product ;
}
cout << endl;
i++;
} while ( i < 10 );
return 0;
system("pause");
}
----------------------------------------------------------------------
使用雙重do-while迴圈
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
//定義一個整數變數
int multiplicand = 1 , product = 1;
//multiplicand * multiplier = product
do{
int multiplier = 1;//重點!!!!!!
do{
product = multiplicand * multiplier ;
cout << " " << multiplicand;
cout << "*" << multiplier ;
//如果 product < 10 多加1個空格
if( product < 10 ) cout << "= " << product ;
else cout << "=" << product ;
multiplier++;
}while ( multiplier < 10 );
cout << endl;
multiplicand++;
}while ( multiplicand < 10 );
return 0;
system("pause");
}
全站熱搜
留言列表