我在这里有一个问题,如果有人可以帮助我在这里会很好.这是我第一次使用这个程序,所以不要去评判.
#include <cstdlib>
#include <iostream>
using namespace std;
int throw1, throw2, throw3, throw4;
int bet1 = 100;
int bet2 = 300;
int bet3 = 500;
int bet=(bet1, bet2, bet3);
int deposit;
int account;
int main(){
int count = 0;
while(count < 3){
cin>>deposit;
while(deposit>5000 || deposit<0){ //Makes sure so that my deposit is between 0-5000
cout<<"deposit failed"<<endl;
cin>>deposit;
}
account = deposit;
cout<<"You have deposited" <<deposit<<"Swedish Krona"<<endl;
cout<<"and you have this much cash on your account"<<account<<"Swedish Krona"<<endl;
if (konto>499){ //Makes sure so that i have the money to bet, and if i dont have the money, i can just put in more
cout<<"please place your bet"<<endl;
cout<<"bet1=100, bet2=300, bet3=500"<<endl;
cin>>bet1;
cin>>bet2;
cin>>bet3;
account = (deposit - bet);
cout<<"you have this much cash on your account"<<account<<"Swedish Krona"<<endl;
}
else if(account>299){
cout<<"please place your bet"<<endl;
cout<<"bet1=100, bet=300"<<endl;
cin>>bet1;
cin>>bet2;
account =(deposit - bet);
cout<<"you have this much cash on your account"<<account<<"Swedish Krona"<<endl;
}
else if(account>99){
cout<<"please place your bet"<<endl;
cout<<"bet1=100"<<endl;
cin>>bet1;
cout<<"you have placed your bet"<<bet<<"Swedish Krona"<<endl;
}
while (account<100 || deposit>5000){
cout<<"insufficient funds"<<endl;
cin>>deposit;
account=deposit;
}
{
cout<<"Throw dice"<<endl;
srand(time(0));
Throw1 = rand() % 6 + 1;
Throw2 = rand() % 6 + 1;
Throw3 = rand() % 6 + 1;
Throw4 = rand() % 6 + 1;
cout<<"You rolled"<<Throw1<<endl;
cout<<"You rolled"<<Throw2<<endl;
cout<<"Computer rolled"<<Throw3<<endl;
cout<<"Computer rolled"<<Throw4<<endl;
}
}
count++;
system ("pause");
}
Run Code Online (Sandbox Code Playgroud)
所以这里的事情是,由于某种原因,我总是下注500,即使输入bet1或bet2,我也不知道如何解决这个问题.然后我的循环函数(int count 0; while(count < 3)count++)它开始无限循环而没有我按任何东西,即使我在简单的编码中使用相同的循环函数,只需键入一些cout <<它工作得很好,但当我在这段代码中使用它时,它会去流失,有谁知道为什么会发生这种情况,如果有人能够回答,我将不胜感激,谢谢先进.
int bet1 = 100;
int bet2 = 300;
int bet3 = 500;
int bet=(bet1, bet2, bet3)
Run Code Online (Sandbox Code Playgroud)
最后一行将按如下方式进行评估:100,300,500.逗号分隔表达式列表的结果将是最后一个值,即500.因此您的投注变量将始终设置为500.
您在下面的代码您的评论什么状态,(int count 0; while(count < 3)count++)看上去像一些奇怪的混合for和while循环.请再次检查您的C++教科书/在线教程,了解如何编写正确的循环.
在您显示的代码中,在while循环中,您不会修改count变量 - 因此如果在循环之前count为<3,它将永远循环.您的代码缩进确实具有误导性.我冒昧地重新格式化你的代码 - 现在你应该看到该count++语句实际上超出了你的主while循环!
当你想要做一些固定次数的事情时,建议使用一个for循环,这会让你更难忘记增量!