- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经有几天无法尝试为我的类(class)完成这项作业了,我需要一些帮助。
作业是编写一个程序,根据用户的高度和体重资格(取决于他们的性别)通知用户他们的接受状态。
在程序结束时,它希望输出被接受的候选人数量以及被接受的候选人总数与候选人总数的平均值。
作业 - https://www.saddleback.edu/faculty/slinker/CS1A/CS1A_Fall2013/Assignment8.pdf
我们不能使用开关、条件运算符和选择(仅用于向结果输出正确的消息)。我们只能使用循环和复杂的 bool 表达式
我遇到的问题是:
如果我的所有 3 个输入都有效,如果它们被接受,为什么它们不输出,如果输入之一(高度或体重)或两个都被拒绝,那么为什么不输出它。我的 bool 变量不正确吗?如果是这样,我该如何解决。
为什么我输入 X 后无法退出循环/程序,我的 while 循环是否正确?
是否有任何我可以更改为“非选择语句”的选择语句。
这是我的代码
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
char gender;
int height;
int weight;
bool heightOK;
bool weightOK;
int candidateCount;
int validCandidateCount;
bool invalidGender;
bool invalidHeight;
bool invalidWeight;
float percentOutput;
candidateCount = 0;
validCandidateCount = 0;
cout << "Please enter the candidate's information (enter 'X' to exit)."
<< endl;
do
{
cout << "Gender: ";
cin.get(gender);
cin.ignore (1000,'\n');
invalidGender = ( !(gender == 'm' ||
gender == 'M' ||
gender == 'f' ||
gender == 'F' ));
candidateCount = candidateCount + 1;
if(invalidGender)
{
cout << "***** Invalid gender; please enter M or F*****" << endl;
}
}while(invalidGender);
while (gender != 'X' || gender != 'x')
{
candidateCount = candidateCount + 1;
do
{
cout << "Height: ";
cin >> height;
invalidHeight = height < 24 || height > 110;
heightOK = ((gender == 'm' || gender == 'M') &&
(height > 65 && height < 80));
heightOK = heightOK || ((gender == 'f' || gender == 'F') &&
(height > 62 && height < 75));
if(invalidHeight)
{
cout << "***** Invalid height; please enter a height in inches between 24 and 110. *****"
<< endl;
}
}while(invalidHeight);
do
{
cout << "Weight: ";
cin >> weight;
invalidWeight = weight < 50 || weight > 1400;
weightOK = ((gender == 'm' || gender == 'M') &&
(weight > 130 && weight < 250));
weightOK = weightOK || ((gender == 'f' || gender == 'F') &&
(weight > 110 && weight < 185));
if(invalidWeight)
{
cout << "***** Invalid weight; please enter a weight in lbs between 50 and 1400."
<< endl;
}
}while(invalidWeight);
if(heightOK && weightOK)
{
cout << "This candidate has been ACCEPTED!" << endl;
validCandidateCount = validCandidateCount + 1;
}
else if (!heightOK)
{
cout << "This candidate has been rejected based on the HEIGHT requirement."
<< endl;
}
else if (!weightOK)
{
cout << "This candidate has been rejected based on the WEIGHT requirement."
<< endl;
}
else if (!(heightOK && weightOK))
{
cout << "This candidate has been rejected based on the HEIGHT and WEIGHT requirements"
<< endl;
}
do
{
cout << "Gender: ";
cin.get(gender);
cin.ignore (1000,'\n');
candidateCount = candidateCount + 1;
if(invalidGender)
{
cout << "***** Invalid gender; please enter M or F*****" << endl;
}
}while(invalidGender);
}
cout << validCandidateCount << " candidate(s) accepted!" << endl;
percentOutput = validCandidateCount / candidateCount;
cout << "That's " << percentOutput <<"%!" << endl;
return 0;
}
最佳答案
主 while 循环应该有 and 条件。
while(gender !='X' && gender!='x)
而且你的选择代码有错误的条件语句。
if(heightOK && weightOK)
{
cout << "This candidate has been ACCEPTED!" << endl;
validCandidateCount = validCandidateCount + 1;
}
else if (!heightOK) // you have written else if(heightOK)
{
cout << "This candidate has been rejected based on the HEIGHT requirement."
<< endl;
}
else if (!weightOK) // you have written else if(weightOK)
{
cout << "This candidate has been rejected based on the WEIGHT requirement."
<< endl;
}
else if (!(heightOK && weightOK))
{
cout << "This candidate has been rejected based on the HEIGHT and WEIGHT requirements"
<< endl;
}
你应该在最后一个 do while 循环中删除那个 invalidgender 条件,否则即使你想按 X 退出它也会导致无限循环。相反,无效的性别条件可以放在主 While 循环的开头。
并且 invalidGender 变量应该再次赋值,否则它将获取之前存储的值。
invalidGender = ( !(gender == 'm' ||
gender == 'M' ||
gender == 'f' ||
gender == 'F' ));
完整代码
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
char gender;
int height;
int weight;
bool heightOK;
bool weightOK;
int candidateCount;
int validCandidateCount;
bool invalidGender;
bool invalidHeight;
bool invalidWeight;
double percentOutput;
candidateCount = 0;
validCandidateCount = 0;
cout << "Please enter the candidate's information (enter 'X' to exit)."
<< endl;
cout << "Gender: ";
cin.get(gender);
while (gender != 'X' && gender != 'x')
{
candidateCount = candidateCount + 1;
do
{
invalidGender = ( !(gender == 'm' ||
gender == 'M' ||
gender == 'f' ||
gender == 'F' ));
if(invalidGender)
{
cout << "***** Invalid gender; please enter M or F*****" << endl;
cout << "Gender: ";
cin>>gender;
cin.ignore (1000,'\n');
}
}while(invalidGender);
do
{
cout << "Height: ";
cin >> height;
invalidHeight = height < 24 || height > 110;
heightOK = ((gender == 'm' || gender == 'M') &&
(height > 65 && height < 80));
heightOK = heightOK || ((gender == 'f' || gender == 'F') &&
(height > 62 && height < 75));
if(invalidHeight)
{
cout << "***** Invalid height; please enter a height in inches between 24 and 110. *****"
<< endl;
}
}while(invalidHeight);
do
{
cout << "Weight: ";
cin >> weight;
invalidWeight = weight < 50 || weight > 1400;
weightOK = ((gender == 'm' || gender == 'M') &&
(weight > 130 && weight < 250));
weightOK = weightOK || ((gender == 'f' || gender == 'F') &&
(weight > 110 && weight < 185));
if(invalidWeight)
{
cout << "***** Invalid weight; please enter a weight in lbs between 50 and 1400."
<< endl;
}
}while(invalidWeight);
if(heightOK && weightOK)
{
cout << "This candidate has been ACCEPTED!" << endl;
validCandidateCount = validCandidateCount + 1;
}
else if (!heightOK)
{
cout << "This candidate has been rejected based on the HEIGHT requirement."
<< endl;
}
else if (!weightOK)
{
cout << "This candidate has been rejected based on the WEIGHT requirement."
<< endl;
}
else if (!(heightOK && weightOK))
{
cout << "This candidate has been rejected based on the HEIGHT and WEIGHT requirements"
<< endl;
}
cout << "Gender: ";
cin>>gender;
cin.ignore (1000,'\n');
}
cout << validCandidateCount << " candidate(s) accepted!" << endl;
percentOutput = (double)validCandidateCount / (double)candidateCount;
cout << "That's " << percentOutput*100 <<"%!" << endl;
return 0;
}
关于C++ 循环和 bool 表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43175590/
我有一个带有列的表提供者 implied(tiny int)(something like nullable bool) provi
我正在阅读 VideoFileWriter来自 AForge.Video.FFMPEG 的类(class)通过 ILSPY 组装(我很想看看特定方法是如何工作的)并发现了这个: public bool
这是我的完整代码... import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import
我有一个输入 list类型 [Maybe SomeType]和一个谓词 p类型 SomeType -> Bool ,我想回答这个问题“谓词 p 是否适用于所有碰巧在输入中的 SomeType ?”。
使用 !!x 有什么区别吗?对比(bool)x ? 假设__STDC_VERSION__ >= 199901L和 #include 他们都保证结果是0吗?或 1 ,并且无论 x 的大小和值如何,都不
我正在编写一些 C++ 代码,我想调用两个函数(checkXDirty 和 checkYDirty),并返回 true如果任一返回 true。即使一个返回 true 我也需要评估两者,所以我的第一个想
我注意到 bool在 QtCreator 中以不同于其他类型的颜色突出显示: 只有在包含某些 header 时才会发生这种情况,最终我将其追踪到 . QtCreator 的代码检查器似乎无法手动跟踪
有一个函数: func (first: Int) -> Int -> Bool -> String { return ? } 返回值怎么写?我对上面 func 的返回类型感到很困惑。 最
训练神经网络学习“异或” 我正在尝试使用“批量归一化”,我创建了一个批量归一化层函数“batch_norm1”。 import tensorflow as tf import nump
我已经创建了任务函数来验证我的 json 文件。一切正常,直到我没有使用结果。当我试图从 async task function 获得结果时它显示错误为 Cannot implicitly conve
我有一个函数 func login (parameters: [(String, Any)], completion: @escaping (Bool) -> Vo
我正在处理最近从 X/Motif 转移到 Qt 的 C++ 代码库。我正在尝试编写一个 Perl 脚本,它将用 bool 替换所有出现的 Boolean(来自 X)。该脚本只是做了一个简单的替换。 s
嗨,我正尝试创建一个Visiblity小部件,如果用户在Firebase数据库阵列上,该小部件将显示。看起来像这样(成员数组): 如您所见,我创建了一个StreamBuilder,如果当前用户的用户名
我创建了如下的rest api方法, Future activateAccount(int id, int code) async{ final body = {"code": '$c
在我的Flutter应用中,我有一个返回Future的函数,但我想将结果作为Stream。这是函数: Future isGpsOn() async { if (await Geolocat
我可以看到 BOOLEAN 覆盖了 __visit_name__ class BOOLEAN(Boolean): __visit_name__ = 'BOOLEAN' 控制调度员选择的访问者方
考虑以下代码: bool x; bool? y = null; x = y?? true; 将 bool? 分配给 bool 是一个编译时错误,但上面的代码在编译和运行时都成功了。为什么?尽管第三条语
我正在重写一些 Javascript 代码以在 Excel VBA 中工作。由于在这个网站上搜索,我已经设法翻译了几乎所有的 Javascript 代码!但是,有些代码我无法准确理解它在做什么。这是一
我想拍一张bool来自Vec并在 if 语句中进行比较。如何解决以下错误? | 7 | if cell { | ^^^^ expected
我在我的应用程序崩溃跟踪工具中发现了一些崩溃。基本上我有一个 tabBarController,其中一个选项卡有一个嵌入式 UIWebView,另一个选项卡有一个带有 UITableView 的 Co
我是一名优秀的程序员,十分优秀!