在下面的代码中,我需要关于switch case替代品的帮助,我的意思是我该如何避免切换并使用其他选项来执行代码。交换机的替代品
代码:
cout << " *********************Intensive Program 1***********************\n\n" << endl;
cout << "\nHere's the menu of choices: ";
cout << "\n1. Add a Circle.\n";
cout << "2. Print a Circle. \n";
cout << "3. Print all Cricles \n";
cout << "4. Exit.\n";
cout << "\nPlease enter your choice: ";
cin >> choice;
while (choice <4){
switch (choice){
case 1:
// For adding cirles to the class
cout << " Enter the value for the radius of the circle: ";
cin >> radius;
cout << " Enter the value for the center of the circle: ";
cin >> center;
myCircle[thisPosition] = new Circle(radius, center);
myCircle[thisPosition]->PrintCircle();
thisPosition++;
break;
case 2: // For printing a particular cirle from the list of cirles
cout << " Enter the Value for which Circle to Print: ";
cin >> printPosition;
myCircle[printPosition - 1]->PrintCircle();
break;
case 3: // For printing all the circles in the class object array pointer list
cout << "\n";
for (int i = 0; i < thisPosition; i++){
myCircle[i]->PrintCircle();
cout << "\n==============================" << endl;
}
break;
case 4:
cout << "\n Good Bye !!! \n " << endl;
break;
}
cout << "\nHere's the menu of choices: ";
cout << "\n1. Add a Circle.\n";
cout << "2. Print a Circle. \n";
cout << "3. Print all Cricles \n";
cout << "4. Exit.";
cout << "Please enter your choice: ";
cin >> choice;
system("pause");
}
}
您的其他选择是使用一堆'if/else if'风格的语句,但在这种情况下,切换是一个更好的选择。 – Annabelle
你指的是什么“其他选择/替代”?在这种情况下,switch语句可能是最好的选择。 – TypeIA
有许多选项,包括一堆'if'和'goto'语句。告诉我们*为什么*你想避免'switch'对于给你一个实际有用的答案是非常有帮助的。你试图解决的**实际问题是什么?我怀疑你有[XY问题](http://meta.stackexchange.com/q/66377/167210)。 –