Java learning records ing...

foreword

As a record post, it mainly records some codes that the teachers in the school brought to practice, or some thinking questions raised.


1. Questions to think about

1. Why is the (byte) 128 output in java -128?
Answer: for reference only

2. What does String[] args mean when declaring main in JAVA ?

In JAVA, String[] args is the formal parameter of the main function. String[] args represents the parameters of the main function, representing string parameters.

String[ ] args function: When running java on the command line, you need to use the java command: java Test value1 value2, followed by two parameters. In the main function, args[] is an array of two lengths, value1 exists in args[0] , value2 exists in args[1].

There is such a statement in Java: public static void main(String[] args) . Among them, args is the Java command line parameter, and the "java file name args parameter" is used when executing the Java program in DOS. The args array can receive these parameters.

2. Code exercises

5.12 Java basic code

1.【Input and output code】

import java.util.Scanner;

public class basic {
    
    

	public static void main(String[] args) {
    
    
		// TODO Auto-generated method stub
        Scanner reader = new Scanner(System.in);
        System.out.print("请输入一个整数;");
        int i = reader.nextInt();
        System.out.println("输入的整数是:"+i);
	}

}

2.【HelloWrold】

public class helloworld {
    
    

	public static void main(String[] args) {
    
    
		// TODO Auto-generated method stub
        System.out.println("hello java world!");
	}

}

3. [Find the area and circumference of a circle]

Circle class

class Circle {
    
       //定义一个类
    double radius;   //类的属性,变量小写  属性通常是名词
    final double PAI = 3.14 ;   //常量大写,并且用final固定值
    
    double getArea() {
    
       //类的操作
    	return PAI * radius * radius;
    	
    }
    
    double getCirumference() {
    
      //方法的命名采用动宾结构,代表做事
    	return 2 * PAI * radius;
    	
    }
}

Main

public class Main {
    
    

	public static void main(String[] args) {
    
    
		// TODO Auto-generated method stub
        Circle c1 = new Circle ();   //声明一个圆类的对象的引用变量,再通过赋值分配存储空间给c1这个引用变量,完成实例化
        c1.radius = 2 ; //通过.使用这个对象的属性
        System.out.println("圆的半径为:"+ c1.radius);
        System.out.println("圆的面积为:" +c1.getArea());
        System.out.println("圆的周长为:" +c1.getCirumference());
	}

}

6.8 Final Review Stage丨Learning Process

1. [Input and output flow template]

//输入输出基本模式
import java.io.InputStream;
import java.io.OutputStream;

public class Main {
    
    

	public static void main(String[] args) {
    
    	
		
		//1、实例化输入输出流类的对象
		InputStream in = new InputStream();
		OutputStream out= new OutputSream();
		//2、调用输入输出流对象的读写数据方法
		in.read();
		out.write();
		//关闭输入输出
		in.close();
		out.close();
	}
}

2. [File input and output template]

/*文件的输入输出基本模式*/
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class Main {
    
    

	public static void main(String[] args) {
    
    	
		
		//1、实例化输入输出流类的对象
		FileInputStream in = null;
		FileOutputStream out= null;
		
		try {
    
    
			//创建了两个文件输入输出的字节流
			in = new FileInputStream("data.txt");
			out = new FileOutputStream("out.txt");
			
			//2、调用输入输出流对象的读写数据方法
			int c;
			while((c = in.read()) != -1)
				out.write(c);
		} catch (FileNotFoundException e) {
    
    
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		} catch (IOException e) {
    
    
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}finally {
    
    
			//3、关闭输入输出
			try {
    
    
				if(in != null)
					in.close();
				if(out != null)
					out.close();
			} catch (IOException e) {
    
    
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}					
		}	
	}
}

operation result:
The input file is data and the output file is out

Guess you like

Origin blog.csdn.net/weixin_52248428/article/details/124735111