当物品价格上涨时保持价值
keep value when item costs more
为学校制作了一个糖果机程序。问题是,当一件东西比我拥有的要贵的时候,它仍然会把我的钱减少到零,但不会带走这个东西。
我的代码
#include "stdafx.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int money = 30;
int x = 15;
char input;
int japp = 5;
int daim = 7;
int cocacola = 10;
int fanta = 10;
cout << " Welcome to my candy machine" << endl;
cout << " choose one of the following items" << endl;
cout << endl;
cout << endl;
while (x != 22)
{
if (money <= 0)
{
cout << endl;
cout << "Your have no money left!! therefore you cant shop anymore " << endl;
cout << endl;
cout << endl;
money = 0;
}
cout << "Slott 1 JAPP " << japp << " 10 kronor" << endl;
cout << "Slott 2 Daim " << daim <<" 10 kronor"<< endl;
cout << "Slott 3 coca cola " << cocacola <<" 15 kronor "<< endl;
cout << "Slott 4 Fanta " << fanta <<" 15 kronor"<< endl;
cout << endl;
cout << endl;
cout << " you have " << money << " kronor left" << endl;
cout << endl;
cout << endl;
cout << "abort [A]" << endl;
cin >> input;
switch (input)
{
case '1':
if (japp <= 5)
{
cout << endl;
cout << endl;
japp--;
money -= 10;
}
if (japp <= 0)
{
cout << " there is no moore japp!!" << endl;
cout << endl;
japp = 0;
}
if (money <= -1)
{
japp++;
}
break;
case '2':
if (daim <= 7)
{
daim--;
money -= 10;
}
if (daim <= 0)
{
cout << " there is no more daim !!" << endl;
cout << endl;
daim = 0;
}
if (money <= -1)
{
daim++;
}
break;
case'3':
if (cocacola <= 10)
{
cocacola--;
money -= 15;
}
if (cocacola <= 0)
{
cout << "there is no more coke !!" << endl;
cout << endl;
cocacola = 0;
}
if (money <= -1)
{
cocacola++;
}
break;
case '4':
if (fanta <= 10)
{
fanta--;
money -= 15;
}
if (fanta <= 0)
{
cout << " there is no moore fanta !!" << endl;
cout << endl;
fanta = 0;
}
if (money <= -1)
{
fanta++;
}
break;
case 'A':
cout << " ending the game...." << endl;
return 0;
case 'a':
cout << " ending the game.... " << endl;
return 0;
default:
cout << " wrong option, pleas choose between option 1 - 4 " << endl;
cout << endl;
cout << endl;
break;
}
}
return 0;
}
感谢你对我的帮助
您的条件不对eg
代替
if(japp<=5 )
你应该做
if(japp>0 && money >=10) // since japp>0 to buy it also since each japp
//costs 10 money , so money should also be greater than 10. Same for other products.
还有你的病情
if(money <-1)
japp++;
和
if(jazz <= 0)
变得不必要
相关文章:
- 没有找到相关文章