精華區beta NTUCH-HW 關於我們 聯絡資訊
功能 用到的觀念 CODE #include<iostream>//標頭檔 #include<iomanip> #include<cmath> #include<cstdlib> #include<ctime> using std::time; using std::rand; using std::srand; using namespace std; int loto_number(int);//declare the prototype int main() { cout<<"this programe give you lottery number"<<endl; srand((unsigned)time(0));//make the table different all the time int num[9],j,k;//declare int variable and array. char ch;//declare char variable do{//do first and then check for(int i=0;i<7;i++)//run the loop { num[i]=loto_number(0);//appoint lottery number to num if(i>=0&&i<6)//0-5 are not special number { for(int j=0;j<i;j++)//run the loop to check whether it has same number while(num[i]==num[j])//run until they're different { num[i]=loto_number(i);//appoint a different number } cout<<num[i]<<endl;//show number and change new line cout<<"press enter to get the next number"<<endl;//show information system("pause");//stop for while } else if(i==6)//6 is a special number { for(int j=0;j<i;j++)//run until they're different while(num[i]==num[j])//appoint a different number { num[i]=loto_number(i);//appoint a different number } cout<<num[i]<<endl;//show number cout<<"this is a special number"<<endl;//show information } } system("pause"); system("cls"); cout<<"these numbers are the lottery number and good luck"<<endl; for(k=0;k<7;k++) { if(k==0) { cout<<"("<<num[k]<<","; } else if(k>0&&k<6) { cout<<num[k]<<","; } else if(k==6) { cout<<num[k]<<"(special number)"<<")"<<endl; } } cout<<"do you want to play again press y to start again; otherwise n"<<endl;//ask you to continue cin>>ch;//give choice if(ch=='n'&&ch=='N')//if ch equal to N or n stop { break;//stop } else//check you type right { while(ch!='Y'&&ch!='y'&&ch!='n'&&ch!='N')//if you type wrong show that you're wrong { cout<<"you type wrong"<<endl; cin>>ch;//type again } } system("cls");//clean the screen }while(ch!='n'&&ch!='N');//if ch equal to N or n, stop system("pause"); return 0; } int loto_number(int a)//declare the prototype { a=rand()%42+1;//mod 42 to get 1-42 return a;//give a to the function who call it } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.112.7.59