在下面的代碼中,我需要關於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)。 –