Java from entry to abandonment 2 (Java method creation and calling, simple use of if)

Yesterday, I started the Java journey, I learned about the Java routine code. Java's main function (method) is written in a fixed way, and every Java program must have a main method, which is the entry point of the Java program. Today, because I have learned the python language before, it is relatively easy to understand the basic concepts of Java in a strict sense. So, today I read the example in the Java book, first tried functional programming, and I added if condition judgment, if the basic concept of Java: function (method), comrades who are not clear about if condition judgment, you can refer to : Java's if judgment , Java's method .

Don't say much, do it first! ! The code is shown in the figure below, and the explanation of the code is in the comments.

class example1{
    
    
	public static void main(String[] args) {
    
    
		int n = 4;
		byte b = (byte) n;//红色的关键字,绿色的注释,语句,方法,类
		System.out.println("b的值是"+b);
		System.out.println();//打印一个空行
		introduce("Mr.Pan_学狂",21);/*在主方法中调用函数(方法)并且传递实参name,age*/
		Number();//函数没有设置参数,调用时执行函数体内部的语句
	}
	
	public static void introduce(String name,int age) {
    
    //创建一个名为introduce的静态方法。设置name,age为形参
		//name = "Mr.Pan_学狂";
		//age = 21;
		System.out.println("作者:"+name);//打印输出姓名
		System.out.println("年龄:"+age+"岁");//打印输出年龄
		System.out.println();//打印一个空行
		
	}
	
	public static void Number() {
    
    
		System.out.println("比较a与b的大小");//打印标题
		int b = 27;
		double a = 13.50;
		if (a > b) {
    
    //if条件判断
			System.out.println("这是错误的结果");//这个结果不会输出,因为b > a,所以不满足条件,则不会执行语句
		}
		else if(a < b) {
    
    //再次判断条件b > a满足。所以,输出函数体内部的语句块
			System.out.println("a的值是: "+a);
			System.out.println("b的值是: "+b);
			System.out.println("b > a");
		}
	}
}


If you need to run this code, you need to create the example1.java file like me, and then put the code in to run. The result is as follows:
Insert picture description here
Today, I made a simple little chestnut, hoping to be helpful to friends who are new to Java.
Finally, thank you all for coming to watch my article. There may be many improprieties in the article, and I hope to point out He Haihan.

Guess you like

Origin blog.csdn.net/weixin_43408020/article/details/113921633