Java jobs) 01

Source: Internet
Author: User
Tags arithmetic first string

One, arithmetic

(a) Design ideas

Use the scanner class to read the runtime user input data, then perform a data type conversion, and then use the output class output after completing the arithmetic.

(ii) Flowchart

(c) Program code

Zhangxiaochen 2015/10 44 The operation

Import Javax.swing.JOptionPane; Import Class Joptionpane

public class Operation {

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

Sum,//Sum of Number1 and number2

Redu,//reduction of Number1 and Number2

Pro Product of Number1 and number2

float Div; Divistor of Number1 and Number2

Read in first number from user as a string

Firstnumber =

Joptionpane.showinputdialog ("Enter first integer");

Read in second number from user as a string

Secondnumber =

Joptionpane.showinputdialog ("Enter second integer");

Convert numbers from type String to type int

Number1 = Integer.parseint (Firstnumber);

Number2 = Integer.parseint (Secondnumber);

Add the numbers

sum = number1 + number2;

Redu=number1-number2;

Pro=number1 * NUMBER2;

Div=number1/number2;

Display the results

Joptionpane.showmessagedialog (

Null

"And yes:" + sum+ "\ n" +

"Difference is:" + redu+ "\ n" +

"Product is:" + pro+ "\ n" +

"Quotient is:" + div+ "\ n",

"Results",

Joptionpane.plain_message);

System.exit (0); Terminate the program

}

}

(iv) operation

Two enumtest

(a) Program code

public class Enumtest {

public static void Main (string[] args) {

Size S=size.small;

Size T=size.large;

S and T refer to the same object?

System.out.println (s==t); //

is the original data type?

System.out.println (S.getclass (). isprimitive ());

Convert from String

Size u=size.valueof ("SMALL");

System.out.println (S==u); True

List all of its values

For (Size value:Size.values ()) {

System.out.println (value);

}

}

}

Enum Size{small,medium,large};

(ii) operation

(iii) Thinking

Enumeration types cannot be directly operated, and type conversions are required to compare sizes or operations.

Three testdouble

(a) Program code

Public class testdouble {

Public Static void Main (String args[]) {

System. out. println ("0.05 + 0.01 =" + (0.05 + 0.01));

System. out. println ("1.0-0.42 =" + (1.0-0.42));

System. out. println ("4.015 * 100 =" + (4.015 * 100));

System. out. println ("123.3/100 =" + (123.3/100));

}

}

(ii) operation

(iii) Thinking

Double type results in loss of data precision due to precision problems

Java jobs) 01

Related Article

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.