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