#include <iostream>
#include <stdlib.h>
#include <ctime>
using namespace std;
int gameStatus=0;
void opPick(int &opChoice);
void judge(int myChoice,int opChoice);
int main(){
int myChoice,opChoice;
do{
cout<<"剪刀(1) 石頭(2) 布(3)";
cin>>myChoice;
opPick(opChoice);
judge(myChoice,opChoice);
}
while(gameStatus==0)
return 0;
}
void opPick(int &opChoice)
{
srand(time(NULL));
opChoice=rand()%3+1;
switch(opChoice){
case 1:
cout<<"電腦出剪刀"<<endl;
break;
case 2:
cout<<"電腦出石頭"<<endl;
break;
case 3:
cout<<"電腦出布"<<endl;
break;
}
return;
}
void judge(int myChoice,int opChoice)
{
if(myChoice==1 && opChoice==3){
cout<<"您獲勝了\a"<<endl;
gameStatus=1;
}
else if(myChoice==2 && opChoice==1){
cout<<"您獲勝了\a"<<endl;
gameStatus=1;
}
else if(myChoice==3 && opChoice==2){
cout<<"您獲勝了\a"<<endl;
gameStatus=1;
}
else if(myChoice==1 && opChoice==2){
cout<<"電腦獲勝了\a"<<endl;
gameStatus=2;
}
else if(myChoice==2 && opChoice==3){
cout<<"電腦獲勝了\a"<<endl;
gameStatus=2;
}
else if(myChoice==3 && opChoice==1){
cout<<"電腦獲勝了\a"<<endl;
gameStatus=2;
}
else {
cout<<"平手"<<endl;
}
return;
}
C語言?
do
{
y = f( x );
x--;
}
while ( x > 0 ) ;
參考
https://docs.microsoft.com/zh-tw/cpp/c-language/do-while-statement-c?view=msvc-160
while結尾要加分號吧!