需要帮助菜单程序 [英] Need help with menu program

查看:71
本文介绍了需要帮助菜单程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。


好​​吧,让我切入追逐并解释我的问题。


我正在尝试设计一个使用if / else if和while循环的菜单计划。该程序计算用户在给定行星上的重量。它首先要求用户的体重,要求他们做出选择。如果他们选择1到9的选择,用户的体重将根据他们选择的任何选择进行计算(例如,如果用户输入150作为他们的体重并选择金星的选择#2,那么用户的体重就是金星将是135磅。但是,我遇到了一些问题。


当我编译程序时,输出并没有像我预期的那样出现。

程序和结果如下:

程序

//这个菜单驱动的程序使用if / else语句来计算

//用户在给定行星上的重量。它还插入一个循环。


#include< iostream>

#include< iomanip> //需要setprecison命令

#include< conio> //需要显示黑色输出屏幕

using namespace std;


int main()

{

int choice;

double weight1,weight2;



//显示th屏幕上的菜单选项。

cout<< " \t\tPlanet Menu \ n \ nn";

cout<< " 1。 Mercury \ n" ;;

cout<< >> 2。 Venus \\\
" ;;

cout<< " 3。 Earth \ n" ;;

cout<< " 4。 Mars \ n" ;;

cout<< << 5。 Jupiter \ n" ;;

cout<< >> 6。 Saturn \ n" ;;

cout<< &-7。天王星\ n" ;;

cout<< " 8。海王星\ n" ;;

cout<< " 9。 Pluto \ n" ;;

cout<< " 99。退出\ n \\ nnn ;;


cout<< 输入你的体重:;

cin>> weight1;


while(choice!= 99)


if(choice == 1)

{

cout<< 请做出选择;

cin>>选择;

weight2 = weight1 * 0.40;

cout<< 你会减肥 << weight2<< 在水星上。 << endl;

}

else if(choice == 2)

{

cout< ;< 请做出选择。 " ;;

cin>>选择;

weight2 = weight1 * 0.90;

cout<< 你会减肥 << weight2<< 在金星上。 << endl;

}

else if(choice == 3)

{

cout< ;< 请做出选择;

cin>>选择;

weight2 = weight1 * 1.00;

cout<< 你会减肥 << weight2<< 在地球上。 << endl;

}

else if(choice == 4)

{

cout< ;< 请做出选择;

cin>>选择;

weight2 = weight1 * 0.40;

cout<< 你会减肥 << weight2<< 在火星上。 << endl;

}

else if(choice == 5)

{

cout< ;< 请做出选择;

cin>>选择;

weight2 = weight1 * 2.50;

cout<< 你会减肥 << weight2<< 在木星上。 " << endl;

}

else if(choice == 6)

{

cout< ;< 请做出选择;

cin>>选择;

weight2 = weight1 * 1.10;

cout<< 你会减肥 << weight2<< 在土星上。 << endl;

}

else if(choice == 7)

{

cout< ;< 请做出选择;

cin>>选择;

weight2 = weight1 * 0.80;

cout<< 你会减肥 << weight2<< 在天王星上。 << endl;

}

else if(choice == 8)

{

cout< ;< 请做出选择;

cin>>选择;

weight2 = weight1 * 1.2;

cout<< 你会减肥 << weight2<< 在海王星上。 " << endl;


}

else if(choice == 9)

{

cout<< 请做出选择;

cin>>选择;

weight2 = weight1 * 0.01;

cout<< 你会减肥 << weight2<< 在冥王星上。 " << endl;

}

//当用户输入其他数字时输出以下消息

//而不是1到9或99在屏幕上。

否则如果(选择!= 99)

{

cout<< 有效选择是1到9或99退出。\ nn;

cout<< 再次运行程序并选择其中一个。\ n;

}


getch();

返回0;

}

输出线

星球菜单


1.水星

2.维纳斯

3.地球

4.火星

5.木星

6.土星

7.天王星

8.海王星

9.冥王星

99.退出


输入你的体重:150

请做出选择2
你将体重60岁。

请做出选择。 2

你将在金星上重135.
请做出选择。 99

你将体重135金星。



你看到了问题吗?无论我选择哪种选择,它总是为Mercury计算。然后,当我再次选择该选项时,它会计算出正确的选择。此外,选择99应该退出,而不是进行计算。 (注意:错误以粗体显示。)并且,当我键入不是其中一个选项的值时,而不是仅显示有效选择是1到9或99退出。再次运行程序并选择其中一个,我得到的只是这个陈述的无限循环。

我的循环可能有问题???

Hello, everyone.

Well, let me cut to the chase and explain my problem.

I am trying to devise a menu plan that uses the if/else if and the while loop. The program calculates the user''s weight on a given planet. It first asks for the user''s weight, asks them for a choice. If they choose choices 1 though 9, the user''s weight will be calculated on whatever choice they picked (for example, if the user inputs 150 as their weight and choose choice #2 for Venus, then the user''s weight on Venus would be 135 pounds. However, I am running into some problems.

When I compile the program, the output does not come out as I expect.
The program and the results are listed below:

Program:
// This menu-driven program uses an if/else statement to calculate
// the user''s weight on a given planet. It also inserts a loop.

#include <iostream>
#include <iomanip> //Needed for the setprecison command
#include <conio> //Needed to show black output screen
using namespace std;

int main()
{
int choice;
double weight1, weight2;


// Displays the menu choices on the screen.
cout << "\t\tPlanet Menu\n\n";
cout << "1. Mercury\n";
cout << "2. Venus\n";
cout << "3. Earth\n";
cout << "4. Mars\n";
cout << "5. Jupiter\n";
cout << "6. Saturn\n";
cout << "7. Uranus\n";
cout << "8. Neptune\n";
cout << "9. Pluto\n";
cout << "99. Exit\n\n";

cout << "Enter your weight: ";
cin >> weight1;

while (choice !=99)

if (choice == 1)
{
cout << "Please make a selection ";
cin >> choice;
weight2 = weight1 * 0.40;
cout << "You will weight" << weight2 << "on Mercury." <<endl;
}
else if (choice == 2)
{
cout << "Please make a selection. ";
cin >> choice;
weight2 = weight1 * 0.90;
cout << "You will weight" << weight2 << "on Venus." <<endl;
}
else if (choice == 3)
{
cout << "Please make a selection ";
cin >> choice;
weight2 = weight1 * 1.00;
cout << "You will weight" << weight2 << "on Earth." <<endl;
}
else if (choice == 4)
{
cout << "Please make a selection ";
cin >> choice;
weight2 = weight1 * 0.40;
cout << "You will weight" << weight2 << "on Mars." <<endl;
}
else if (choice == 5)
{
cout << "Please make a selection ";
cin >> choice;
weight2 = weight1 * 2.50;
cout << "You will weight" << weight2 << "on Jupiter . " <<endl;
}
else if (choice == 6)
{
cout << "Please make a selection ";
cin >> choice;
weight2 = weight1 * 1.10;
cout << "You will weight" << weight2 << "on Saturn." <<endl;
}
else if (choice == 7)
{
cout << "Please make a selection ";
cin >> choice;
weight2 = weight1 * 0.80;
cout << "You will weight" << weight2 << "on Uranus." <<endl;
}
else if (choice == 8)
{
cout << "Please make a selection ";
cin >> choice;
weight2 = weight1 * 1.2;
cout << "You will weight" << weight2 << "on Neptune. " <<endl;

}
else if (choice == 9)
{
cout << "Please make a selection ";
cin >> choice;
weight2 = weight1 * 0.01;
cout << "You will weight" << weight2 << "on Pluto. " <<endl;
}
//Outputs the following message when user enters number other
//than 1 through 9 or 99 on the screen.
else if (choice !=99)
{
cout << "The valid choices are 1 through 9 or 99 to exit.\n";
cout << "Run the program again and select one of those.\n";
}

getch();
return 0;
}

Output Line:
Planet Menu

1. Mercury
2. Venus
3. Earth
4. Mars
5. Jupiter
6. Saturn
7. Uranus
8. Neptune
9. Pluto
99. Exit

Enter your weight: 150
Please make a selection 2
You will weight60on Mercury.
Please make a selection. 2
You will weight135on Venus.
Please make a selection. 99
You will weight135on Venus.


Do you see the problem? No matter what choice I key in for the first choice, it always calculates for Mercury. Then, when I put that choice in again, it calculates the correct choice. Also, the choice 99 should exit, not do a calculation. (Note: The errors are in bold.) And, when I key in a value that is not one of the choices, instead of just showing "The valid choices are 1 through 9 or 99 to exit. Run the program again and select one of those," all I get is an infinite loop with this statement.
Could there be something wrong with my loops???

推荐答案


您好,每个人。


好​​吧,让我切入追逐并解释我的问题。


我正在尝试设计一个使用if / else if和while循环的菜单计划。该程序计算用户在给定行星上的重量。它首先要求用户的体重,要求他们做出选择。如果他们选择1到9的选择,用户的体重将根据他们选择的任何选择进行计算(例如,如果用户输入150作为他们的体重并选择金星的选择#2,那么用户的体重就是金星将是135磅。但是,我遇到了一些问题。


当我编译程序时,输出并没有像我预期的那样出现。

程序和结果如下:

程序

//这个菜单驱动的程序使用if / else语句来计算

//用户在给定行星上的重量。它还插入一个循环。


#include< iostream>

#include< iomanip> //需要setprecison命令

#include< conio> //需要显示黑色输出屏幕

using namespace std;


int main()

{

int choice;

double weight1,weight2;



//显示th屏幕上的菜单选项。

cout<< " \t\tPlanet Menu \ n \ nn";

cout<< " 1。 Mercury \ n" ;;

cout<< >> 2。 Venus \\\
" ;;

cout<< " 3。 Earth \ n" ;;

cout<< " 4。 Mars \ n" ;;

cout<< << 5。 Jupiter \ n" ;;

cout<< >> 6。 Saturn \ n" ;;

cout<< &-7。天王星\ n" ;;

cout<< " 8。海王星\ n" ;;

cout<< " 9。 Pluto \ n" ;;

cout<< " 99。退出\ n \\ nnn ;;


cout<< 输入你的体重:;

cin>> weight1;


while(choice!= 99)


if(choice == 1)

{

cout<< 请做出选择;

cin>>选择;

weight2 = weight1 * 0.40;

cout<< 你会减肥 << weight2<< 在水星上。 << endl;

}

else if(choice == 2)

{

cout< ;< 请做出选择。 " ;;

cin>>选择;

weight2 = weight1 * 0.90;

cout<< 你会减肥 << weight2<< 在金星上。 << endl;

}

else if(choice == 3)

{

cout< ;< 请做出选择;

cin>>选择;

weight2 = weight1 * 1.00;

cout<< 你会减肥 << weight2<< 在地球上。 << endl;

}

else if(choice == 4)

{

cout< ;< 请做出选择;

cin>>选择;

weight2 = weight1 * 0.40;

cout<< 你会减肥 << weight2<< 在火星上。 << endl;

}

else if(choice == 5)

{

cout< ;< 请做出选择;

cin>>选择;

weight2 = weight1 * 2.50;

cout<< 你会减肥 << weight2<< 在木星上。 " << endl;

}

else if(choice == 6)

{

cout< ;< 请做出选择;

cin>>选择;

weight2 = weight1 * 1.10;

cout<< 你会减肥 << weight2<< 在土星上。 << endl;

}

else if(choice == 7)

{

cout< ;< 请做出选择;

cin>>选择;

weight2 = weight1 * 0.80;

cout<< 你会减肥 << weight2<< 在天王星上。 << endl;

}

else if(choice == 8)

{

cout< ;< 请做出选择;

cin>>选择;

weight2 = weight1 * 1.2;

cout<< 你会减肥 << weight2<< 在海王星上。 " << endl;


}

else if(choice == 9)

{

cout<< 请做出选择;

cin>>选择;

weight2 = weight1 * 0.01;

cout<< 你会减肥 << weight2<< 在冥王星上。 " << endl;

}

//当用户输入其他数字时输出以下消息

//而不是1到9或99在屏幕上。

否则如果(选择!= 99)

{

cout<< 有效选择是1到9或99退出。\ nn;

cout<< 再次运行程序并选择其中一个。\ n;

}


getch();

返回0;

}

输出线

星球菜单


1.水星

2.维纳斯

3.地球

4.火星

5.木星

6.土星

7.天王星

8.海王星

9.冥王星

99.退出


输入你的体重:150

请做出选择2
你将体重60岁。

请做出选择。 2

你将在金星上重135.
请做出选择。 99

你将体重135金星。



你看到了问题吗?无论我选择哪种选择,它总是为Mercury计算。然后,当我再次选择该选项时,它会计算出正确的选择。此外,选择99应该退出,而不是进行计算。 (注意:错误以粗体显示。)并且,当我键入不是其中一个选项的值时,而不是仅显示有效选择是1到9或99退出。再次运行程序并选择其中一个,我得到的只是这个陈述的无限循环。

我的循环可能有问题???
Hello, everyone.

Well, let me cut to the chase and explain my problem.

I am trying to devise a menu plan that uses the if/else if and the while loop. The program calculates the user''s weight on a given planet. It first asks for the user''s weight, asks them for a choice. If they choose choices 1 though 9, the user''s weight will be calculated on whatever choice they picked (for example, if the user inputs 150 as their weight and choose choice #2 for Venus, then the user''s weight on Venus would be 135 pounds. However, I am running into some problems.

When I compile the program, the output does not come out as I expect.
The program and the results are listed below:

Program:
// This menu-driven program uses an if/else statement to calculate
// the user''s weight on a given planet. It also inserts a loop.

#include <iostream>
#include <iomanip> //Needed for the setprecison command
#include <conio> //Needed to show black output screen
using namespace std;

int main()
{
int choice;
double weight1, weight2;


// Displays the menu choices on the screen.
cout << "\t\tPlanet Menu\n\n";
cout << "1. Mercury\n";
cout << "2. Venus\n";
cout << "3. Earth\n";
cout << "4. Mars\n";
cout << "5. Jupiter\n";
cout << "6. Saturn\n";
cout << "7. Uranus\n";
cout << "8. Neptune\n";
cout << "9. Pluto\n";
cout << "99. Exit\n\n";

cout << "Enter your weight: ";
cin >> weight1;

while (choice !=99)

if (choice == 1)
{
cout << "Please make a selection ";
cin >> choice;
weight2 = weight1 * 0.40;
cout << "You will weight" << weight2 << "on Mercury." <<endl;
}
else if (choice == 2)
{
cout << "Please make a selection. ";
cin >> choice;
weight2 = weight1 * 0.90;
cout << "You will weight" << weight2 << "on Venus." <<endl;
}
else if (choice == 3)
{
cout << "Please make a selection ";
cin >> choice;
weight2 = weight1 * 1.00;
cout << "You will weight" << weight2 << "on Earth." <<endl;
}
else if (choice == 4)
{
cout << "Please make a selection ";
cin >> choice;
weight2 = weight1 * 0.40;
cout << "You will weight" << weight2 << "on Mars." <<endl;
}
else if (choice == 5)
{
cout << "Please make a selection ";
cin >> choice;
weight2 = weight1 * 2.50;
cout << "You will weight" << weight2 << "on Jupiter . " <<endl;
}
else if (choice == 6)
{
cout << "Please make a selection ";
cin >> choice;
weight2 = weight1 * 1.10;
cout << "You will weight" << weight2 << "on Saturn." <<endl;
}
else if (choice == 7)
{
cout << "Please make a selection ";
cin >> choice;
weight2 = weight1 * 0.80;
cout << "You will weight" << weight2 << "on Uranus." <<endl;
}
else if (choice == 8)
{
cout << "Please make a selection ";
cin >> choice;
weight2 = weight1 * 1.2;
cout << "You will weight" << weight2 << "on Neptune. " <<endl;

}
else if (choice == 9)
{
cout << "Please make a selection ";
cin >> choice;
weight2 = weight1 * 0.01;
cout << "You will weight" << weight2 << "on Pluto. " <<endl;
}
//Outputs the following message when user enters number other
//than 1 through 9 or 99 on the screen.
else if (choice !=99)
{
cout << "The valid choices are 1 through 9 or 99 to exit.\n";
cout << "Run the program again and select one of those.\n";
}

getch();
return 0;
}

Output Line:
Planet Menu

1. Mercury
2. Venus
3. Earth
4. Mars
5. Jupiter
6. Saturn
7. Uranus
8. Neptune
9. Pluto
99. Exit

Enter your weight: 150
Please make a selection 2
You will weight60on Mercury.
Please make a selection. 2
You will weight135on Venus.
Please make a selection. 99
You will weight135on Venus.


Do you see the problem? No matter what choice I key in for the first choice, it always calculates for Mercury. Then, when I put that choice in again, it calculates the correct choice. Also, the choice 99 should exit, not do a calculation. (Note: The errors are in bold.) And, when I key in a value that is not one of the choices, instead of just showing "The valid choices are 1 through 9 or 99 to exit. Run the program again and select one of those," all I get is an infinite loop with this statement.
Could there be something wrong with my loops???



你好,我真的没有看到你的while循环的大括号。这是一个错字吗?

其次,第一次运行循环时程序不知道是什么选择,因为你要求用户输入if语句中的值。您应该在循环开始时询问选择,然后检查ifs是什么。希望有所帮助。

Hello, I don''t really see your braces for your while loop. Is that a typo?
Second, the first time you run the loop the program doesn''t know what choice is since you ask the user for the values in the if statements. You should ask for choice at the beggining of the loop and then check what is it is with the ifs. Hope that helps.



你好,我真的没有看到你的while循环的大括号。这是一个错字吗?

其次,第一次运行循环时程序不知道是什么选择,因为你要求用户输入if语句中的值。您应该在循环开始时询问选择,然后检查ifs是什么。希望有所帮助。
Hello, I don''t really see your braces for your while loop. Is that a typo?
Second, the first time you run the loop the program doesn''t know what choice is since you ask the user for the values in the if statements. You should ask for choice at the beggining of the loop and then check what is it is with the ifs. Hope that helps.



不,这不是拼写错误。我应该在哪里为我的while循环括号?另外,你输入请输入你的体重后,我是否应该要求选择。

No, that''s not a typo. Where should I put the braces for my while loop? Also, are youu saying that I should ask for the choice after I typed in "Please enter your weight."



否,这不是一个错字。我应该在哪里为我的while循环括号?另外,在我输入请输入您的体重之后,您是否应该要求我做出选择。
No, that''s not a typo. Where should I put the braces for my while loop? Also, are youu saying that I should ask for the choice after I typed in "Please enter your weight."



while循环的大括号应该在while语句后面一个,在最后一个if块后面。这样它就会执行直到用户输入99.

另外cin>>选择;" line应该在while循环的开头,如下所示:

Hi, Your braces for the while loop should be one right after the while statement and one after the last if block. That way it will execute until the user enters 99.
Also the "cin >> choice;" line should be at the beginning of the while loop like this:

展开 | 选择 | Wrap | 行号


这篇关于需要帮助菜单程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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