引言
Java作为一种广泛应用于企业级应用、Android开发以及Web开发的编程语言,其强大的功能和丰富的库让许多开发者对其情有独钟。通过解决各种编程挑战,不仅能够巩固已有的Java知识,还能激发创新思维,提升编程技能。本文将为您带来一些有趣的Java编程题目,帮助您在轻松愉快的氛围中提升编程能力。
一、基础算法题
1. 冒泡排序
问题描述:实现一个冒泡排序算法,对整数数组进行排序。
代码示例:
public class BubbleSort {
public static void bubbleSort(int[] arr) {
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
public static void main(String[] args) {
int[] arr = {64, 34, 25, 12, 22, 11, 90};
bubbleSort(arr);
System.out.println("Sorted array:");
for (int i : arr) {
System.out.print(i + " ");
}
}
}
2. 素数检测
问题描述:编写一个函数,检测一个整数是否为素数。
代码示例:
public class PrimeNumber {
public static boolean isPrime(int num) {
if (num <= 1) {
return false;
}
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) {
return false;
}
}
return true;
}
public static void main(String[] args) {
int num = 29;
if (isPrime(num)) {
System.out.println(num + " is a prime number.");
} else {
System.out.println(num + " is not a prime number.");
}
}
}
二、数据结构与算法进阶
1. 链表操作
问题描述:实现一个单链表,包括插入、删除、查找等基本操作。
代码示例:
public class LinkedList {
static class Node {
int data;
Node next;
Node(int d) {
data = d;
next = null;
}
}
static Node head;
public static void insert(int data) {
Node newNode = new Node(data);
newNode.next = head;
head = newNode;
}
public static boolean search(int key) {
Node current = head;
while (current != null) {
if (current.data == key) {
return true;
}
current = current.next;
}
return false;
}
public static void delete(int key) {
Node temp = head, prev = null;
if (temp != null && temp.data == key) {
head = temp.next;
return;
}
while (temp != null && temp.data != key) {
prev = temp;
temp = temp.next;
}
if (temp == null) return;
prev.next = temp.next;
}
public static void main(String[] args) {
insert(1);
insert(2);
insert(3);
System.out.println("List: " + search(2));
delete(2);
System.out.println("List after deleting 2: " + search(2));
}
}
2. 字符串匹配算法
问题描述:实现KMP字符串匹配算法,查找子字符串在主字符串中的位置。
代码示例:
public class KMPAlgorithm {
public static void KMPSearch(String pat, String txt) {
int M = pat.length();
int N = txt.length();
int[] lps = new int[M];
int j = 0;
// Preprocess the pattern (calculate lps[] array)
computeLPSArray(pat, M, lps);
int i = 0; // index for txt[]
while (i < N) {
if (pat.charAt(j) == txt.charAt(i)) {
j++;
i++;
}
if (j == M) {
System.out.println("Found pattern at index " + (i - j));
j = lps[j - 1];
}
// Mismatch after j matches
else if (i < N && pat.charAt(j) != txt.charAt(i)) {
// Do not match lps[0..lps[j-1]] characters,
// they will match anyway
if (j != 0)
j = lps[j - 1];
else
i = i + 1;
}
}
}
public static void computeLPSArray(String pat, int M, int[] lps) {
// length of the previous longest prefix suffix
int len = 0;
int i = 1;
lps[0] = 0; // lps[0] is always 0
// the loop calculates lps[i] for i = 1 to M-1
while (i < M) {
if (pat.charAt(i) == pat.charAt(len)) {
len++;
lps[i] = len;
i++;
} else {
if (len != 0) {
len = lps[len - 1];
} else {
lps[i] = len;
i++;
}
}
}
}
public static void main(String[] args) {
String txt = "ABABDABACDABABCABAB";
String pat = "ABABCABAB";
KMPSearch(pat, txt);
}
}
三、实战项目
1. 计算器
问题描述:实现一个简单的计算器,支持加、减、乘、除四种运算。
代码示例:
import java.util.Scanner;
public class SimpleCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter first number: ");
double num1 = scanner.nextDouble();
System.out.println("Enter second number: ");
double num2 = scanner.nextDouble();
System.out.println("Enter operator (+, -, *, /): ");
char operator = scanner.next().charAt(0);
switch (operator) {
case '+':
System.out.println("Result: " + (num1 + num2));
break;
case '-':
System.out.println("Result: " + (num1 - num2));
break;
case '*':
System.out.println("Result: " + (num1 * num2));
break;
case '/':
if (num2 != 0)
System.out.println("Result: " + (num1 / num2));
else
System.out.println("Division by zero is not allowed.");
break;
default:
System.out.println("Invalid operator!");
break;
}
scanner.close();
}
}
结语
通过以上趣味编程题目,相信您已经在轻松愉快的氛围中提升了Java编程技能。在编程道路上,不断挑战自我,勇于创新,才能不断进步。希望这些题目能对您的编程之路有所帮助。
