首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >if else程序切换程序

if else程序切换程序
EN

Stack Overflow用户
提问于 2010-10-09 14:12:14
回答 3查看 1.8K关注 0票数 1

//我的程序太长并且有错误..我想让它变得简单和容易的read..how来改变它在开关程序??请帮帮我!=(

代码语言:javascript
复制
#include <iostream>

using namespace std;

int main ()
{
    int movie1;
    int movie2;
    int movie3;
    int seats;
    int price;
    int select;
    char response;

    cout<<"______Wellcome to Strawberry Gold Cinema____\n"
        <<"Now you are booking a ticket cinema and please choose your movie... ";
        cout << "\nPress 1 for= Harry Potter \n"
            << "Press 2 for = Iron Man\n"
            <<"Press 3 for = Romeo and Juliet\n";
        cout<<"Enter your choice > ";
        cin>>select;


 if (select ==1 )

 {


     cout<<"The price of the ticket per seat is RM10.00\n"
        <<"Please enter the number of seat ";
        cin>>seats;


        if (seats<=30)

        {
            price = 10 * seats;

            cout<<"The total price is RM"<<price<<endl;
        }

        else if (seats>=30)

        {
            cout<<"The movie is full.\n"
            <<"Would like to choose another movie??\n"
            <<"Section Y or N \n";
            cout<<"Enter your choice = ";
            cin>>response;
        }



        if ( toupper( response ) == 'Y' )
        {
        cout <<"Please choose movie 2 or movie 3\n"
        <<"Enter your choice ";
        cin>>select;




        if (select == 2)
        {
         cout<<"The price of the ticket per seat is RM11.00\n"
           <<"Please enter the number of seat";
        cin>>seats;


        if (seats<=30)

        {
            price = 11 * seats;

            cout<<"The total price is RM"<<price<<endl;
        }

        }
        else if (seats>=30)

        {
            cout<<"The movie is full.\n"
            <<"Would like to choose another movie??\n"
            <<"Section Y or N \n";
            cout<<"Enter your choice = ";
            cin>>response;

        }
            if ( toupper( response ) == 'Y' )


            {
             cout<<"The price of the ticket per seat is RM13.00\n"
             <<"Please enter the number of seat";
            cin>>seats;


        if (seats<=30)

        {
            price = 13 * seats;

            cout<<"The total price is RM"<<price<<endl;
        }

        else if (seats>=30)

        {
            cout<<"The movie is full.\n"
            <<"Next movie in 5 hours...."<<endl;
        }

else
        cout << "Next movie in 5 hours.\n";
        }
        }
        }

//for 2

 if (select ==2 )

 {


     cout<<"The price of the ticket per seat is RM11.00\n"
        <<"Please enter the number of seat";
        cin>>seats;


        if (seats<=30)

        {
            price = 11 * seats;

            cout<<"The total price is RM"<<price<<endl;
        }

        else if (seats>=30)

        {
            cout<<"The movie is full.\n"
            <<"Would like to choose another movie??\n"
            <<"Section Y or N \n";
            cout<<"Enter your choice = ";
            cin>>response;
        }



        if ( toupper( response ) == 'Y' )
        {
        cout <<"Please choose movie 1 or movie 3\n"
        <<"Enter your choice ";
        cin>>select;




        if (select == 2)
        {
         cout<<"The price of the ticket per seat is RM10.00"
           <<"Please enter the number of seat";
        cin>>seats;


        if (seats<=30)

        {
            price = 10 * seats;

            cout<<"The total price is RM"<<price<<endl;
        }

        }
        else if (seats>=30)

        {
            cout<<"The movie is full.\n"
            <<"Would like to choose another movie??\n"
            <<"Section Y or N \n";
            cout<<"Enter your choice = ";
            cin>>response;

        }
            if ( toupper( response ) == 'Y' )


            {
             cout<<"The price of the ticket per seat is RM13.00\n"
             <<"Please enter the number of seat";
            cin>>seats;


        if (seats<=30)

        {
            price = 13 * seats;

            cout<<"The total price is RM"<<price<<endl;
        }

        else if (seats>=30)

        {
            cout<<"The movie is full.\n"
            <<"Next movie in 5 hours...."<<endl;
        }
else
        cout << "Next movie in 5 hours.\n";

        }
        }
        }



//for seat 3

 if (select ==3 )

 {


     cout<<"The price of the ticket per seat is RM13.00\n"
        <<"Please enter the number of seat";
        cin>>seats;


        if (seats<=30)

        {
            price = 13 * seats;

            cout<<"The total price is RM"<<price<<endl;
        }

        else if (seats>=30)

        {
            cout<<"The movie is full.\n"
            <<"Would like to choose another movie??\n"
            <<"Section Y or N \n";
            cout<<"Enter your choice = ";
            cin>>response;
        }



        if ( toupper( response ) == 'Y' )
        {
        cout <<"Please choose movie 1 or movie 2\n"
        <<"Enter your choice ";
        cin>>select;




        if (select == 1)
        {
         cout<<"The price of the ticket per seat is RM10.00\n"
           <<"Please enter the number of seat";
        cin>>seats;


        if (seats<=30)

        {
            price = 10 * seats;

            cout<<"The total price is RM"<<price<<endl;
        }

        }
        else if (seats>=30)

        {
            cout<<"The movie is full.\n"
            <<"Would like to choose another movie??\n"
            <<"Section Y or N \n";
            cout<<"Enter your choice = ";
            cin>>response;

        }
            if ( toupper( response ) == 'Y' )


            {
             cout<<"The price of the ticket per seat is RM12.00\n"
             <<"Please enter the number of seat";
            cin>>seats;


        if (seats<=30)

        {
            price = 12 * seats;

            cout<<"The total price is RM"<<price<<endl;
        }

        else if (seats>=30)

        {
            cout<<"The movie is full.\n"
            <<"Next movie in 5 hours...."<<endl;
        }
         else
        cout << "Next movie in 5 hours.\n";
        }
        }
        }






 return 0;

}
EN

