Java basic exercises

Case: weight loss plan

Requirements:
Enter the number of weeks to display today's weight loss activities
Monday: running
Tuesday: swimming
Wednesday: walk slowly
Thursday: spinning
Friday: Boxing
Saturday: mountain climbing
Sunday: have a good meal
if version:

//Weight loss plan if version
import java.util.Scanner;

public class WeightLossPlan {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("Please enter the number of weeks:");
		int week = sc.nextInt();
		if (week < 1 || week > 7) {
			System.out.println("The number of weeks you entered is incorrect");
		} else if (week == 1) {
			System.out.println("Monday: running");
		} else if (week == 2) {
			System.out.println("Tuesday: swimming");
		} else if (week == 3) {
			System.out.println("Wednesday: walk slowly");
		} else if (week == 4) {
			System.out.println("Thursday: spinning");
		} else if (week == 5) {
			System.out.println("Friday: Boxing");
		} else if (week == 6) {
			System.out.println("Saturday: mountain climbing");
		} else if (week == 7) {
			System.out.println("Sunday: have a good meal");
		}

	}

}

switch version:

//Weight loss plan switch version
import java.util.Scanner;

public class WeightLossPlan {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("Please enter the number of weeks:");
		int week = sc.nextInt();
		switch (week) {
		case 1:
			System.out.println("Monday: running");
			break;
		case 2:
			System.out.println("Tuesday: swimming");
			break;
		case 3:
			System.out.println("Wednesday: walk slowly");
			break;
		case 4:
			System.out.println("Thursday: spinning");
			break;
		case 5:
			System.out.println("Friday: Boxing");
			break;
		case 6:
			System.out.println("Saturday: mountain climbing");
			break;
		case 7:
			System.out.println("Sunday: have a good meal");
			break;
		default:
			System.out.println("The number of weeks you entered is incorrect");
		}
	}
}

Operation results:

Case: every seven

Demand: when friends get together, they may play a game: every seven. The rule is: start counting from any number. When the number you want to report contains 7 or a multiple of 7, you should say: too. In order to help you better play the game, here we print out the data between 1 – 100 on the console that meets the rule of passing every seven. In this way, when you play games in the future, you will know that the data have to say: Yes

The code is as follows:

//Pass every seven days: directly print out the data between 1-100 that meets the rule of pass every seven days on the console
//The number contains 7 or a multiple of 7
public class SevenPass {
	public static void main(String[] args) {
		for (int i = 1; i < 101; i++) {
			if (i % 7 == 0 | i % 10 == 7 | i / 10 % 10 == 7) {
				System.out.print(i + " ");
			}
		}
	}
}

Operation results:

Case: Immortal rabbit

Demand: there is a pair of rabbits. They give birth to a pair of rabbits every month from the third month after birth. When the little rabbits grow to the third month, they give birth to another pair of rabbits every month. If the rabbits don't die, what is the logarithm of the rabbits in the twentieth month?
The code is as follows:

//Immortal rabbit
//Law finding:
//First month: 1 pair
//Second month: 1
//The third month: 2
//The fourth month: 3
//Fifth month: 5
//Starting from the third, each data is the sum of the first two data, and the first and second are known
public class UndeadRabbit {
	public static void main(String[] args) {
		int[] arr = new int[20];
		arr[0] = 1;// Known first two months
		arr[1] = 1;
		for (int i = 2; i < arr.length; i++) {
			arr[i] = arr[i - 1] + arr[i - 2];
		}
		System.out.println(arr[19]);
	}
}

Operation results:

Case: a hundred dollars and a hundred chickens

Demand: the mathematical problem put forward by Chinese ancient mathematician Zhang Qiujian in the book "Suanjing": a chicken Weng is worth five, a chicken mother is worth three, and a chicken chick is worth three. How about a chicken owner, a chicken mother and a chicken chick?
The code is as follows:

//A hundred dollars and a hundred chickens: a chicken Weng is worth five, a chicken mother is worth three, and a chicken chick is worth three. When you buy a hundred chickens for a hundred dollars, ask the chicken owner, the chicken mother and the chicken chick
//Let x,y,z
//x+y+z=100
//5x+3y+z/3=100
//If you buy one alone
//0<=x<=20
//0<=y<=33
//0<=z<=100
public class hundredChikens {
	public static void main(String[] args) {
		int x, y, z;
		for (x = 0; x < 21; x++) {// Chicken Weng range
			for (y = 0; y < 34; y++) {// Chicken range
				z = 100 - x - y;// Chick
				if (z % 3 == 0 & 5 * x + 3 * y + z / 3 == 100) {
					System.out.println("Chicken Weng:" + x + " Hen:" + y + " Chicks:" + z);
				}
			}
		}
	}
}

Operation results:

Case: summation of array elements

Requirement: there is an array with elements of {68,27,98171996,51210}. Find the sum of elements that meet the requirements in the array. The requirements are: the single bit and ten bit of the summation element cannot be 7, and can only be an even number
The code is as follows:

//Requirement: there is an array with elements of {68,27,88171996,51210}.
//Find the sum of elements that meet the requirements in the array,
//The requirement is: the sum element can't have 7 bits and 10 bits, and can only be an even number
public class ArraySum {
	public static void main(String[] args) {
		int[] arr = { 68, 27, 88, 171, 996, 51, 210 };
		int sum = 0;
		for (int i = 0; i < arr.length; i++) {
			if (arr[i] % 2 == 0 && arr[i] / 10 % 10 != 7) {
				sum = sum + arr[i];
			}
		}
		System.out.println(sum);
	}
}

