Java Chapter II hands-on brain exercises

Source: Internet
Author: User

1, write a program, the user input two number, to find out its subtraction, and the message box display calculation results.

Import Javax.swing.JOptionPane;

public class test{

public static void Main (string[] args) {

int N1=integer.parseint (Joptionpane.showinputdialog ("Input number 1:"));

int N2=integer.parseint (Joptionpane.showinputdialog ("Input number 2:")); gets The value entered from the Joptionpane.showinputdialog dialog box and then converts to the int type

Joptionpane.showmessagedialog (NULL, "The sum is:" + (N1+N2)

+ "\ n" + "The difference is:" + (N1-N2)

+ "\ n" + "The product is:" + (N1*N2)

+ "\ n" + "the Division is:" + (N1/N2));

}

}

Java variables Follow the "masking principle of the same name variable", please read the relevant information after class to find out the relevant knowledge, and then write some test code, as in this example, consciously defined in different places with the same name variable, to see exactly what the output is.

What can you conclude by checking the number of bits per data type in Java and the range of values ?

There are four basic types of the following: int length data type: Byte (8bits), short (16bits), Int (32bits), Long (64bits), char data type: Unicode characters, 16-bit corresponding class type: Integer, Float, Boolean, Character , Double, Short, Byte, Long

Conclusion:

Convert from low precision to high precision

Basic type conversion to class type

class type to string conversion

Conversion of a primitive type to a string

Why is the numeric value of a double type not "mathematically accurate" result?

This involves the conversion of binary and decimal. NThe binary can be understood as: a power of numeric x cardinality, such as the decimal number we are familiar with123.4=1xTen²+2x10+3x(Tenof the0Second Power) +4x(Tenof the-1Second Power)the same is true of other binaries, such as binary numbers11.01=1x2+1x(2of the0Second Power) +0+1x(2of the-2Second Power)=of decimal3.25. Doublethe value of the type occupies64bit, i.e. -Binary number, except that the highest bit represents the positive and negative sign, and the lowest bit is bound to have an error with the actual data (unless the actual data happens to be2of theNSecond party). For example, for example, to use a4bitto represent decimals3.26, from high to low in turn corresponding2of the1,0,-1,-2power, according to the top analysis, should be in the binary number11.01 (corresponding to the decimal3.25)and the11.10 (corresponding to the decimal3.5)choose between. in simple terms, the values that we give, in most cases, require more digits than 64bit to be accurately represented (even for infinite numbers), and the double type has only 64bit, the number of digits in the back is bound to bring an error, unable to get "mathematically accurate" results.

you should use a string instead of a double value when building a BigDecimal object , or it is still possible to raise the problem of computational precision. (why is this?) )

Double, cannot accurately represent16More than the number of valid digits in theBigDecimalWhen the applicationBigDecimal (String)It makes sense for the constructor to create objects. Other than thatBigDecimalThe object is created and we cannot use the traditional+、-*/ 

Java Chapter II hands-on brain exercises

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.