我正在编写一段代码,使用switch语句在Celsius和Fahrenheit之间进行转换,如下面的代码所示。
#include <stdio.h>
#include <iostream>
using namespace std;
float celsius;
float fahrenheit;
void convertF()
{
celsius = ((fahrenheit - 32) * 5) / 9;
}
void convertC()
{
fahrenheit = (((celsius * 9) / 5) + 32);
}
int main()
{
unsigned short choice;
cout << "Welcome to the temperature converter.\n";
cout << "Please press '1' for celsius to fahrenheit conversion" << endl;
cout << "Or, type '2' for fahrenheit to celsius conversion." << endl;
cout << "To exit, press 0" << endl;
cin >> choice;
switch(choice)
{
case 0:
return 0;
break;
case 1:
cout << "Please enter temperature in celsius:";
cin >> celsius;
convertC();
cout << "\n";
cout << "Computing...\n\n";
cout << "The temperature in Fahrenheit is " << fahrenheit << ".\n";
cout << "Press any key to terminate the program." << endl;
cout << endl;
break;
case 2:
cout << "Please enter temperature in fahrenheit:";
cin >> fahrenheit;
convertF();
cout << "\n";
cout << "Computing...\n\n";
cout << "The temperature in celsius is " << celsius << ".\n";
cout << "Press any key to terminate the program." << endl;
cout << endl;
break;
default:
cout << "That is not an option!" << endl;
cout << "Please close the program and try again." << endl;
break;
}
return 0;
}
如果用户选择1或2,程序将执行转换。然后按任意键结束程序。
但是我想在第一次转换后给他们另一次转换的机会,这样他们就可以退出程序或执行另一次转换。我试图在案例1和案例2中加入另一个switch语句,但它没有用。
解决此问题的最佳方法是什么?我在想是否有完全使用switch语句的替代方法?
答案 0 :(得分:1)
你需要的只是用switch语句将程序的一部分包含在某种循环中。
该程序可能类似于以下内容
#include <iostream>
using namespace std;
float convertF( float fahrenheit )
{
return ((fahrenheit - 32) * 5) / 9;
}
float convertC( float celsius )
{
return (((celsius * 9) / 5) + 32);
}
int main()
{
cout << "Welcome to the temperature converter.\n";
while ( true )
{
cout << "\nPlease press '1' for celsius to fahrenheit conversion" << endl;
cout << "Or, type '2' for fahrenheit to celsius conversion." << endl;
cout << "To exit, press 0" << endl;
unsigned short choice;
if ( !( cin >> choice ) || ( choice == 0 ) ) break;
switch( choice )
{
float celsius;
float fahrenheit;
case 1:
cout << "Please enter temperature in celsius:";
cin >> celsius;
cout << "\n";
cout << "Computing...\n\n";
cout << "The temperature in Fahrenheit is " << convertC( celsius ) << ".\n";
break;
case 2:
cout << "Please enter temperature in fahrenheit:";
cin >> fahrenheit;
cout << "\n";
cout << "Computing...\n\n";
cout << "The temperature in celsius is " << convertF( fahrenheit ) << ".\n";
break;
default:
cout << "That is not an option!" << endl;
break;
}
}
return 0;
}
为了更具可读性,您可以引入类似
的枚举enum { CELSIUS = 1, FAHRENHEIT = 2 };
并将其枚举器用作案例标签。例如
switch( choice )
{
float celsius;
float fahrenheit;
case CELSIUS:
//...
case FAHRENHEIT:
//...
default:
//...
}
答案 1 :(得分:1)
您可以在循环中使用switch语句:
do
{
cin >> choice;
switch(choice)
{
//cases
}
}
while (choice != 0);
或
choice = /* not 0 */;
while(choice != 0)
{
cin >> choice;
switch(choice)
{
//cases
}
}
或
for(unsigned int choice = /* not 0 */; choice != 0; )
{
cin >> choice;
switch(choice)
{
//cases
}
}
答案 2 :(得分:0)
您可以像这样使用while循环:
unsigned int choice;
do
{
cin >> choice;
// switch with only case 1 and 2
}
while(choice != 0);
return 0;
我不知道你完全使用switch语句的替代方法是什么意思。
答案 3 :(得分:0)
do-while
循环可能是解决它的最简单方法。您想检查while(choice != 0)
以继续运行循环,直到用户输入0作为选择,这意味着您希望输入部分也在do-while
循环内,因为您希望进一步输入,并在do-while
内也有switch语句。当choice
输入为0
时,循环将结束。
这显示了do-while
循环的一些示例:http://www.cplusplus.com/doc/tutorial/control/
答案 4 :(得分:-1)
只需更改您的代码:
cout << "Welcome to the temperature converter.\n";
while(1) // <----- Add this
{ // <----- Add this
cout << "Please press '1' for celsius to fahrenheit conversion" << endl;
//...
// All your code down to
//...
cout << "Please close the program and try again." << endl;
break;
}
} // <----- Add this
return 0;