# C語言乘法的程式

(用C語言寫)

How much is 6 times 7?

Very good!

Excellent!

Good job!

Keep up the good work!

Wrong. Try once more.

Don＇t give up!

No. Keep trying.

Enter -1 to end.

How much is 4 times 1? 4

Keep up the good work!

How much is 4 times 0?

Enter -1 to end.

How much is 2 times 7? 13

No. Keep trying.

How much is 2 times 7? 14

Very good!

How much is 9 times 5?

Atilla :

### 3 個解答

• 達達
Lv 5
1 0 年前
最佳解答

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

int main()

{

int A,B,key;

printf("Enter -1 to end.\n");

srand(time(NULL));

A=rand()%10;

B=rand()%10;

while(key!=-1)

{

printf("How much is %d times %d?",A,B);

scanf("%d",&key);

if(key==A*B&&key!=-1)

{

A=rand()%10;

B=rand()%10;

switch(rand()%4+1)

{

case 1:printf("Excellent!\n");break;

case 2:printf("Good job!\n");break;

case 3:printf("Keep up the good work!\n");break;

case 4:printf("Very good!\n");break;

}

}

else if(key!=-1)

switch(rand()%4+1)

{

case 1:printf("Wrong. Try once more.\n");break;

case 2:printf("Don’t give up!\n");break;

case 3:printf("No. Keep trying.\n");break;

}

}

return 0;

}

• 匿名使用者
1 0 年前

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

main()

{

srand(time(NULL));

do{

num1=1+rand()%9;

num2=1+rand()%9;

printf("How much is %d times %d ?\n",num1,num2);

do{

scanf("%d",&response);

if(response==-1)

break;

x=1+rand()%4;

switch(x){

break;

case 2:printf("Wrong. Try once more.\n\n");

break;

case 3:printf("Don't give up!\n\n");

break;

case 4:printf("No. Keep trying.\n\n");

break;

}

}

}

y=1+rand()%4;

switch(y){

case 1:printf("Very good!\n\n");

break;

case 2:printf("Excellent!\n\n");

break;

case 3:printf("Nice work!\n\n");

break;

case 4:printf("Keep up the good work!\n\n");

break;

}

}

}

system("pause");

return 0;

}

參考資料： 經驗
• 1 0 年前

我沒有設計防其他按鈕(非數字)，例如輸入英文或是其他字元

#include <iostream>

#include <math.h>

#include <time.h>

using namespace std;

int main(void);

int main(void){

int x, y, key;

int input;

bool getnumber = 1;

srand(time(NULL));

cout << "Enter -1 to end." << endl;

do{

if(getnumber){

x = rand()%9 + 1;

y = rand()%9 + 1;

}

key = rand()%4 + 1;

cout << "How much is " << x << " times " << y << " ? ";

cin >> input;

if(input == -1)

break;

else if(input == x*y){

getnumber = 1;

switch(key){

case 1:

cout << "Very good!" << endl;

break;

case 2:

cout << "Excellent!" << endl;

break;

case 3:

cout << "Good job!" << endl;

break;

default:

cout << "Keep up the good work!" << endl;

}

}else{

getnumber = 0;

switch(key){

case 1:

cout << "No. Please try again." << endl;

break;

case 2:

cout << "Wrong. Try once more." << endl;

break;

case 3:

cout << "Don’t give up!" << endl;

break;

default:

cout << "No. Keep trying." << endl;

}

}

}while(1);

// system("pause");

return 0;

}

2008-01-09 14:59:23 補充：

其實C 跟 C++ 沒有差很多，因為工作要用C++所以我習慣上也就用C++的方式回答

這裡面的寫法只有cout 跟 cin 這兩個妳不知道吧

其實就是printf 跟 scanf

2008-01-10 10:29:32 補充：

達達回答的也很正確，但是有個缺點，就是

while(key!=-1)

因為它只有宣告key卻沒有給起始值直接判斷是不是等餘-1

或許你會覺得這樣沒有錯，而且執行也不會錯，但是，對於我們程式設計師們來說這是個很嚴重的事情，因為有可能key在你沒設定起始值的時候它會剛剛好是 -1 (雖然發生機率很低，但是還是要注意)