2020-09-08 Class Notes

Main knowledge point

1.Basic structure of java program

Insert picture description here
Note:
1. The package line can have 0 or more, usually at the top of the program.
2. The import line can have 0 or more, usually after the package and before the class name.
3. The main program entry parameter String[] args It has the same effect as String args[], the former is recommended for easy understanding

2. Camel case nomenclature

Generally used to define variables, class names, attributes, namespaces, etc. Except for the first word in lowercase, the first letter of the remaining words is uppercase,
such as myFirstProject

3. Data type

Including 8 basic data types and reference data types The
following table lists the 8 basic data types and corresponding default values

Types of Defaults
byte 0
short 0
int 0
long 0L
float 0.0f
double 0.0d
boolean false
char ‘u0000’

4. Variable

1. Local variables
2. Global variables
3. Parametric variables
Note: Local variables include parametric variables, but the two have subtle differences.
Parametric variables do not need to be defined, local variables need to be defined first

5. Data type conversion (automatic conversion and forced conversion)

In Java, due to inheritance and up-casting, subclasses can be naturally converted to parent classes, but the conversion of parent classes to subclasses requires forced conversion.
Can be converted from a large storage range to a small storage range;
byte<-short<-int<-long<-float<-double
Format: type b=(type)a (type stands for type, refer to the basic data types in the table above)
Note: It may cause loss of accuracy! ! Be cautious!

6. Notes on strings

1. String splicing automatically converts other characters into string types. For
example, the following code block, the running result is Hello JavaEE51
instead of Hello JavaEE6

public class test
{
    
    
  public static void main(String[] args)
  {
    
    
    System.out.println("Hello JavaEE"+5+1);
  }
}

Only when the format is the following code block, can print 6

public class test
{
    
    
     public static void main(String[] args)
       {
    
    
         Ststem.out.println("Hello JavaEE"+(5+1));
       }
}

Note that when concatenating strings, all will become string types. If necessary, parentheses should be added to ensure the correctness of the program

7. Set (list) and array

1. List collection creation, addition, deletion

List<String> list=new ArraryList<String>();
list.add("hello java");
list.remove(XXX);//xxx可以填充索引,也可以填充Object o(内容)

2. Two creations of one-dimensional arrays

int array[]=new arrary[length];
String array[]={
    
    "X","XX","XXX","XXXX"};

8. Branch structure

The branch structure includes if single branch, if-else, if-else-if, switch-case, trinocular operation

9. Loop structure

The loop structure includes while, do-while, for, and for-each.
Note:
1. The body of the do-while loop is executed at least once
. 2. For-each has higher performance than for !
Use the for-each loop to traverse the list collection as follows

for(String i:list)
{
    
    
   system.out.println(i);
}

10. Actively throw exceptions

Java's exception capture mechanism allows users to customize exceptions, and also allows users to actively throw exceptions. The
following table lists 9 common exceptions and their meanings

abnormal meaning
ClassCastException Type conversion exception
ClassNotFoundException Class not found exception
ArithmeticException Arithmetic abnormality
ArrayStoreException The array contains an incompatible value exception
ArrayIndexOutOfBoundsException Array subscript out of bounds exception
SQLException Operation database exception class
NullPointerExxeption Null pointer exception
NumberFormatException String to number throws an exception
FileNotFoundException File not found exception

11. Main function main(args) callback method

Often used in menus, reenter the main function

12.continue与break

continue: end this cycle and enter the next cycle
break: end the cycle

Project case (2)

1. Print solid and hollow isosceles triangles (and beautiful)
Insert picture description here
code logic: double loop, type all spaces first, and then type *
1. Solid

for(int i=0;i<n;i++)
{
    
    
    for(int j=4;j>i;j--)
    {
    
    
        System.out.print(" ");//打印空格
    }
    for(int j=0;j<i*2+i;j++)
    {
    
    
        System.out.print("*");
    }
    System.out.println("\n");
}

2. Hollow

public class Test {
    
    
	public static void main(String[] args)  {
    
    
		//控制行数
		for(int i=0;i<5;i++)
		{
    
    
			//控制每行的空格数 
			//总行数-当前行数=每行的空格数
			for(int j=4;j>i;j--)
			{
    
    
				System.out.print(" ");
			}
			
			// 打印每行星星数
			for(int j=0;j<i*2+1;j++)
			{
    
    
				
				//判断是否打到最后一行
				//1.全部要打 2.美观
				if(i==4)
				{
    
    
					//奇数打印星星 偶数不打印星星
					if(j%2==1)
					{
    
    
						System.out.print(" ");
						continue;
					}
				System.out.print("*");
				continue;
				}
				//空心 每行的第一个星星与最后一个星星打印,其余打空格
				if(j==0){
    
    
				System.out.print("*");
				continue;
				}
				//最后一个
				if(j==i*2)
				{
    
    
					System.out.print("*");
					continue;
				}
				//其余的都打空格
				System.out.print(" ");
			}
			//换行
			System.out.println();
		}
	}

2. Find out all the numbers within xx that can be divisible by 7 (high performance, code that meets the requirements)

public class test4 {
    
    
     public static void main(String args[]) throws IOException
     {
    
    
    	 int n=0;
    	 System.out.println("请输入一个整数:");
    	 Scanner s=new Scanner(System.in);
    	 String ss=s.nextLine();
    	 for (int i = 0; i < ss.length(); i++) {
    
    
    		 if (! (ss.charAt(i) >= '0' && ss.charAt(i) <= '9')) {
    
    
    			 System.out.println("请输入合理的整数");
    			 //break;
    			 main(args);
    		 }
    		 
    			 
    		 
 			}
    	
    	 n=Integer.parseInt(ss);
			//  System.out.println(n);
			  s(n);
    		
    	 
    	 
    	 
     }
     public static void s(int n) throws IOException
     {
    
    
    	 int count=0;
    	 int sum=0;
    	 int[] m =new int[100];    	
    	 for(int i=1;i<n;i++)
    	 {
    
    
    		 if(i%7==0)
    		 {
    
    
    			 count++;
    			// System.out.println(i);
    			m[count-1]=i;
    		 }
    	 }
    	 System.out.println("符合条件的总个数为:"+count+"分别是:");
    	 for(int i=0;i<count;i++)
    	 {
    
    
    		 System.out.println(m[i]);
    		 sum+=m[i];
    	 }
    	 double sum2=sum;
    	 System.out.println("他们的总和为:"+sum);
    	 System.out.println("他们的平均数为:"+sum2/count);
    	 System.out.println("其中最小值为:"+m[0]);
    	 System.out.println("其中最大值为:"+m[count-1]);
    	

Guess you like

Origin blog.csdn.net/sxh06/article/details/108473078