高效的编码实践C ++

时间:2016-02-03 19:27:08

标签: c++

我正在设置一个程序,它将创建一个随机数1-3,然后为该数字指定一个属性,即R,P,S(Rock,Paper或Scissors)。

然后我测试那个数字并将我的变量的值变成“Rock,Paper或Scissors”

有没有办法做到这一点并不是多余,只需输入每个场景。我假设它处理某种循环,但我不太确定。很抱歉,如果这些问题看起来很糟糕,因为我正在学习并试图找出最有效的方法来编写这些方案。

如果您发现任何编码不佳,请告诉我们!

if((weap1 == "Rock") && (cpuWeap == "Scissors"))
    {
    cout<<"The CPU picked Scissors!"<<endl;
    cout<<"Congratulations you won!"<<endl;
    }
    else if ((weap1 =="Paper") && (cpuWeap == "Rock"))
    {
    cout<<"The CPU picked Rock!"<<endl;
    cout<<"Congratulations you won!"<<endl;
    }
    else if ((weap1 =="Scissors") && (cpuWeap == "Paper"))
    {
    cout<<"The CPU picked Paper!"<<endl;
    cout<<"Congratulations you won!"<<endl;
    }
    else if (weap1 == cpuWeap)
    {
    cout<<"You and the CPU picked "<<weap1<<"!"<<endl;
    cout<<"It's a draw!"<<endl;
    }
    else
    {
    cout<<"The CPU picked "<<cpuWeap<<"."<<endl;
    cout<<"Sorry, you lost!"<<endl;
    }       }

1 个答案:

答案 0 :(得分:2)

  1. 只需拨打srand一次。

  2. 在检查cpu的值之前,实际调用i函数。