如何让一个数字下降?

时间:2016-07-11 08:27:13

标签: c++

在这里,我制作了一个程序,询问用户的年龄和他们有多少钱。让我们举例说,例如用户认为他/她有500美元并以50美元购买一只球棒。如何让500美元降到450美元?

以下是代码:

#include <iostream>
using namespace std;

int main()
{
    int age;
int money;
cout << "How old are you?" << endl;
cin >> age;
if (age <= 12) {
    cout << "Not for kids. Sorry!" << endl;
}
else {
    cout << "How much money do you have?" << endl;
}
cin >> money;
if (money <= 50) {
    cout << "Sorry not enough" << endl;
}
else {
    cout << "Here are the items you can buy" << endl;
    cout << " a = $50 Bat\n b = $100 Beats\n c = $500 Xbox One\n d = $500 PS4\n";
}
int a;
int b;
int c;
int d;
if (cin >> a) {
    money -= 50; //This is the part where I tried to make the $500 go down
}
cout << "You have " << money << "left" << endl;

return 0;
}

这是我试图让500美元降到450美元的部分:

if (cin >> a) {
    money -= 50; //This is the part where I tried to make the $500 go down
}
cout << "You have " << money << "left" << endl;

return 0;
}

此外,如果有办法让我的程序输出与我的代码相同但更短,那也很棒!

3 个答案:

答案 0 :(得分:1)

您必须修改代码,使其具有逻辑性和可读性。 我是为你写的。

#include <iostream>
using namespace std;

int main()
{
int age, money;
int priceA = 50, priceB = 100, priceC = 500, priceD = 500;
char choice;

cout << "How old are you?" << endl;
cin >> age;
if (age <= 12) {
    cout << "Not for kids. Sorry!" << endl;
    return 0;
}

cout << "How much money do you have?" << endl;
cin >> money;
if (money <= 50) {
    cout << "Sorry not enough" << endl;
    return 0;
}

cout << "Here are the items you can buy" << endl;
cout << " a = $ " << priceA << " Bat" << endl;
cout << " b = $ " << priceB << " Bat" << endl;
cout << " c = $ " << priceC << " Bat" << endl;
cout << " d = $ " << priceD << " Bat" << endl;

cin >> choice;

switch (choice) {
  case 'a':
    if(money >= priceA) {
    money -= priceA;
    break;
    } else {
    cout << "You have not enough money!" << endl;
    return 0;
    }
  case 'b':
    if(money >= priceB) {
    money -= priceB;
    break;
    } else {
    cout << "You have not enough money!" << endl;
    return 0;
    }
  case 'c':
    if(money >= priceC) {
    money -= priceC;
    break;
    } else {
    cout << "You have not enough money!" << endl;
    return 0;
    }
  case 'd':
    if(money >= priceD) {
    money -= priceD;
    break;
    } else {
    cout << "You have not enough money!" << endl;
    return 0;
    }
  default:
    cout << "Wrong input" << endl;
    return 0;
 }

cout << "You have " << money << " left" << endl;

return 0;
}

答案 1 :(得分:0)

如果您使用角色表示用户输入的选择,请将其读入char

根据他们的选择发货:

char choice;
if (cin >> choice) {
  switch (choice) {
  case 'a':
    money -= 50;
    break;
  case 'b':
    money -= 100;
    break;
  case 'c':
    money -= 500;
    break;
  case 'd':
    money -= 500;
    break;
  default:
    std::cerr << "invalid choice";
  }
} else {
  std::cerr << "invalid choice";
}

答案 2 :(得分:0)

这是解决这个问题的正确方法。

#include <iostream>
using namespace std;

int main()
{
int age, money;
int priceA = 50, priceB = 100, priceC = 500, priceD = 500;
char choice;

cout << "How old are you?" << endl;
cin >> age;
if (age <= 12) {
    cout << "Not for kids. Sorry!" << endl;
    return 0;
}

cout << "How much money do you have?" << endl;
cin >> money;
if (money <= 50) {
    cout << "Sorry not enough" << endl;
    return 0;
}

cout << "Here are the items you can buy" << endl;
cout << " a = $ " << priceA << " Bat" << endl;
cout << " b = $ " << priceB << " Bat" << endl;
cout << " c = $ " << priceC << " Bat" << endl;
cout << " d = $ " << priceD << " Bat" << endl;

cin >> choice;

if(choice=='a' && money >=50)
{
   money-=50;
}
if(choice=='b' && money >=100)
{
   money-=100;
}
if(choice=='c' && money >=500)
{
   money-=500;
}
if(choice=='d' && money>=500)
{
   money-=500;
}
cout << "You have " << money << " left" << endl;

return 0;
}