Java basic syntax - breakpoint debugging and data encryption (basic syntax practice learning)

Table of contents

Eclipse breakpoint debugging

Basic grammar exercises

Breakpoint debugging of Eclipse

   Function: View the program execution flow and debug the program

   Breakpoint: It is a mark, is the debug we often use (to check program errors, we use debug as)

        A– Where to add ?—— Add before the actual program line number

        B–How to add?——Double-click the previous line number, and there will be a small dot, which is the breakpoint.

        C–How to run the program with breakpoint?——Right-click Debug As 

        D–Where to click?——Click step over (or shortcut key F6)

        E–Where are you looking?——There is a Debug view on the right

        F–How to remove breakpoints?——Double-click the breakpoint again, you can also Breakpoints(remove)

Basic grammar exercises

  1. Enter the month with the keyboard and output the corresponding season.
  2. Print all palindromes in 5 digits
  3. The Immortal Rabbit Question
  4. Find the sum of elements in an array that meets the requirements
  5. Referee score
  6.  Array reversal                                                                                                                                  
  7. Array basic search
  8. data encryption

Exercise 1: Enter the month with the keyboard and output the corresponding season

package com.demo;

import java.util.Scanner;

/*
 * 
 * 		3,4,5	春
 * 		6,7,8	夏
 * 		9,10,11	秋
 * 		12,1,2	冬
 * 
 * 分析:
 * 		A:键盘录入一个月份,用Scanner实现
 * 		B:判断该月份是几月,根据月份输出对应的季节
 * 			if
 * 			switch
 */
public class Test {
	public static void main(String[] args) {
		//键盘录入一个月份,用Scanner实现
		Scanner sc = new Scanner(System.in);
		
		//接收数据
		System.out.println("请输入月份(1——12):");
		int month = sc.nextInt();
		

		if(month>12 || month<1) {
			System.out.println("你输入的月份有误:");
		}else if((month == 1) || (month == 2) || (month == 12)) {
			System.out.println("冬");
		}else if((month == 3) || (month == 4) || (month == 5)) {
			System.out.println("春");
		}else if((month == 6) || (month == 7) || (month == 8)) {
			System.out.println("夏");
		}else if((month == 9) || (month == 10) || (month == 11)) {
			System.out.println("秋");
		}
	}
}

 Exercise 2:Print all palindromes in 5 digits

package com.demo;
/**
 * 需求:打印5位数中的所有回文数
 * 什么是回文数?个位与万相同,十位与万位相同
 * @author 无限嚣张(菜菜)
 *
 */
public class Test3 {
	public static void main(String[] args) {
		int i = 0;
		for(int x=10000; x<100000; x++) {
			int ge = x%10;
			int shi = x/10%10;
			int qian = x/10/10/10%10;
			int wan = x/10/10/10/10%10;
			
			if((ge == wan) && (shi == qian)) {
				System.out.println(x);
				i++;
			}
		}
		System.out.println(i);
	}
}

Exercise 3: The Immortal Rabbit Problem

package com.demo;

/*
 * 需求:
 * 		有一对兔子,从出生后第3个月起每个月都生一对兔子小兔子长到三个月后,每月又生一对兔子;
 * 		假如兔子都不死,问第二十个月的兔子对数多少?
 * 
 * 规律:
 * 		第一个月:1
 * 		第二个月:1
 * 		第三个月:2
 * 		第四个月:3
 * 		第五个月:5
 * 		...
 * 规律:从第三个月开始,每个月的兔子对数是前两个月兔子对数之和,第一个月和第二个月兔子对数都是2
 * 
 * 			...	
 */
public class Test4 {
	public static void main(String[] args) {
		//定义数组
		int[] arr = new int[20];
		
		//给数组元素赋值
		arr[0] = 1;
		arr[1] = 1;
		
		//找规律赋值ֵ
		for(int x=2; x<arr.length; x++) {
			arr[x] = arr[x-1] + arr[x-2];
		}
		
		//输出结果
		System.out.println("第二十个月兔子的对数是:"+arr[19]);
	}
}

Exercise 4: Find the sum of elements in the array that meets the requirements

package com.demo;

/*
 * 需求:
 * (1)定义一个int类型的一维数组:{171,72,19,16,118,51,210,7,18}
 * (2)求出该数组中满足要求的元素和
 * 		要求:求和的元素的个位和十位不能包含7,并且只能为偶数
 * 
 * 分析:
 * 		A:定义一个int类型的一维数组
 * 		B:定义一个求和变量
 * 		C:遍历数组,获取数组中的每一个元素
 * 		D:判断该元素是否满足条件,如果满足条件就累加
 * 			假设数据是x
 * 			个位不能是7,x%10 != 7
 * 			十位不能是7,x/10%10 != 7
 * 			必须是偶数x%2 == 0
 * 		E:输出求和结果
 */
public class Test5 {
	public static void main(String[] args) {
		//定义一个一维数组
		int[] arr = {171,72,19,16,118,51,210,7,18};
		
		//定义一个求和变量
		int sum = 0;
		
		//遍历数组
		for(int x=0; x<arr.length; x++) {
			//判断该元素是否满足条件,如果满足条件就累加
			if((arr[x]%10 != 7) && (arr[x]/10%10 != 7) && (arr[x]%2 == 0)) {
				sum += arr[x];
			}
		}
		
		//输出结果
		System.out.println("sum:"+sum);
	}
}

 Exercise 5:Referee’s Score