回答 3

Stack Overflow用户

发布于 2010-10-09 14:19:53

根据经验,每当您发现自己复制和粘贴了超过一到两行代码时,都应该停下来思考一下,将这些代码重构为一个函数是否会更干净。

在这种情况下,您可以创建一个do_movie_specific_stuff()函数,该函数将三个电影之间不同的数据作为其参数(如果三个切换情况之间有任何不同的话)。那么switch语句中的每个case都是一个单独的语句:使用正确的参数调用该函数。

票数 4
EN

Stack Overflow用户

发布于 2010-10-09 14:26:37

我建议把你的逻辑放到一个循环中,如果你熟悉的话,把你的逻辑分解成函数,或者可能的类。类似这样的东西(这是未编译/未测试的半伪代码...只是为了给你一个想法):

代码语言:javascript
复制
bool done = false;
do
{
    cout << "Please choose a move (1, 2, or 3)" << endl;
    cin >> movieId;

    double price = getMoviePrice(movieId);
    cout << "The price of the movie is: " + price << endl;

    cout << "How many seats" << endl;
    cin >> seats;

    int availableSeats = getAvailableSeats(movieId);

    // and so on...  If the user indicates they want to quit, just set done to true!

} while (!done)

如果你需要跟踪用户已经尝试过的电影,你可以这样做,你只需要在某个地方跟踪它,并在逻辑中处理它。

票数 1
EN

Stack Overflow用户

发布于 2010-10-09 14:58:32

您所展示的代码是一个很好的例子,说明了纯线性代码的问题:它就是不能工作。

提示:我会创建三个函数来处理每个电影,或者更好地,创建一个通用函数来处理所有电影,它将从某个类中获取数据:

代码语言:javascript
复制
class Movie {
  std::wstring name;
  int seats;
  int soldSeats;
  int pricePerSeat;
};
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3895908

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档