我坚持使用一个应该计算字符串中大写字母的函数。但计数器的结果却是 0,我不知道我在哪里犯了错误。
const bigLettersCount = (str) => {
let result = 0;
for (let i = 0; i < str.length; i += 1) {
if (str[i].toUpperCase() === str[i]) {
result += i;
}
return result;
}
}
bigLettersCount('HeLLo')
Run Code Online (Sandbox Code Playgroud) C# 中的 String 是一种引用类型,但它重写了==, 和Equals()来比较字符串的值。有没有办法检查两个字符串是否确实是同一实例并指向同一内存?
EvenObject.ReferenceEquals("A", "A")会返回 true,因为它只是调用==.
这个测试将会通过。所以仍在等待检查是否相同的实例。
[Test]
public void TestString()
{
var a = "A";
var b = "A";
var c = a;
Assert.IsTrue((object)a == (object)b);
Assert.IsTrue(ReferenceEquals(a,b)); //It is same as objA == objB
Assert.IsTrue(Object.ReferenceEquals(a,b));
Assert.AreEqual(a,b);
Assert.AreSame(a, b);
unsafe
{
TypedReference tra = __makeref(a);
IntPtr ptra = (IntPtr)(&tra);
TypedReference trb = __makeref(b);
IntPtr ptrb = (IntPtr)(&trb);
Assert.AreNotEqual(ptra, ptrb);
Assert.AreNotSame(ptra, ptrb);
TypedReference trc = __makeref(c);
IntPtr ptrc = (IntPtr)(&trc);
Assert.AreNotEqual(ptra, …Run Code Online (Sandbox Code Playgroud) 我仍在学习 C++ 编程的基础知识,并尝试了 C++ 运算符的一些应用程序,但似乎有些事情我仍然不明白。
我尝试编写以下行来测试使用等于(==)作为三元运算符的可能性:
cout << "(2+2 == 2*2 == pow(2, 2)) == " << (2+2 == 2*2 == pow(2, 2)) << endl;
输出为0
所以,我怀疑这可能是因为 pow(2,2) 的返回值是双精度型,而前两个操作数的返回值是整数,因此我尝试了以下操作:
cout << "(2+2 == 2*2 == int(pow(2, 2))) == " << (2+2 == 2*2 == int(pow(2, 2))) << endl;
cout << "(double(2+2) == double(2*2) == pow(2, 2)) == " << (double(2+2) == double(2*2) == pow(2,2)) << "\n\n";
Run Code Online (Sandbox Code Playgroud)
两行代码的输出也是 0。
我还尝试使用括号来减少操作数的数量,但最终得到相同的输出。
可能重复:
Java中的字符串比较
继承我的剧本:
public class euler4 {
/**
* a palindromatic number reads the same both ways. the largest palindrome
* made from the product of 2-digit numbers is 9009 = 91 * 99.
*
* find the largest palindrome made from the product of two 3-digit numbers.
*/
public static void main(String[] args) {
/*for(int t = 100; t < 200; t++) {
for(int l = 100; l < 100; l++) {
String milah = "" + t * …Run Code Online (Sandbox Code Playgroud) 这是我的调试代码:
log.error(u.getName() + " - " + u.getName().length() + " - host_node" + " - " + u.equals("host_node"));
Run Code Online (Sandbox Code Playgroud)
我的日志:
11:27:16 [main] ERROR com.google - host_node - 9 - host_node - false
Run Code Online (Sandbox Code Playgroud)
为什么它不相等,我甚至检查长度,看看是否首先包含空格.但事实并非如此.
我的印象是,当寻找2 ints ==测试之间的平等就足够了
作为一部分equals(),我
if (this.getDayOfWeek() != that.getDayOfMonth()) {
return false;
}
Run Code Online (Sandbox Code Playgroud)
其中getDayOfWeek定义为
public int getDayOfWeek() {
return calendarInstance.get(Calendar.DAY_OF_WEEK);
}
Run Code Online (Sandbox Code Playgroud)
好的,我们int在这里得到2 秒.两者都是1.而且不一样(根据equals)
请帮我理解原因 1 != 1
这怎么可能?这两个值都在内存中,并且在设置了这两个值后在断点处暂停执行。我很高兴找到一个 if 语句,该语句检查当值实际上相同时要执行的两个布尔值是否不同。因此,我在同一断点处使用“立即窗口”进行了更深入的研究。

出于好奇,我停止了程序,并且(就在我注意到这种不一致的原始调试点)我插入了几行代码:
var one = true;
var two = true;
Run Code Online (Sandbox Code Playgroud)

我破坏了 C# 还是什么?同一方法调用范围内的两对布尔值(我仔细检查了它们的 .GetType() 返回布尔值)如何使用相等运算符不一致?
尽管这无关紧要,但第一对(其中 double equals 方法返回错误的结果)是在可能会或可能不会更改属性值的方法调用之前和之后使用 PropertyInfo.GetValue(object target) 收集的在目标上。我使用了两次 GetValue 以确保在“做某事”之前更改了属性的值。
我在多次执行程序时得到相同的结果,所以我不认为这是执行环境的侥幸。
我正在运行这样的字符串相等检查:
if($myString eq "ExampleString")
Run Code Online (Sandbox Code Playgroud)
是否存在myString可能导致执行进入if结构的值,无论字符串文字是什么?
我对Python和Coding比较陌生.我一直很好,直到遇到"无":
variable = None
while variable != "":
print("Why does this loop start?")
Run Code Online (Sandbox Code Playgroud)
虽然看起来很简单但我无法理解为什么上面的循环开始了.如何None为空字符串不同?我以为他们都评价为False.