- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
有人知道这段代码有什么问题吗?它会出现上面的错误。我正在尝试创建一个程序来生成“随机”字母,并为用户提供最多 6 次猜测该字母的机会。
if ( 播放 != "y"|| 播放 != "Y")
编辑:完整代码
// This function displays game instructions, and returns nothing.
void Instructions();
// This function plays one game, and returns "W" if the player wins
// or "L" if the player runs out of guesses.
char Play();
//this function prompts the player to make a guess and returns that guess
char getLetter();
//The function returns 1 if the guess matches the solution and returns a 0 if they do not match
char guess();
// This function returns a random letter between "A" and "Z
char getAnswer();
int CompareLetters(char guess, char answer);
int main()
{
char answer;
//1. Greet the user and ask if they would like to play a guessing game.
printf("\nHello there!\nWould like to play a guessing game?Enter Y or N: \n");
scanf(" %c", &answer);
if(answer == 'y' || answer == 'Y')
Instructions();
{
printf("\nYou entered Y. Let's play!\n");
do{
}while (answer == 'y' || answer == 'Y');
}
printf("\nMaybe next time.\n");
printf("\nGoodBye for now.\n");
return -1;
}
void Instructions()
{
printf("I have a capital letter in mind. You have 6 chances to guess which letter I am \nthinking. I will let you know if you are too high or too low.\n");
printf("After each guess, you will be informed if your guess is too high or too low.\nGood luck!\n");
}
int PlayGuess(char answer)
{
int NumGuesses=0; int WinOrLose=0;
while (NumGuesses < MAX_GUESSES && WinOrLose==0);
{
//6. If the player guesses wrong for a 6th time, console them and let the program end with a return code of 1.
char guess;
guess = getLetter();
CompareLetters(guess,answer);
if(CompareLetters(guess,answer)==1)
{
WinOrLose = 1;
}
else
{
NumGuesses++;
}
}
if (WinOrLose==1)
{
return 1;
}
else
{
return 0;
}
}
//3. Generate a "random" character between 'A' and 'Z'. This will be the value the player will try to guess.
char getLetter()
{
char guess=0;
printf("Please enter your letter guess:", guess);
scanf(" %c",&guess);
return guess;
}
int CompareLetters(char guess, char answer)
{
if(guess == answer)
{
return 1;
}
else
{
////5. If the player does not guess the right answer, display whether the guess is "too high" or "too low".
if (guess < answer)
{
printf("Your guess is too low.");
}
else
{
printf("Your guess is too high.");
}
{
printf("\nDo you want to play again? Y or N: \n");
}
if ( Play != 'y' && Play != 'Y' )
printf("Thanks for playing.Goodbye!/n");
}
最佳答案
Play
似乎是一个(单个)char
,而不是 char
数组。因此,您应该使用 'y'
而不是 "y"
。
if ( Play != 'y' || Play != 'Y' )
这里似乎还存在逻辑错误 - 大概您打算使用 &&
而不是 ||
:
if ( Play != 'y' && Play != 'Y' )
编辑:您现在已经添加了其余的源代码。这里有很多问题,但我将列出一些问题来帮助您入门。
对 Instructions()
的调用不在大括号内,因此 Instructions()
将有条件地调用,其余代码将无条件执行 。
if(answer == 'y' || answer == 'Y')
Instructions();
{
printf("\nYou entered Y. Let's play!\n");
这应该是:
if(answer == 'y' || answer == 'Y')
{
printf("\nYou entered Y. Let's play!\n");
Instructions();
您的 do {} while
语句为空。这也是一个无限循环,因为您的 scanf
答案位于循环之外。
do{
}while (answer == 'y' || answer == 'Y');
您应该将 scanf
移至循环内部,并添加对 Play()
的调用。
与 CompareLetters
中的 Play
进行比较的 if 语句既不正确(没有名为 Play
的变量),而且错误place(CompareLetters
不应对此负责。)
您应该将其移至 main
,按照我上面提到的方式更新并与 answer
变量进行比较。
关于c - 操作数类型 ('char' 和 'char *' 不兼容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19349299/
int enter_path(char** path) { char* standard = "./questions.txt"; printf("\n\t%s\n\t%s",
我有以下几行代码: #define PORT 9987 和 char *ptr = (char *)&PORT; 这似乎适用于我的服务器代码。但是当我在我的客户端代码中写它时,它给出了这个错误信息:
大家好,我在成员函数中有以下内容 int tt = 6; vector>& temp = m_egressCandidatesByDestAndOtMode[tt]; set& egressCandi
我知道您可以通过以下方式在正则表达式中使用 NOT 操作数: [^AB] :匹配除 "A" 之外的任何内容或"B" A(?!B) :匹配"A" ,后面不跟 "B" (?
我的代码如下,下面还解释了为什么会发生左值; typedef struct test_item { char id[MENU_NAME_LEN + NULL_SPACE]; MenuF
我正在审查一些 javascript 代码,程序员在几个地方使用了 >>。我试图在谷歌上搜索但找不到这个操作数/运算符的作用。所以我来了。下面的代码示例: var triplet=(((binarra
我使用以下行(希望这是最佳实践,如果不正确请纠正我)来处理命令行选项: #!/usr/bin/bash read -r -d '' HELP &2 for i in "${invalid_opti
我正在尝试编辑一个计时器应用程序,出现了这行代码。我该如何解决? let styleMask: Int = NSClosableWindowMask | NSTitledWindowMask 错误是:
我可以得到两个特定日期之间的差异,这将等于日期总数。现在我想将工作日除以总天数并得到整数输出。 @IBAction func go(_ sender: UIButton) { let con
我的项目有一个问题,它应该使用一个线程将每一行相加,然后将它们全部相加,但是我收到一个错误,指出左值需要作为一元 '&"操作数 pthread_create(&tid, NULL, &sum_line
我的代码有问题。有以下功能: static Poly PolyFromCoeff(int coeff); static Mono MonoFromPoly(const Poly *p, int exp
在 C# 中是否没有字符串的 OR 操作数? 我正在查看 Microsoft C# 操作数页面 - 没有关于字符串的任何类型的 OR。 我有一个要写的 if 语句: if (Convert.ToStr
下面的函数左移一个double操作数: double shl(double x,unsigned long long n) { unsigned long long* p = (unsigne
我在 Linux 中使用了以下简单的 ksh 脚本 #!/bin/ksh set -x ### Process list of *.dat files if [ -f *.dat ] then pri
我有一个使用 Entity Framework 的查询。它有许多不同的操作数,我对其优先级感到困惑。我得到了错误的结果。我需要所有 IsPaid == true 或 IsPaid == null 的记
我有以下代码来尝试创建一个约束数组以添加到 View 中: let views = ["button": button] let metrics = ["margin": 16] var constr
这个问题在这里已经有了答案: How to compare one value against multiple values - Swift (8 个答案) 关闭 6 年前。 我有一种情况,我必须
我使用 jquery $.ajax 将请求发送到服务器,它返回 JSON。 $.ajax({ url: 'moreMonth.ajax', data: { startIndex: id },
我的问题是程序没有按照“他”的预期读取代码。 我有 if (hero.getPos() == (6 | 11 | 16)) { move = new Object[] {"Up", "Righ
我在对象中创建线程时遇到问题。错误是需要作为一元“&”操作数的左值 CPP文件 #include "AirQ.h" static int i=0; AirQ::AirQ(int pinNo, bool
我是一名优秀的程序员,十分优秀!