Introduction Notes for Java Novels 2019-7-22
1. Array
a) Define array variables
Type >[] < Name > = new < Type > [Number of Elements];
eg: int[] grades =new int[100];
double[] averages = new double[100];
(The number of elements must be an integer; the number of elements must be given; the number of elements can be a variable)
Question: How to calculate the average number of user input and output all the numbers larger than the average?
File name: Average.java
package average; import java.util.Scanner; public class Average { public static void main(String[] args) { Scanner in = new Scanner(System.in); int[] numbers = new int[100];//Define arrays int x; double sum=0; int cnt=0; x=in.nextInt(); while(x!=-1)//All input stops when input-1 { numbers[cnt]=x; sum+=x; cnt++;//Calculate how many numbers there are, and initialize with 0. x=in.nextInt(); } if(cnt>0) { double average =sum/cnt; for(int i=0;i<cnt;i++)//Numbers in traversal arrays { if(numbers[i]>average) { System.out.println("Above average:"+numbers[i]);//Number of inputs above average } } System.out.println("The average is:"+(sum/cnt)); } } }
Operation results
2. Strings
(a) The zero or more characters enclosed in double quotation marks are the face of a string;
b) String variables:
String s;
String is a class, and String's variable is the manager of the object, not the owner.
c) new = create
String s = new String("a string");
Create an object of String class, initialize the object with "a string", and create the variable s that manages the object
Initialization of string variables can also be directly String s = hello;
d) A string can be connected with a plus sign. When one side of the + is a string and the other side is not a string, the other side is expressed as a string and connected to eg:
"I'am "+"18"->"I' am 18";
1+2+"age"->"3age";
"age"+1+2->"age12";
e) Input string:
in.next(); // Enter a word marked with spaces, which include spaces, tab s, newlines. Read only one word. in.nextLine();// Enter a whole line The difference between them is that when you enter "this is a test", in.next() only outputs this, in.nextLine() outputs a complete line.
f) Compare two strings:
if(input=="bye"){....}//Compare the same if(input.equals("bye")){...}//Are the contents of the comparison the same?
g) String operations:
Strings are objects, and all operations on them are carried out through. This operator represents operations on the left string. Strings on the right can be variables or constants.
s1.compareTo(s2);//Comparing the size of two strings, if s1 is less than s2, the result is negative, if equal to 0, if greater than the result is positive; compareToIgnoreCase You can compare sizes without distinguishing between case and case. s1.length();//Gets the length of the string
s.charAt(index);//Access the characters in the string and return a single character in index. Index ranges from 0 to length()-1, just like an array. s.substring(n);//Get the whole content from position n to the end. s.substring(b,e);//Before getting the position from b to e (note that the position of E is not included)
Look for characters: s.index0f(c);//Get the position of the C character, there is no return - 1; s.LastIndex0f(c); // Start from the right; s.index0f(c,n);//Look for the C character from the n position; s. LastIndex 0f (c, n); // Start from the right; s.index0f(t);//Find the location of the string t; s.LastIndex0f(t); // Start from the right;
s.replace(char oldchar ,char newchar);//String replacement, all oldchar s will be replaced; s.trim();//Remove spaces; s.startWith(String prefix);// s.endWith(String prefix);//Judging the beginning and end of the string, the return value is boolean type.
s.toUpperCase();//Convert to capital letters; s.toLowerCase();//Convert to lowercase letters;
Note: String manipulation does not change the string itself. To get a new string, you need to assign a new string.
h) To supplement the boolean problem, boolean (boolean type) has two values: true and false, which are used to judge the logical conditions. Integer values and boolean values cannot be converted to each other.