[Java] Code Formatting Tutorial in Eclipse

1. Manual formatting

1. Open the program, select the code to be formatted 2. Click the menu
insert image description here
on the menu bar 3. Click the option on the drop-down menu 4. The code has been formatted Source code:Source
insert image description here
Format
insert image description here

insert image description here

package TestDemo;

import java.util.Scanner;

public class Cipher {
    
    
	private static int[] enc_key = {
    
     6, 3, 0, 4, 5, 1, 2 }; // 加密密钥
	private static int[] dec_key = {
    
     2, 5, 6, 1, 3, 4, 0 }; // 解密密钥

	public static void main(String[] args) {
    
    
		char[][] m = new char[2][7];
		char[][] c = new char[2][7];
		char[][] dec_m = new char[2][7];
		System.out.println("请输入两行7个英文字符的明文:");
		try (Scanner scanner = new Scanner(System.in)) {
    
    
			m[0] = scanner.nextLine().toCharArray();
			m[1] = scanner.nextLine().toCharArray();
			for (int i = 0; i < 7; i++) {
    
    
				c[0][enc_key[i]] = m[0][i];
				c[1][enc_key[i]] = m[1][i];
			}
			for (int i = 0; i < 7; i++) {
    
    
				dec_m[0][dec_key[i]] = c[0][i];
				dec_m[1][dec_key[i]] = c[1][i];
			}
			System.out.println("您输入的原始明文为:");
			System.out.println(m[0]);
			System.out.println(m[1]);
			System.out.println("加密结果为:");
			System.out.println(c[0]);
			System.out.println(c[1]);
			System.out.println("解密结果为:");
			System.out.println(dec_m[0]);
			System.out.println(dec_m[1]);
		} catch (Exception e) {
    
    
			System.err.println(e);
		}
	}
}

2. Automatic formatting

1. In the menu bar, click Windowthe menu
insert image description here
2, select in the drop-down bar Preferences
insert image description here
3, click Java, select Editor, and then click Save Actionsoption
insert image description here
4, the default is not checked, just check it
insert image description here
5, simply test
the code:

package TestDemo;

public class Rotor {
    
    
	public static void main(String args[])
	{
    
    
	String str="rotor",sub1;
	int i=0,n;
	boolean yes=true;
	if (args.length>0)
	str=args[0];
	System.out.println("str="+str);
	n=str.length();
	char ch1,ch2;
	String temp="";
	for (i=0;i<n;i++) {
    
    
	sub1=str.substring(i,i+1);
	temp=sub1+temp; }
	System.out.println("temp="+temp);
	System.out.println(str+":"+str.equals(temp));
	}
}

Ⅰ. The newly created class has not saved the code
insert image description here
Ⅱ. After the code is written, Ctrl+ssave it and the code will be automatically formatted
insert image description here

Guess you like

Origin blog.csdn.net/Cappuccino_jay/article/details/130386433