Java Professional Course II
Operation formats and operations
1. Operations between two numbers
Import Java.util.Scanner;
public class P {
public static void Main (string[] args)
{
System.out.print ("Please enter the first number:");
Scanner sc=new Scanner (system.in);
int N1=sc.nextint ();//define a variable to be of type int
System.out.print ("Please enter a second number:");
int N2=sc.nextint (); Defines the second variable to be of type int
SYSTEM.OUT.PRINTLN (n1+ "*" +n2+ "=" +n1*n2);//two numbers multiplied
System.out.println (n1+ "+" +n2+ "=" + (N1+N2));//two numbers added
System.out.println (n1+ "/" +n2+ "=" + (double) n1/n2);//Divide two numbers
System.out.println (n1+ "-" +n2+ "=" + (N1-N2));//two digit subtraction
2. Simply seek the source code of the perimeter of the graph
Import Java.util.Scanner;
public class p2{
public static void Main (string[] args) {
System.out.println ("Please enter the length of the rectangle:");
Scanner sc=new Scanner (system.in);//define a condition
int N1=sc.nextint ();//Enter a variable of type int
System.out.println ("Please enter the width of the rectangle:");
int N2=sc.nextint ();//define a variable of the second int type
System.out.print (2*N1+2*N2);//output an output with a mathematical formula
3. Using the classic of the trinocular operator to find odd even numbers of source code
Import Java.util.Scanner;
public class P5 {
public static void Main (string[] args) {
System.out.print ("Please enter a positive integer:");
Scanner sc=new Scanner (system.in);
int N1=sc.nextint ();//defines the type of the input number as int
String result= (n1%2==0? " is even ":" is odd ");
SYSTEM.OUT.PRINTLN (result);
4. Array Concepts
Arrays: Used to hold multiple data of the same type
Array item: Represents an item of data in an array
Subscript: The number of an element in an array
Length: Number of array elements
After an array is created, each entry must have a value, and if it is not assigned, the default value
Source code format
public class TheArray {
public static void Main (string[] args) {
Defining arrays
Syntax 1: data type [] variable;
Syntax 2: Data type variables [];
Int[] numbers; Declares an array of type int (each element of an array must be of type int), named numbers
Assigning a value to an array
Syntax: Array variable = new data type [length];
Numbers = new int[4];//assigns a value to the array numbers with a length of 4
Merge writing
int[] numbers = new INT[4];
//
Manipulating elements in an array
Modifying elements of an array
Array variable name [subscript] = expression;
Numbers[0] = 2;//assigns 2 to the 1th item of the array (the element with the subscript 0)
NUMBERS[3] = 15;
NUMBERS[5] = 1;//This code will cause subscript to cross the line
//
Get the elements in the array
Array variable name [subscript]
System.out.println (numbers[2]);
NUMBERS[1] = Numbers[0] + numbers[3];
Numbers[0] = numbers[1]*2;
//
Get the length of the array
Array variable name. length
System.out.println (numbers.length);
//
The value of the last item in the output array
System.out.println (Numbers[numbers.length-1]);
}
}
Java Specialty Courses