java笔记

java笔记_第1张图片

package test2;

public class Puppy{
	   int age;										//属性
	   public Puppy(String name)					//init
	   {
		   System.out.println("build succeed!!  you name is :" + name);
	   }
	   public void fix_age(int input_age)			//方法1
	   {
		   age = input_age;
	   }
	   public int get_age()							//方法2
	   {
		   return age;
	   }
	   
	   public static void main(String []args)
	   {
		   Puppy mypuppy = new Puppy("liu");
		   
		   mypuppy.fix_age(20);
		   
		   System.out.println(mypuppy.get_age(););
	   }
	}


package test2;
public class Puppy{
	   
	   public static void main(String []args)
	   {
		  //输入
		   		
		  //输出
		   		System.out.println();			//输出换行
		   		System.out.print("你好");			//不换行
		   		System.out.println();			//输出换行
		   //格式化输出
		   		double d = 3.1415926;
		        System.out.printf("%.2f\n", d); // 显示两位小数3.14
		   //数组操作
		        int []lis = {2,1,3,5,6};
		        //迭代方式1
		        for(int i=0;i<lis.length;i++)
		        {
		        	System.out.printf("%d\n",lis[i]);
		        }
		        //迭代方式2
		        for(int i : lis)
		        {
		        	System.out.printf("%d\n",i);	//变量n直接拿到ns数组的元素,而不是索引。
		        	System.out.print(i+",");
		        }
		    //二维数组
		        int[][] ns = {
		                { 1, 2, 3, 4 ,4},
		                { 5, 6, 7 },
		                { 9, 10, 11, 12 }
		            };
		            System.out.println(ns.length); //3
		         //遍历方法
		         for(int []i : ns)
		         {
		        	 for(int j :i)
		        	 {
		        		 System.out.print(j+" ");
		        	 }
		        	 System.out.print("\n");
		         }
		       
		        
	   }
	   
	   
	   /*
	    Scanner scanner = new Scanner(System.in); // 创建Scanner对象
        System.out.print("Input your name: "); // 打印提示
        String name = scanner.nextLine(); // 读取一行输入并获取字符串
        System.out.print("Input your age: "); // 打印提示
        int age = scanner.nextInt(); // 读取一行输入并获取整数
        System.out.printf("Hi, %s, you are %d\n", name, age); // 格式化输出
	    */
	}

你可能感兴趣的:(java笔记)