package com.demo;

import java.util.Scanner;

/*
 * 需求:在编程竞赛中,有6个评委为参赛的选手打分。分数为:0-100整数分
 * 选手的最后得分:去掉一个最高分,去掉一个最低分后的4个评委平均值
 * 
 * 
 * 
 */
public class Test6 {
	public static void main(String[] args) {
		//定义数组
		int[] arr = new int[6];
		
		//创建新对象
		Scanner sc = new Scanner(System.in);
		
		//键盘录入评委的分数
		for(int x=0; x<arr.length; x++) {
			//键盘录入评委分数
			System.out.println("请输入第"+(x+1)+"个评委给出的分数(0-100)");
			int score = sc.nextInt();
			arr[x] = score;
		}
		
		//写方法获取最大值和最小值
		//调用方法
		int max = getMax(arr);
		int min = getMin(arr);
		
		//求和
		int sum = sum(arr);
		
		//选手的最后得分:去掉一个最高分,去掉一个最低分后的4个评委平均值
		int avg = (sum-max-min)/(arr.length-2);
		
		//输出结果
		System.out.println("最后得分"+avg);
	}
	
	/*
	 * 求和
	 */
	public static int sum(int[] arr) {
		//定义初值
		int sum = 0;
		
		for(int x=0; x<arr.length; x++) {
			sum += arr[x];
		}
		
		return sum;
	}
	
	/*
	 * 找最小值
	 */
	public static int getMin(int[] arr) {
		//定义一个最小值
		int min = arr[0];
		
		for(int x=1; x<arr.length; x++) {
			if(arr[x] < min) {
				min = arr[x];
			}
		}
		
		return min;
	}
	
	/*
	 * 找最大值
	 */
	public static int getMax(int[] arr) {
		//定义一个最大值
		int max = arr[0];
		
		for(int x=1; x<arr.length; x++) {
			if(arr[x] > max) {
				max = arr[x];
			}
		}
		
		return max;
	}
}

 练习6:Number of pairs reversal

package com.demo;

import java.util.Scanner;


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

		int[] arr = new int[5];
		Scanner sc = new Scanner(System.in);
		for(int x=0; x<arr.length; x++) {
			System.out.println("请输入"+(x+1)+"个元素值");
			int number = sc.nextInt();
			arr[x] = number;
		}
		
		//反转前
		printArray(arr);
		
		//定义方法将arr数组中内容反转
		reverse(arr);
		
		//定义方法将反转后的数组进行遍历
		printArray(arr);
	}
	
	//
	public static void printArray(int[] arr) {
		System.out.print("[");
		for(int x=0; x<arr.length; x++) {
			if(x==arr.length-1) {
				System.out.println(arr[x]+"]");
			}else {
				System.out.print(arr[x]+", ");
			}
		}
	}
	
	/*
	 * 数组反转
	 *
	 */
	public static void reverse(int[] arr) {
		for(int start=0,end=arr.length-1; start<=end; start++,end--) {
			int temp = arr[start];
			arr[start] = arr[end];
			arr[end] = temp;
		}
	}
	
}

 Exercise 7:Basic search in array

/*
 *需求:数组元素查找(查找指定元素第一次在数组中出现的索引)
 */
public class Test8 {
	public static void main(String[] args) {
		//定义数组int[] arr = {5,7,3,2,5};
		int[] arr = {5,7,3,2,5};
		
		//键盘录入一个被查询的数据
		Scanner sc = new Scanner(System.in);
		
		System.out.println("请输入被查询的数据");
		int number = sc.nextInt();
		
		int index = getIndex(arr, number);
		System.out.println("index:"+index);
	}
	
	/*
	 * 查询索引
	 */
	public static int getIndex(int[] arr,int value) {
		//
		for(int x=0; x<arr.length; x++) {
			if(arr[x] == value) {
				return x;
			}
		}
		return -1;
	}
}

 Exercise 8:Data Encryption

package com.demo;

import java.util.Scanner;

/*
 * 需求:键盘录入数据,要求数据是4位整数,现对数据进行加密,加密规则如下:
 * 每位数字都加5,然后除以10的余数代替该数字
 * 再将第一位和第四位交换。第二位和第三位交换
 * 请把加密后的数据输出中到控制台
 */
public class Test9 {
	public static void main(String[] args) {
		//创建对象
		Scanner sc = new Scanner(System.in);
		//接收数据
		System.out.println("请输入4位整数数据");
		int number = sc.nextInt();
		
		//创建数组
		int[] arr = new int[4];
		arr[0] = number/10/10/10%10;
		arr[1] = number/10/10%10;
		arr[2] = number/10%10;
		arr[3] = number%10;
		

		for(int x=0; x<arr.length; x++) {
			arr[x] += 5;
			arr[x] %= 10;
		}
		
		int temp = arr[0];
		arr[0] = arr[3];
		arr[3] = temp;
		
		temp = arr[1];
		arr[1] = arr[2];
		arr[2] = temp;
		
		//输出结果ܺ
		System.out.println("加密后的数据:");
		for(int x=0; x<arr.length; x++) {
			System.out.print(arr[x]);
		}
		System.out.println();
	}
}

 

Guess you like

Origin blog.csdn.net/zywcxz/article/details/128771720