52ky 发表于 2022-9-25 21:47:37

95个java经典谜题解惑

官方Java开发工程师QQ群(27163376),验证请输入您的ID一本回答java中经典的迷惑点的书本,肯定让你能心动的书本。。。。肯定“豆”有所值。。下面的方法目的断定它那仅有的参数是不是是一个奇数。这个方法可以准确办公吗?publicstaticbooleanisOdd(inti){returni%2==1;}奇数可以被定义为被2整除余数为1的整数。表达式i%2计算的是i整除2时所生成的余数,因而看起来这个程序应当可以准确办公。惋惜的是,它不能;它在四分之一的时间里回来的都是错误的答案。为何是四分之一?由于在一切的int数值中,有一半都是负数,而isOdd方法关于对一切负奇数的判别都会失败。在任何负整数上调用该方法都回回来false,不论该整数是偶数仍是奇数。这是Java对取余操作符(%)的定义所生成的后果。该操作符被定义为关于一切的int数值a和一切的非零int数值b,都满意下面的恒等式<imgsrc="sad.gif"smilieid="2"border="0"alt=""/>a/b)
b+(a%b)==a.......本资料共包括以下附件:
JAVA解惑.pdf

(Official Java development engineer QQ group (27163376), please enter your ID to verify a book that answers the classic confusing points in java, a book that will definitely make your heart flutter. . . . Definitely "beans" worth it. . The purpose of the following method is to determine whether its only parameter is an odd number. Does this method work correctly? publicstaticbooleanisOdd(inti){returni=1;} Odd numbers can be defined as integers that have a remainder of 1 when divisible by 2. The expression icomputes the remainder when i divides by 2, so it seems that this program should work correctly. Sadly, it can't; it comes back with the wrong answer a quarter of the time. Why is a quarter? Since half of all int values ??are negative, the isOdd method will fail to discriminate against all negative odd numbers. Calling this method on any negative integer returns false, whether the integer is even or odd. This is a consequence of Java's definition of the remainder operator (%). The operator is defined to satisfy the following identity for all int values ??a and all non-zero int values ??b, <imgsrc="sad.gif"smilieid="2"border="0"alt=""/> a/b)
b (a ==a....... This document includes the following attachments:
JAVA puzzles.pdf)




页: [1]
查看完整版本: 95个java经典谜题解惑