C語言乘法的程式
(用C語言寫)
撰寫一個學習乘法的程式,程式會利用rand來產生兩個個位正整數,然後印出如以下的問題:
How much is 6 times 7?
在使用者將答案鍵入後程式會檢查,並依對錯印出不同的評語。
正確答案的評語:
Very good!
Excellent!
Good job!
Keep up the good work!
錯誤答案的評語:
No. Please try again.
Wrong. Try once more.
Don't give up!
No. Keep trying.
請使用1到4的亂數和switch結構對每個答案選擇適當的評語 (如圖一) ;錯的答案要再回答一次(如圖二)。
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 :
你是用C++寫嗎??
我沒學過C++耶~
看不太懂,
能不能用C語言寫?
謝謝~
3 個解答
- 達達Lv 51 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;
case 4:printf("No. Please try again.\n");break;
}
}
return 0;
}
- 匿名使用者1 0 年前
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
main()
{
int num1,num2,answer,response,x,y;
srand(time(NULL));
do{
num1=1+rand()%9;
num2=1+rand()%9;
printf("How much is %d times %d ?\n",num1,num2);
answer=num1*num2;
do{
printf("The answer is(-1 to end):");
scanf("%d",&response);
if(response==-1)
break;
if(answer!=response){
x=1+rand()%4;
switch(x){
case 1:printf("No.Please try again.\n\n");
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;
}
}
}
while(answer!=response);
if(answer==response){
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;
}
}
}
while(answer==response);
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 (雖然發生機率很低,但是還是要注意)