Java Learning Path (v)--thinking in Java

Source: Internet
Author: User

Writing procedures to be careful, encounter bugs to be good at troubleshooting.

Today in Java to write a few small programs, because of the pursuit of optimization and efficacy, ignoring a small detail problem. But finally found the bug and solved it.

We all know that when using a for loop, for example:

for (int i = 0; i < n; i++) {...}

We usually write here. But this is not enough, because this statement can also be optimized. such as I<n, it is better to judge the i!=n directly, so that in the computer can save computing time. Also, i++ can also be changed to ++i, because i++ need the computer to save the value before I +1,++i directly let i+1 and no temporary variable. However, because of this, a small imperceptible bug happened.

The original topic is very simple, is to find the whole number of integers a, b. At first I wrote this:

Import Java.io.*;import java.util.*;p ublic class Number2 {public static void main (string[] args) throws Ioexception{int be Gin, end; String str; BufferedReader br = new BufferedReader (new InputStreamReader (system.in)); str = Br.readline (); begin = new Integer (str); BufferedReader BR1 = new BufferedReader (new InputStreamReader (system.in)); str = Br1.readline (); end = new Integer (str); for (; begin! = end; ++begin) {Boolean b = true;for (int i = 2; I! = math.sqrt (begin); ++i) {if (0 = = begin% i) {b = False;break;}} if (b) System.out.println (begin);}}}

Pay attention to the code coloring section, at first glance nothing wrong, the pursuit of performance. But the result of the operation was a surprise. For a prime number from 101 to 200, 121,169 two numbers appear. After careful investigation I found that the original bug is here: Math.sqrt () is a double,i is an int, in the comparison with double is never equal. Or we'll cast the coloring statement to (int), or we'll rewrite it as I<MATH.SQRT (begin).

After the modification, it runs successfully with the following code:

1 ImportJava.io.*;2 ImportJava.util.*;3  Public classnumber2 {4      Public Static voidMain (string[] args)throwsioexception{5         intbegin, end;6 String str;7BufferedReader br =NewBufferedReader (NewInputStreamReader (system.in));8str =br.readline ();9Begin =NewInteger (str);TenBufferedReader BR1 =NewBufferedReader (NewInputStreamReader (system.in)); Onestr =br1.readline (); AEnd =NewInteger (str); -          for(; begin! = end; + +)begin) { -             Booleanb =true; the              for(inti = 2; I < math.sqrt (begin); ++i) { -                 if(0 = = begin%i) { -b =false; -                      Break; +                 } -             } +             if(b) A System.out.println (begin); at         } -     } -}
The input and output streams that appear in the code are discussed later.

Java Learning Path (v)--thinking in Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.