我正在做一个要求我使用函数检查两个字符串是否相等的赋值.我一直在第20行得到一个解析错误,这里调用了函数,我不知道出了什么问题.如果您发现可能导致问题的原因,请查看并告诉我.谢谢!
#include <iostream>
#include <string>
using namespace std;
bool checker(string firstWordParameter, string secondWordParameter);
int main()
{
string firstWord, secondWord;
bool match;
cout << "Hello user.\n"
<< "This program will determine whether two words are the same.\n"
<< "Please enter your first word you would like to check: ";
getline(cin, firstWord);
cout << "Great, now enter the second word: ";
getline(cin, secondWord);
match = bool checker(firstWord, secondWord);
if(match == true){
cout << "Match.";
}else{
cout << "Totally not a match.";
} …Run Code Online (Sandbox Code Playgroud) 在我尝试编写具有高可读性的代码时,我一直使用or和and运算符作为||和的同义词&&.虽然,现在我已经了解or并且and有不同的应用.http://devblog.avdi.org/2010/08/02/using-and-and-or-in-ruby/
令人困惑的是在我的rails应用程序和IRB控制台中运行以下代码时的区别.
在IRB:
var = false or true
# => true
Run Code Online (Sandbox Code Playgroud)
在我的rails应用程序中:
var = false or true
# => false
Run Code Online (Sandbox Code Playgroud)
简而言之,似乎or在IRB和实际的rails应用程序中没有相同的优先级.这令人困惑和误导.有什么理由吗?
//我使用的是布尔函数,它返回一个false和true,但主要不是//拾取它。假设my_string.Is_full和my_string.Is_empty说“它不//满”和“它不为空”。语法错误?
#include <iostream>
#include <string>
using namespace std;
const int SIZE = 5;
template <class New_Type>
class Array_Class
{
public:
Array_Class();
~Array_Class();
void Add(New_Type item);
void Print();
void PrintB();
bool Is_Empty();
bool Is_Full();
private:
New_Type *A;
New_Type *B;
int count;
};
template <class New_Type>
Array_Class<New_Type>::Array_Class()
{
cout << "You are inside the default constructor.\n";
cout << "New_Type has a size of " << sizeof(New_Type) << " bytes\n\n";
count = 0;
A = new New_Type[SIZE];
}
template <class New_Type>
Array_Class<New_Type>::~Array_Class()
{ …Run Code Online (Sandbox Code Playgroud) 我正在开发一个程序,我需要一个只有三个状态的变量,未触及(未初始化),true和false,因此很明显布尔值将是最佳选择.但在确定bool是否未受影响时,我遇到了一些问题.我很快发现布尔默认为204或205,但你怎么能说出来?我已经对它进行了一些测试,我注意到在我的一个程序中,它一直是204,但在另一个程序中它是205.
我不确定是什么决定了这个值,以及它的值是否因程序或基于设备和所使用的操作系统而异,但是无论操作系统或设备如何,该值在整个程序中是否一致?就像在,它会起作用吗?
//for some reason the default is only 204 or 205 when in an array
bool asdf[1];
const bool UNDEFINED_BOOL = asdf[0];
Run Code Online (Sandbox Code Playgroud)
而不是使用整个UNDEFINED_BOOL程序的值作为比较,以查看bool(在数组中)是否未定义?它似乎在我的设备上工作,但它适用于所有设备和所有操作系统?这是整个C++中一致的东西吗?还有另一种方法可以找出未定义的bool的值是什么,或者是没有示例你无法确定的东西?
编辑:当我的程序设置为true或false时,永远不会有理由将其设置为未初始化,所以这不是我要担心的事情
我最近一直在努力尝试使用布尔.基本上,我不知道为什么这不起作用.
在一个文件中我有这样的东西:
public static boolean newboolean;
if (something) {
newboolean = true;
}
else {
newboolean = false;
}
Run Code Online (Sandbox Code Playgroud)
在另一个文件中我有:
if(firstclass.newboolean = true) {
doSomething()
}
else {
doSomethingDifferent()
}
Run Code Online (Sandbox Code Playgroud)
基本上,如果第一个中的东西if (something)是真的,那么newboolean应该设置为true.
但是,而不是这样doSomething()做doSomethingDifferent().但是,我知道布尔值设置为true的事实.
这让我觉得有一个问题告诉我的第二个文件它是真的,希望这是有道理的.有人可以帮忙吗?
我有一段工作的代码,当它在读入的文本文件中遇到错误时,它会对该文本进行着色以突出显示它,因为它被写回到一个新文件中.
if (item.LogEntry.ToUpper().Contains("Error=\"Device Not Found\"".ToUpper()))
{
//Write out to new file in red to highlight error
}
else
{
//Write out to new file as normal
}
Run Code Online (Sandbox Code Playgroud)
我试图在名为GetCriticalErrors()的方法中编写所有这些.我正在使用bool在发现错误时返回true或false.这就是我到目前为止所拥有的.
bool aCriticalError;
public bool GetCriticalErrors(string logEntry)
{
foreach (var item in logEntry.ToUpper())
{
if (item.ToString().Contains("Error=\"Device Not Found\"".ToUpper()))
{
return true;
}
else
{
return false;
}
}
return aCriticalError;
}
Run Code Online (Sandbox Code Playgroud)
我这样称呼方法
if (GetCriticalErrors(item.LogEntry) == true)
{
//Write out to new file in red to highlight error
}
else
{
//Write …Run Code Online (Sandbox Code Playgroud) 我收到错误:
错误:(65,3)java:非法启动表达式
参考这一行:
public boolean equals(WordList wordList)
Run Code Online (Sandbox Code Playgroud)
我认为这是由字符串数组的范围引起的WordList[].但是,这似乎应该是可以接受的,因为我在构造函数中调用变量的实例.
我曾试图改变WordList[]建设public equals(WordList wordList),boolean equals(WordList wordList)以及其他的组合,虽然这些组合已经改变了错误信息.
码:
public class WordList
{
String[] words;
public int count;
//constructor
public WordList()
{
//create a size two array of strings and assign it to words instance variable
words = new String[2];
count = 0;
}
public int addWord(String word)
{
if(findWord(word) == -1) //word not in list
{
return count;
}
if(words.length == count)
{
String[] temp = …Run Code Online (Sandbox Code Playgroud) 我正在学习C#并尝试过这个问题.请看下面问题下面的两次尝试,有人可以指出我在哪里/怎么出错了吗?谢谢.
尝试1:
如果第一个布尔值为真且第二个布尔值为假,则返回true.
public static bool ReturnTrueAndFalse(bool boolean1, bool boolean2)
{
if (boolean1 = true && (boolean2 = false))
{
return true;
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
尝试2:
public static bool ReturnTrueAndFalse(bool boolean1, bool boolean2)
{
boolean1 = true;
boolean2 = false;
return boolean1 && boolean2;
}
Run Code Online (Sandbox Code Playgroud) 我正在编写一个接收两个数字的方法,如果它们都是偶数或两个都将返回true ...如果只有一个是奇数而一个是偶数,则返回false.它需要返回一个布尔语句,但它不工作..任何帮助表示赞赏!谢谢...
public static boolean compareEvenOdd(int x, int y) {
if((x % 2 ==0) && ( y% 2==0))||((x%2 != 0) && (y%2 != 0)){
return true;
} else
return false;
}
Run Code Online (Sandbox Code Playgroud) 为什么0 &&和布尔值计算为0而不是布尔值?
0 && false
> 0
0 && true
> 0
但是false && 0返回false而true && 0返回true
false && 0
> false
我认为数值和假表达式总是会返回一个布尔值.
1 && false
> false
boolean ×10
c++ ×3
java ×3
c# ×2
arrays ×1
class ×1
expression ×1
function ×1
if-statement ×1
irb ×1
javascript ×1
methods ×1
ruby ×1
string ×1
templates ×1