Java 02 Post-lesson Jobs

Source: Internet
Author: User

First question:

//enter two numbers in the message box and display the results of the two-digit subtraction with a message boxImportJavax.swing.JOptionPane; Public classCalculator { Public Static voidMain (String args[]) {string firstnumber,secondnumber; intNumber1,number2,sum,minus,plus,divide; Firstnumber=joptionpane.showinputdialog ("Please enter the first number:"); Secondnumber=joptionpane.showinputdialog ("Please enter a second number:"); Number1=Integer.parseint (Firstnumber); Number2=Integer.parseint (secondnumber);//calculates the result of a subtraction of two numberssum=number1+number2; Minus=number1-number2; Plus=number1*number2; Divide=number1/number2; Joptionpane.showmessagedialog (NULL, "Two number of the and for:" +sum+ "," + "The difference of two numbers is:" +minus+ "," + "two number of product for:" +Plus+ "," + "two number of quotient:" +divide, "results", Joptionpane.plain_message); System.exit (0); }}

The second question:

①enumtest.java results:

False

False

True

SMALL

MEDIUM

LARGE

The first result is to compare whether the two are the same, the second is to determine whether it is a basic type, the third is to assign and compare, and the next three is the result of outputting all the values one at a time.

An enum can be used to create an enumeration type that is of a reference type, is not part of the original data type, and each value refers to an object in the enumeration, using "= =" and "Equals ()" to determine whether the two values are equal.

② in the computer, the fixed-point number has 3 kinds of notation: the original code, the inverse code and the complement. The three representations have both symbolic and numeric digits, and the sign bit is 0 for "positive", 1 for "negative" and the value bit, and three for the method of representation.

The so-called original code is the binary fixed-point notation, that is, the highest bit is the sign bit, "0" is positive, "1" is negative, the remaining bits represent the size of the value.

The inverse code notation stipulates that the inverse code of a positive number is the same as its original code, and the inverse of a negative number is a bitwise negation of its original code, except for the sign bit.

The complement notation stipulates that the complement of positive numbers is the same as the original code, and that the complement of negative numbers is added to the minus 1 of the inverse code.

The masking principle of a variable of the same name means that the variable will be given a value based on the nearest assignment to him, and the relationship is weaker, and the assignment with a far distance will not work.

③double results are not accurate, mainly due to the floating point in the calculation of the expression method, floating-point number is a certain subset of the rational number of numbers representation, in the computer to approximate a real number, the real numbers by an integer or a fixed number of points multiplied by a cardinality of the whole power to get, The actual size of floating-point numbers cannot be accurately saved.

WORKAROUND: You can use Java.math.BigDecimal to calculate, using the function of the subtraction, the result will be more accurate, the Division will not be able to leave 10 bits after rounding.

int x=100;

int y=200;

System.out.println ("x+y=" +x+y);

System.out.println (x+y+ "=x+y");

The result is:

x+y=100200

300=x+y

The first output, simply splicing the numbers and strings together, no other meaning, and the second output, the output is "x+y" results, so the results are not the same.

Java 02 Post-lesson Jobs

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.