加载中...

How much do you remember about these Java syntax errors? (This article is intended for Java beginners)

This article explains

After learning java basic grammar, I found that many basic theories were not very clear. According to my own needs, I consulted various information forums and blogs. After my sorting and summary, I wrote this error prone basic review blog. The materials and pictures in the article draw lessons from high-quality blogs and forums.

一.Overview of contents involved

1.1 basic structure of Java programming
1.2 how does "scanner" input?
1.3 what is the difference between "break" and "continue"?
1.4 continue with label
1.5 call of test class
1.6 testing constctor constructor
1.7 test method
1.8 test object
1.9 test overload
2.0 test override
2.1 testing extensions inheritance
2.2 test interface
2.3 testing abstract classes
2.4 test internal class
2.5 how to use foreach loop?
2.6 test recursion and iteration efficiency test

二.SHOW YOUR CODE

1.1 basic structure of Java programming

public class TestJava {
        public static void main(String[] args) {
        
        }
}

1.2 how does “Scanner” input?

import java.util.Scanner;

public class TestScanner {
        public static void main(String[] args) {
                Scanner scanner = new Scanner(System.in);
                String name = scanner.nextLine();
                int age = scanner.nextInt();
	
                System.out.println("My name is:"+name);
                System.out.println("My age is:"+age);	
        }	
}

1.3 what is the difference between "break" and "continue"?

Break's tests are as follows:

public class TestBreak {
        public static void main(String[] args) {	
                //定义计时器
                int total = 0;
                //生成随机数
                while(true) {
                        total++;	//每循环一次加一
                        int i = (int)Math.round(100*Math.random());
                        System.out.println(i);
                        if(i == 98) {
                            break;
                        }
                }
                //输出循环的次数
                System.out.println("Game is over,used "+total+" time");
        }

}

Continue's tests are as follows:

public class TestContinue {
        public static void main(String[] args) {
                //记录total
                int total = 0;
                System.out.println("Begin");
                        total++;
                        for(int i=0;i<10;i++) {
                                if(i%2==0) {
                                        continue;
                                }
                                System.out.println(i);
                        }
        }
}

Summary: break can be used to jump out of the cycle, and continue is used to end this cycle and continue to the next cycle

1.4 Continue with label

public class TestLabelContinue {
        public static void main(String[] args) {
                //打印100~150之间的质数
                outer:for(int i=100;i<=150;i++) {
                        for(int j=2;j<i/2;j++) {
                                if(i%j==0) {
                                    continue outer;
                                }
                        }
                        System.out.println(i+" ");
                }
        }
}

1.5 Call of test class

 public class TestClass {

        //属性fields
        int id;
        String sname;
        int age;

        Computer comp;	//计算机

        //方法
        void study() {
                System.out.println("我在学习"+comp.brand);
        }

        void play() {
                System.out.println("我在打游戏");
        }

        public static void main(String[] args) {
                TestClass stu = new TestClass();
                stu.id = 20201528;
                stu.sname = "eason";
                stu.age =18;
                
                Computer c1 = new Computer();
                c1.brand = "联想";
                stu.comp = c1;
	
                stu.play();
                stu.study();
        }
}

class Computer {
    String brand;	//品牌
 }

1.6 Test constctor constructor

class Point {
        double x, y;

        //构造方法名称和类名一致
        public Point(double _x, double _y) {
                x = _x;
                y = _y;
        }
        //算距离
        public double getDistance(Point p) {
                return Math.sqrt((x-p.x)*(x-p.x)+(y-p.y)*(y-p.y));
        }
}

public class TestConstctor {

        public static void main(String[] args) {
                Point p = new Point(3.0, 4.0);
                Point origin =new Point(0.0, 0.0);
	
                System.out.println(p.getDistance(origin));
        }

}

1.7 Test method

public class TestMethod {
        //通过对象调用普通方法
        public static void main(String[] args) {
                TestMethod tm = new TestMethod();
                tm.printDate();
                int d = tm.add(1, 2, 3);
                System.out.println(d);
        }

        void printDate() {
                System.out.println("今年18岁");
        }

        int add(int a,int b,int c) {
                return a+b+c;
        }
}

1.8 Test object

public class TestObject {

        public static void main() {
	
                TestObject to = new TestObject();
                System.out.println(to.toString());
	
        }

        public String toString() {
                return "测试Object对象";
        }
}

1.9 Test overload

public class TestOverload {

        public static void main(String[] args) {
	
        }

        public static void add(int a, int b) {
                System.out.println(a+b);
        }

        //不构成重载
        /*
         //仅有形参不同
         public static void add(int b, int a){
                System.out.println(a+b);
         }
         //仅有返回值类型不同
         public static int add(int a, int b){
         }
  
         */

}

