Java Simple Learning

Source: Internet
Author: User

Enumeration type:Size s=size.small;

converting from a string to an enumeration

Enumeration can be used in for,switch Statements

Run Enumtest.java

Enum types are reference types and do not belong to the original data type, and you can use the "= =" and the equals () methods to directly compare the values of the enumeration variables.

The concept of original code, anti-code, and complement:

The number is represented in binary form in the computer. The number is divided into signed number and unsigned number. The original code, the inverse code, the complement are the symbolic fixed-point number representation method. The highest bit of a signed fixed-point number is the sign bit,0is positive,1is vice. The following are the8as an example of a bit integer, the original code is the binary form of the number itself. For example0000001is that+1 1000001is that-1the inverse code and complement of positive numbers are the same as the original code. The inverse of a negative number is the negation of the original code except the sign bit.[-3]Anti-=[10000011]Anti-=11111100the complement of a negative number is that the original code in addition to the symbol bit after the negation in the end and then add1. [-3]complement=[10000011]complement=11111101a number and its complement are reversible.

The so-called original code is described in the previous 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 1of the inverse code.

type conversions in Java

It can be seen that a numeric value with a double type is calculated, and the result is imprecise. We can use The method of using the BigDecimal class to solve.

The basic method of converting strings to numbers

When you run the following program

int x=100;

int y=200;

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

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

Output Result:

x+y=100200

300=x+y

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

Import Javax.swing.JOptionPane; Import Class Joptionpane

public class Addition {

public static void Main (String args[])

{

String Firstnumber,//First string entered by user

Secondnumber; Second string entered by user

int Number1,//first number to add

Number2,//second number to add

SUM1,

SUM2,

SUM3,

SUM4;

Read in first number from user as a string

Firstnumber =

Joptionpane.showinputdialog ("Please enter the first number (numerator)");

Read in second number from user as a string

Secondnumber =

Joptionpane.showinputdialog ("Please enter the second number (denominator)");

Convert numbers from type String to type int

Number1 = Integer.parseint (Firstnumber);

Number2 = Integer.parseint (Secondnumber);

Add the numbers

SUM1 = Number1 + number2;

sum2 = Number1-number2;

SUM3 = Number1 * NUMBER2;

SUM4 = Number1/number2;

Display the results

Joptionpane.showmessagedialog (

NULL, "addition result" + Sum1 + "" +

"Subtraction Result" + sum2 + "" +

"Multiply result" + sum3 + "" +

"Division Result" + sum4 + "", "Results",

Joptionpane.plain_message);

System.exit (0); Terminate the program

}

}

Java Simple Learning

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.