比较数组到变量的更好方法? [英] Better way to compare array to variable?

查看:79
本文介绍了比较数组到变量的更好方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将用户输入的变量与包含小写字母的数组进行比较。用户看到字母的小写版本,并且应该输入大写字母。有没有人对如何做得更好有任何想法?这段代码总是说答案是正确的,即使它不是。谢谢。

解决方案

你的逻辑完全搞砸了。只要用户输入任何A-Z字符,它将返回正确。为什么你需要任何类型的数组。如果您向用户显示小写字母,您已经知道它是什么。您所要做的就是将用户输入的值与您已知的值进行比较。数组似乎完全没有意义。


我将在C ++中回答这个问题,因为我这样做会更加舒服,因此我不会犯任何语法错误。逻辑不会改变。





将用户输入转换为char。仅使用以下代码段接受大写字母字符。



 如果 (userInput <  '  A' | | userInput >  '  Z'







 std :: cout<< ;  起始值为:<< YourLowerVariable<<的std :: ENDL; 

if (YourLowerVariable + 32 == userInput)
{
std :: cout<< 你是对的!;

system( pause);
}

return 0 ;





ASCII版本a与A之间的差异为32,b与B之间存在差异32上。


I am trying to compare a user entered variable to an array containing the lowercase alphabet. The user sees the lowercase version of the letter and is supposed to enter the uppercase. Does anyone have any ideas on how to do this better? This code always says the answer is correct, even if it''s not. Thank you.

解决方案

Your logic on this is completely messed up. As long as the user enters ANY A-Z character it will return "Correct". Why do you need an array of any kind. If you show the user a lower case letter, you already know what that is. All you have to do is compare the user''s entered value to the one value you already know. The array seems completely pointless.


I am going to answer this in C++ as it is more comfortable for me to do so, and so I do not make any syntax mistakes. The logic does not change.


Convert the user input to a char. Only accept uppercase alphabet characters by using the following snippet.

if( userInput < 'A' || userInput > 'Z' )




std::cout << "The starting lower is: " << YourLowerVariable<< std::endl;

if (YourLowerVariable + 32 == userInput)
{
    std::cout << "You are correct!";

system("pause");
}

return 0;



There is a difference of 32 in the ASCII version of a to A, as well as b to B and so on.


这篇关于比较数组到变量的更好方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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