如何将Switch语句与Do while语句一起使用 [英] How to use Switch statement with the Do while statement

查看:662
本文介绍了如何将Switch语句与Do while语句一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用开关和while语句编写显示以下菜单的程序:

Use the switch and do while statements to write a program that displays the following menu:

---------------------------- Menu -------------------------
1.Option 1 
2.Option 2 
3.Option 3 
4.Option 4 
5.Help
6.Quit
----------------------------------------------------------- 
Please Enter You Choice (1-6):


$ b $b如果用户输入1-5之间的选项,程序将显示消息,说明

选项由用户选择。菜单再次显示。 如果用户输入6作为选项,程序将显示感谢您选择我们的程序并结束。
$ b $b如果用户输入的选项不是1-6,程序将显示无效的选择 。您需要选择1到6之间的选项。菜单再次显示。



请帮我完成此代码

谢谢






 If the user enters a choice between 1-5, the program displays message stating the
option picked by the user. The menu is displayed again.  If the user enters 6 as choice, the program displays "Thank you for choosing our program" and ends.
 If the user enters a choice other than 1-6, the program displays "Invalid choice. You need to select an option between 1 and 6". The menu is displayed again.

PLEASE HELP ME TO FINISH THIS CODE
THANK YOU


#include <iostream>

using namespace std;

int main() {

	int Number;
	int Menu;
	cout << "----------------menu------------------" << endl;
	cout << " 1.Option 1 " << endl;
	cout << " 2.Option 2 " << endl;
	cout << " 3.Option 2 " << endl;
	cout << " 4.Option 4 " << endl;
	cout << " 5.Help " << endl;
	cout << " 6.Quit " << endl;
	cout << "---------------------------------------" << endl;
	cout << " Please Enter You Choice (1-6): ";
	cin >>Number;
	switch (Number)
	{
	case 1 :
	case 2:
	case 3:
	case 4:
	case 5:
		break;
	case 6:
		cout << "Thank you for choosing our program " << endl;
		break;
	default:
		cout << "Invalid choice. You need to select an option between 1 and 6" << endl;
		break;
	}

	system("pause");
    return 0;
}

推荐答案

#include <iostream>

using namespace std;

int main() {

	int Number;
	int Menu;
	cout << "----------------menu------------------" << endl;
	cout << " 1.Option 1 " << endl;
	cout << " 2.Option 2 " << endl;
	cout << " 3.Option 2 " << endl;
	cout << " 4.Option 4 " << endl;
	cout << " 5.Help " << endl;
	cout << " 6.Quit " << endl;
	cout << "---------------------------------------" << endl;

	do {

		cout << " Please Enter You Choice (1-6): ";
		cin >> Number;
		switch (Number)
		{
		case 1: 
			cout << "You chose option 1 " << endl;
			cout << "----------------menu------------------" << endl;
			cout << " 1.Option 1 " << endl;
			cout << " 2.Option 2 " << endl;
			cout << " 3.Option 2 " << endl;
			cout << " 4.Option 4 " << endl;
			cout << " 5.Help " << endl;
			cout << " 6.Quit " << endl;
			cout << "---------------------------------------" << endl;
			break;
		case 2:	cout << "You chose option 2 " << endl;
			cout << "----------------menu------------------" << endl;
			cout << " 1.Option 1 " << endl;
			cout << " 2.Option 2 " << endl;
			cout << " 3.Option 2 " << endl;
			cout << " 4.Option 4 " << endl;
			cout << " 5.Help " << endl;
			cout << " 6.Quit " << endl;
			cout << "---------------------------------------" << endl;
			break;

		case 3:
			cout << "You chose option 3 " << endl;
			cout << "----------------menu------------------" << endl;
			cout << " 1.Option 1 " << endl;
			cout << " 2.Option 2 " << endl;
			cout << " 3.Option 2 " << endl;
			cout << " 4.Option 4 " << endl;
			cout << " 5.Help " << endl;
			cout << " 6.Quit " << endl;
			cout << "---------------------------------------" << endl;
			break;
		case 4:
			cout << "You chose option 4 " << endl;
			cout << "----------------menu------------------" << endl;
			cout << " 1.Option 1 " << endl;
			cout << " 2.Option 2 " << endl;
			cout << " 3.Option 2 " << endl;
			cout << " 4.Option 4 " << endl;
			cout << " 5.Help " << endl;
			cout << " 6.Quit " << endl;
			cout << "---------------------------------------" << endl;
			break;
		case 5:
			cout << "You chose option 5 " << endl;
			cout << "----------------menu------------------" << endl;
			cout << " 1.Option 1 " << endl;
			cout << " 2.Option 2 " << endl;
			cout << " 3.Option 2 " << endl;
			cout << " 4.Option 4 " << endl;
			cout << " 5.Help " << endl;
			cout << " 6.Quit " << endl;
			cout << "---------------------------------------" << endl;
			break;
			break;
		case 6:
			cout << "Thank you for choosing our program " << endl;
	
			break;
		default:
			cout << "Invalid choice. You need to select an option between 1 and 6" << endl;			cout << "----------------menu------------------" << endl;
			cout << " 1.Option 1 " << endl;
			cout << " 2.Option 2 " << endl;
			cout << " 3.Option 2 " << endl;
			cout << " 4.Option 4 " << endl;
			cout << " 5.Help " << endl;
			cout << " 6.Quit " << endl;
			cout << "---------------------------------------" << endl;
			break;
		}
	} while (Number!= 6);
	system("pause");
    return 0;
}
</iostream>


这篇关于如何将Switch语句与Do while语句一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