2.0 Test override

public class TestOverride {

        public static void main(String[] args) {
                Car car = new Car();
	
                car.run();
                car.stop();
        }
}
//Vehicle
class Vehicle {
        public void run() {
                System.out.println("跑");
        }
        public void stop() {
                System.out.println("停下!");
        }
        public Person Whoispsg() {
                return new Person();
        }
}
//Car
class Car extends Vehicle {
        public void run() {
                System.out.println("开车");
        }
        public Student Whoispsg() {
                return new Student();
        }
}

2.1 Test extensions

public class TestExtends {
    
        public static void main(String[] args) {
                Student stu = new Student();
                stu.name = "eason";
                stu.height = 164;
                stu.major = "挖掘机";
	
                stu.rest();
                stu.study();
	
                System.out.println(stu instanceof Person);
                System.out.println(stu instanceof Object);
        }
}

class Person /*extends Object*/{
        String name;
        int height;

        public void rest() {
                System.out.println("休息一下");
        }
}

class Student extends Person {
        String major;

        public void study() {
                System.out.println("学习学习");
        }
}

2.2 Test interface

public class TestInterface {

        public static void main(String[] args) {
                Volant v = new Angel();
                v.fly();
                Honest h1 = new GoodMan();
                h1.helpOther();
                Honest h2 = new BadMan();
                h2.helpOther();
        }

}	

         /**
         * 飞行接口
         * @author RealH
         *
         */
interface Volant {
        int FLY_HEIGHT = 1000;
        void fly();
}

//善良接口
interface Honest {
        void helpOther();
}
//Angel
class Angel implements Volant,Honest {//实现类可以实现多个父接口

        @Override
        public void helpOther() {
                // TODO Auto-generated method stub
                System.out.println("Angel.helpOther();");
        }

        @Override
        public void fly() {
                // TODO Auto-generated method stub
                System.out.println("Angel.fly();");
        } 

}
//GoodMan
class GoodMan implements Honest {

        @Override
        public void helpOther() {
                // TODO Auto-generated method stub
                System.out.println("GoodMan.helpOther();");
        }

}
//BadMan
class BadMan implements Honest {

        @Override
        public void helpOther() {
                // TODO Auto-generated method stub
                System.out.println("BadMan.helpOther();");
        }

}

2.3 Test abstract class

public abstract class Animal {
        //没有实现,但子类必须有实现,子类必须重写,抽象类不能new,只能用来继承
        abstract public void shout();

        public void run() {
                System.out.println("跑");
        }

        public static void main(String[] args) {
                Animal a = new Dog();

                a.shout();
        }

}

class Dog extends Animal {
        @Override
        public void shout() {
                System.out.println("汪!");
        }
}

2.4 Test inner class

public class TestInnerClass {
        public static void main(String[] args) {

                //创建内部类对象
                Outer.Inner inner = new Outer().new Inner();
                inner.show();
        }
}

class Outer {
        private int age = 10;

                class Inner {
                        int age = 20;
                        public void show() {
                                int age = 30;
                                System.out.println("外部类的成员变量age:"+Outer.this.age);
                                System.out.println("内部类的成员变量age:"+this.age);
                                System.out.println("局部变量age:"+age);
                        }
                }
}

2.5 How do I use a foreach loop?

public class TestForeach {

        public static void main(String[] args) {
                int[] a = new int[4];
                System.out.println(a.length);
                //初始化
                for(int i = 0; i < a.length; i++) {
                        a[i] = 100*i;
                }

                //读取元素
                //for(int j = 0; j <= a.length; j++) {
                //	System.out.println(a[j]);
                //}
                System.out.println("#########");
                //foreach循环来读取数组元素
                for(int m : a) {
                        System.out.println(m);
                }

                String[] ss = {"aa", "bb", "cc", "dd"};
                for(String temp : ss) {
                        System.out.println(temp);
                }
        }
}

2.6 Test recursion and iterative efficiency test

public class TestRecursion {

        public static void main(String[] args) {
                long d1 = System.currentTimeMillis();	//返回当时的时刻毫秒数ms
                System.out.printf("%d的阶乘结果为:%s\n", 10, factorial(10));
                long d2 = System.currentTimeMillis();
                System.out.printf("递归的耗时为:%s\n", d2-d1);
        }
        //递归必须有递归头和递归体
        static long factorial(int n) {
                if(n == 1) {	//递归头
                        return 1;
                } else {		//递归体
                        return n*factorial(n-1);
                }
        }

}
posted @ 2021-07-20 06:00  nongeason  阅读(46)  评论(0)    收藏  举报