Case: the contents of the array are the same

Requirements: design a method to compare whether the contents of two arrays are the same
The code is as follows:

//The contents of the array are the same
//Requirements: design a method to compare whether the contents of two arrays are the same
//First compare the length, and then traverse each element
public class ArraySame {
	public static void main(String[] args) {
		int[] arr1 = { 11, 22, 33, 44, 55 };
		int[] arr2 = { 11, 22, 33, 44, 55 };
		System.out.println(isSame(arr1, arr2));

	}

	public static boolean isSame(int[] arr1, int[] arr2) {
		if (arr1.length != arr2.length) {
			return false;
		} else {
			for (int i = 0; i < arr1.length; i++) {
					System.out.println(arr1[i]+"--"+arr2[i]);
					if (arr1[i] != arr2[i]) {
						return false;
					}
				}
		}
		return true;
	}
}

Operation results:

Cases: finding

Requirement: know an array arr={19,28,37,46,50}; Enter a data on the keyboard, find the index of the data in the array, and output the found index on the console
The code is as follows:

import java.util.Scanner;

//lookup
//Requirement: an array arr={19,28,37,46,50} is known;
//Enter a data on the keyboard, find the index of the data in the array, and output the found index on the console
//If the entered number does not exist, - 1 is returned
public class Search {
	public static void main(String[] args) {
		int[] arr = { 19, 28, 37, 46, 50 };
		Scanner sc = new Scanner(System.in);
		System.out.println("Please enter the data to find:");
		int n = sc.nextInt();
		int index = -1;// Define an index with an initial value of - 1
		for (int i = 0; i < arr.length; i++) {
			if (n == arr[i]) {
				index = i;
			}
		}
		System.out.println("Index of this data:" + index);
	}
}

Operation results:

Case: reverse

Requirement: an array arr={19,28,37,46,50} is known; Use the program to exchange the element values in the array
Array after exchange arr={50,46,37,28,19}; And output the exchanged array elements on the console
The code is as follows:

import java.util.Iterator;

//reversal
//Requirement: an array arr={19,28,37,46,50} is known; Use the program to exchange the element values in the array
//Array after exchange arr={50,46,37,28,19}; And output the exchanged array elements on the console
public class FanZhuan {
	public static void main(String[] args) {
		int[] arr = { 19, 28, 37, 46, 50 };
		//Method I:
		for (int i = 0; i < arr.length; i++) {
			if (i < arr.length - 1 - i) {// The start index is smaller than the end index
				if (arr[i] != arr[arr.length - 1 - i]) {
					int temp = arr[i];// Define intermediate variables
					arr[i] = arr[arr.length - 1 - i];
					arr[arr.length - 1 - i] = temp;
				}
			} else if (i <= arr.length - 1 - i) {
				arr[i] = arr[arr.length - 1 - i];
			}
		}
		//Method 2: define two indexes
//		for (int start = 0, end = 0; start <= end; start++, end--) {
//			if (arr[start] != arr[end]) {
//				int temp = arr[start];//  Define intermediate variables
//				arr[start] = arr[end];
//				arr[end] = temp;
//			}
//		}
		// output
		for (int i = 0; i < arr.length; i++) {
			System.out.print(arr[i] + " ");
		}
	}

}

Operation results:

Case: judges scoring

Requirements: in the programming competition, 6 judges will score the contestants, and the score is an integer of 0 – 100
The final score of the contestant is: the average value of the four scores after removing the highest score and the lowest score, regardless of the decimal part
The code is as follows:

import java.util.Scanner;

//Judges score
//There are 6 judges to score the contestants, and the score is an integer of 0-100
//The final score of the contestant is the average of the four scores after removing the highest score and the lowest score, regardless of the decimal part
public class PingweiDafen {
	public static void main(String[] args) {
		int[] arr = new int[6];
		Scanner sc = new Scanner(System.in);
		System.out.println("Please enter the scores of 6 judges:");
//		int mark;//  fraction
		for (int i = 0; i < 6; i++) {
//			mark = sc.nextInt();
//			arr[i] = mark;
			arr[i] = sc.nextInt();
		}
		System.out.print("fraction:");
		for (int i = 0; i < arr.length; i++) {
			System.out.print(arr[i] + " ");
		}

		int min = ismin(arr);
		int max = ismax(arr);
		int sum = issum(arr);
		int average = (sum - min - max) / 4;
		System.out.println();
		System.out.println("Final score:" + average);

	}

	public static int issum(int[] arr) {// Element summation
		int sum = 0;
		for (int i = 0; i < arr.length; i++) {
			sum += arr[i];
		}
		return sum;
	}

	public static int ismax(int[] arr) {// Get maximum
		int max = arr[0];
		for (int i = 1; i < arr.length; i++) {
			if (arr[i] > max) {
				max = arr[i];
			}
		}
		return max;
	}

	public static int ismin(int[] arr) {// Get minimum value
		int min = arr[0];
		for (int i = 1; i < arr.length; i++) {
			if (arr[i] < min) {
				min = arr[i];
			}
		}
		return min;
	}
}

Operation results:

Keywords: Java

Added by naitsir on Fri, 21 Jan 2022 00:48:18 +0200