- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我在做一个示例练习时遇到了一些麻烦。
目标是让计算机通过反复取中点直到达到数字来猜测您的数字。我拥有一切,除了我似乎无法“降低”工作。
#include <iostream>
using namespace std;
void playOneGame();
bool shouldPlayAgain();
int getAverage(int min, int max);
int getLow(int min, int max);
int getHigh(int min, int max);
char getResponse();
const int MAX_VALUE = 100;
const int MIN_VALUE = 0;
int AVERAGE = 50;
char choice;
int high = MAX_VALUE;
int low = MIN_VALUE;
int main()
{
do
{
playOneGame();
} while (shouldPlayAgain());
return 0;
}
void playOneGame()
{
cout<<"Think of a number between " << MIN_VALUE << " and " << MAX_VALUE << endl;
getAverage(MIN_VALUE, MAX_VALUE);
}
int getAverage(int min, int max)
{
AVERAGE = (max - min)/2;
getResponse();
}
int getHigh(int low, int max)
{
low = AVERAGE;
AVERAGE += ( high - low ) / 2;
getResponse();
}
int getLow(int low, int max)
{
high = AVERAGE;
AVERAGE -= (high - low) /2;
getResponse();
}
char getResponse()
{
cout << "Is it " << AVERAGE << " ?" << " (h/l/c): " << endl;
cin >> choice;
if(choice == 'h' || choice == 'H')
{
getHigh(low, high);
}
else if(choice == 'l' || choice == 'L')
{
getLow(low, high);
}
else
{
cout<< "Got it!" <<endl;
}
}
bool shouldPlayAgain()
{
bool status;
char choice;
cout << "Do you want to play again? Y/N\n";
cin >> choice;
if(choice == 'y' || choice == 'Y')
status = true;
else
status = false;
return status;
}
#15 的示例输出如下:
Think of a number between 0 and 100Is it 50 ? (h/l/c): lIs it 25 ? (h/l/c): lIs it 13 ? (h/l/c): hIs it 19 ? (h/l/c): lIs it 10 ? (h/l/c): hIs it 14 ? (h/l/c): hIs it 16 ? (h/l/c): lIs it 8 ? (h/l/c): hIs it 12 ? (h/l/c): hIs it 14 ? (h/l/c): hIs it 15 ? (h/l/c): cGot it!Do you want to play again? Y/N
代替
Is it 25 ? (h/l/c): lIs it 13 ? (h/l/c): hIs it 19 ? (h/l/c): lIs it 10 ? (h/l/c):
应该是:
Is it 25 ? (h/l/c): lIs it 13 ? (h/l/c): hIs it 19 ? (h/l/c): lIs it 16 ? (h/l/c):
最佳答案
您的变量作用域有问题。
此处的变量“low”具有局部作用域。它使用传递给函数的值
int getHigh(int low, int max)
{
low = AVERAGE;
赋值给low意味着赋给局部变量,而不是全局变量
此处的变量“high”具有全局范围。它使用此页面上任何位置分配的值
int getLow(int low, int max)
{
high = AVERAGE;
AVERAGE -= (high - low) /2;
另外,尝试使用函数的返回值。
关于C++ 猜谜游戏 : unable to get midpoint properly,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46272808/
我正在用 JS 编写一个使用坐标的简单绘图应用程序。我需要递归地找到两个点的中点以画一条线(A 点和 B 点之间的中点,然后是三个结果点之间的两个中点,等等)。参见 this GIF一个更清楚的例子。
我确实有两种通过 bresenham/midpoint 算法创建实心圆的方法。但是第二种方式看起来比第一种方式好得多。 我想创建一个像第二个的结果,但是用第一个的方式一。我想用第一种方法来做,因为我需
我在做一个示例练习时遇到了一些麻烦。 目标是让计算机通过反复取中点直到达到数字来猜测您的数字。我拥有一切,除了我似乎无法“降低”工作。 #include using namespace std; v
本文整理了Java中com.evolveum.midpoint.prism.impl.lex.json.YamlLexicalProcessor类的一些代码示例,展示了YamlLexicalProce
我正在使用 python seaborn 包来生成发散的调色板 (seaborn.diverging_palette)。 我可以选择我的两种末端颜色,并定义中心是浅-> 白色还是深色-> 黑色(cen
本文整理了Java中com.evolveum.midpoint.xml.ns._public.common.common_3.XmlSchemaType类的一些代码示例,展示了XmlSchemaTyp
本文整理了Java中com.evolveum.midpoint.xml.ns._public.common.common_3.WorkItemType类的一些代码示例,展示了WorkItemType类
本文整理了Java中com.evolveum.midpoint.xml.ns._public.common.common_3.WorkItemEventCauseInformationType类的一些
本文整理了Java中com.evolveum.midpoint.xml.ns._public.common.common_3.ZoneOfControlType类的一些代码示例,展示了ZoneOfCo
我编写这段代码是为了使用比类(class)预期更多的 Java 元素。但我很难让它在所有 4 个方向上工作。 getter 和 setter 嵌入到使 Karel 移动的方法中。如果我能得到任何帮助让
在 d3 中,假设有 node1 和 node2,用水平相邻的圆圈表示。有一条短的水平线(我猜是一条路径)连接 node1 和 node2。 我如何:(1) 找到那条线/路径的中点,并且(2) 给那个
本文整理了Java中com.evolveum.midpoint.xml.ns._public.common.common_3.XmlSchemaType.getCachingMetadata()方法的
本文整理了Java中com.evolveum.midpoint.xml.ns._public.common.common_3.XmlSchemaType.setCachingMetadata()方法的
本文整理了Java中com.evolveum.midpoint.xml.ns._public.common.common_3.WorkItemType.setOriginalAssigneeRef()
本文整理了Java中com.evolveum.midpoint.xml.ns._public.common.common_3.XmlSchemaType.setDefinition()方法的一些代码示
本文整理了Java中com.evolveum.midpoint.xml.ns._public.common.common_3.WorkItemType.setProcessSpecificPart()
本文整理了Java中com.evolveum.midpoint.xml.ns._public.common.common_3.XmlSchemaType.()方法的一些代码示例,展示了XmlSchem
本文整理了Java中com.evolveum.midpoint.xml.ns._public.common.common_3.XmlSchemaType.getDefinition()方法的一些代码示
本文整理了Java中com.evolveum.midpoint.xml.ns._public.common.common_3.WorkItemType.getCandidateRef()方法的一些代码
本文整理了Java中com.evolveum.midpoint.xml.ns._public.common.common_3.WorkItemType.getCreateTimestamp()方法的一
我是一名优秀的程序员,十分优秀!