如果您想学习Java编程语言,最好的方法是通过实践来掌握它。在这篇文章中,我将介绍一些简单的Java入门小项目,可以帮助您开始学习 Java。(参考示例附在最后)

   1. 计算器应用程序

这是一个基本的Java项目,它可以让您了解Java编程语言的基础知识。您可以创建一个简单的计算器应用程序,该应用程序可以执行加、减、乘、除等基本数学运算。您可以使用Java Swing库为UI创建按钮和文本框,并使用Java中的数学函数来执行运算。

   2. 单词计数器

这是另一个简单的Java项目,可以帮助您加深对Java编程语言的理解。您可以创建一个程序,该程序读取输入文件中的文本,并计算每个单词出现的次数。您可以使用Java中的FileInputStream和BufferedReader类来读取文件内容,使用Map类来存储单词和其出现的次数。

   3. 简单的图书管理系统

这是一个介绍Java编程语言面向对象编程(OOP)概念的项目。您可以创建一个简单的图书管理系统,该系统可以添加、删除、查看和更新图书信息。您可以使用Java中的类和对象来表示图书和图书馆,使用数组或集合来存储多个图书。

   4. 简单的网络爬虫

这是一个有趣的Java项目,可以让您了解Java编程语言在网络编程方面的应用。您可以创建一个简单的网络爬虫,该爬虫可以从网页中提取信息。您可以使用Java中的URLConnection和BufferedReader类来访问网页内容,并使用正则表达式或其他技术来提取所需信息。

总之,这些Java入门小项目都非常适合初学者,它们涵盖了Java编程语言的不同方面。通过完成这些项目,您可以加深对Java编程语言的理解,并建立起自己的编程知识库。

参考示例:

  1. 计算器应用程序示例代码:
 
  import javax.swing.*;
  
import java.awt.event.*; public class Calculator extends JFrame implements ActionListener { private JTextField textField; private JButton addButton, subtractButton, multiplyButton, divideButton, equalsButton; private double firstNumber = 0.0; private double secondNumber = 0.0; private char operator; public Calculator() { setTitle("Simple Calculator"); setSize(300, 250); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel = new JPanel(); textField = new JTextField(15); textField.setHorizontalAlignment(JTextField.RIGHT); textField.setEditable(false); panel.add(textField); addButton = new JButton("+"); addButton.addActionListener(this); panel.add(addButton); subtractButton = new JButton("-"); subtractButton.addActionListener(this); panel.add(subtractButton); multiplyButton = new JButton("*"); multiplyButton.addActionListener(this); panel.add(multiplyButton); divideButton = new JButton("/"); divideButton.addActionListener(this); panel.add(divideButton); equalsButton = new JButton("="); equalsButton.addActionListener(this); panel.add(equalsButton); getContentPane().add(panel); setVisible(true); } public void actionPerformed(ActionEvent e) { if (e.getSource() == addButton) { operator = '+'; firstNumber = Double.parseDouble(textField.getText()); textField.setText(""); } else if (e.getSource() == subtractButton) { operator = '-'; firstNumber = Double.parseDouble(textField.getText()); textField.setText(""); } else if (e.getSource() == multiplyButton) { operator = '*'; firstNumber = Double.parseDouble(textField.getText()); textField.setText(""); } else if (e.getSource() == divideButton) { operator = '/'; firstNumber = Double.parseDouble(textField.getText()); textField.setText(""); } else if (e.getSource() == equalsButton) { secondNumber = Double.parseDouble(textField.getText()); double result = 0.0; switch (operator) { case '+': result = firstNumber + secondNumber; break; case '-': result = firstNumber - secondNumber; break; case '*': result = firstNumber * secondNumber; break; case '/': result = firstNumber / secondNumber; break; } textField.setText(Double.toString(result)); } } public static void main(String[] args) { new Calculator(); } }

   2. 单词计数器示例代码:

 
  import
   java.io.*;
  
import java.util.*; public class WordCounter { public static void main(String[] args) { Map<String, Integer> wordCountMap = new HashMap<>(); try { FileInputStream fileInputStream = new FileInputStream("input.txt"); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream, "UTF-8")); String line; while ((line = bufferedReader.readLine()) != null) { String[] words = line.split("\\s+"); // split by space or tab for (String word : words) { if (wordCountMap.containsKey(word)) { int count = wordCountMap.get(word); wordCountMap.put(word, count + 1); } else { wordCountMap.put(word, 1); } } } bufferedReader.close(); fileInputStream.close(); } catch (IOException e) { e.printStackTrace(); } System.out.println(wordCountMap); } }

   3. 简单的图书管理系统:

  
   import java.util.ArrayList;
   
import java.util.Scanner; public class Library { private ArrayList<Book> books; public Library() { books = new ArrayList<>(); } public void addBook(Book book) { books.add(book); } public void removeBook(Book book) { books.remove(book); } public void displayBooks() { for (Book book : books) { System.out.println(book); } } public static void main(String[] args) { Library library = new Library(); // 添加一些示例图书 library.addBook(new Book("The Great Gatsby", "F. Scott Fitzgerald", 1925)); library.addBook(new Book("To Kill a Mockingbird", "Harper Lee", 1960)); library.addBook(new Book("Pride and Prejudice", "Jane Austen", 1813)); Scanner scanner = new Scanner(System.in); while (true) { System.out.println("\n请选择一个操作:"); System.out.println("1. 显示所有图书"); System.out.println("2. 添加一本新图书"); System.out.println("3. 删除一本图书"); System.out.println("4. 退出"); int choice = scanner.nextInt(); switch (choice) { case 1: library.displayBooks(); break; case 2: System.out.print("请输入书名:"); String title = scanner.next(); System.out.print("请输入作者名:"); String author = scanner.next(); System.out.print("请输入出版年份:"); int year = scanner.nextInt(); library.addBook(new Book(title, author, year)); System.out.println("添加成功!"); break; case 3: System.out.print("请输入要删除的书名:"); String bookTitle = scanner.next(); boolean bookRemoved = false; for (Book book : library.books) { if (book.getTitle().equals(bookTitle)) { library.removeBook(book); bookRemoved = true; System.out.println("删除成功!"); break; } } if (!bookRemoved) { System.out.println("未找到该书,删除失败!"); } break; case 4: System.out.println("程序已退出。"); return; default: System.out.println("输入无效,请重新选择。"); } } } } class Book { private String title; private String author; private int year; public Book(String title, String author, int year) { this.title = title; this.author = author; this.year = year; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public int getYear() { return year; } public void setYear(int year) { this.year = year; } @Override public String toString() { return "书名:" + title + "\t作者:" + author + "\t出版年份:" + year; } }

   4. 简单的网络爬虫示例代码:

  
   import
    org.jsoup.Jsoup;
   
import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; import java.io.IOException; public class WebCrawler { private static final int MAX_DEPTH = 2; // 爬取的最大深度 private static int depth = 0; // 当前深度 public static void main(String[] args) { String url = "https://www.example.com"; // 起始 URL crawl(url); } public static void crawl(String url) { if (depth > MAX_DEPTH) { return; } try { Document document = Jsoup.connect(url).get(); // 获取页面文档对象 Elements links = document.select("a[href]"); // 获取所有链接元素 for (Element link : links) { String nextUrl = link.absUrl("href"); System.out.println(nextUrl); // 输出当前页的链接 // 递归爬取下一层页面 depth++; crawl(nextUrl); depth--; } } catch (IOException e) { e.printStackTrace(); } } }