App下載

Java入門(mén)簡(jiǎn)單小項(xiàng)目:w3cschool助你學(xué)Java!

小丸子的西瓜夢(mèng) 2023-05-29 11:53:27 瀏覽數(shù) (2050)
反饋

如果您想學(xué)習(xí)Java編程語(yǔ)言,最好的方法是通過(guò)實(shí)踐來(lái)掌握它。在這篇文章中,我將介紹一些簡(jiǎn)單的Java入門(mén)小項(xiàng)目,可以幫助您開(kāi)始學(xué)習(xí) Java。(參考示例附在最后)

   1. 計(jì)算器應(yīng)用程序

這是一個(gè)基本的Java項(xiàng)目,它可以讓您了解Java編程語(yǔ)言的基礎(chǔ)知識(shí)。您可以創(chuàng)建一個(gè)簡(jiǎn)單的計(jì)算器應(yīng)用程序,該應(yīng)用程序可以執(zhí)行加、減、乘、除等基本數(shù)學(xué)運(yùn)算。您可以使用Java Swing庫(kù)為UI創(chuàng)建按鈕和文本框,并使用Java中的數(shù)學(xué)函數(shù)來(lái)執(zhí)行運(yùn)算。

   2. 單詞計(jì)數(shù)器

這是另一個(gè)簡(jiǎn)單的Java項(xiàng)目,可以幫助您加深對(duì)Java編程語(yǔ)言的理解。您可以創(chuàng)建一個(gè)程序,該程序讀取輸入文件中的文本,并計(jì)算每個(gè)單詞出現(xiàn)的次數(shù)。您可以使用Java中的FileInputStream和BufferedReader類(lèi)來(lái)讀取文件內(nèi)容,使用Map類(lèi)來(lái)存儲(chǔ)單詞和其出現(xiàn)的次數(shù)。

   3. 簡(jiǎn)單的圖書(shū)管理系統(tǒng)

這是一個(gè)介紹Java編程語(yǔ)言面向?qū)ο缶幊?OOP)概念的項(xiàng)目。您可以創(chuàng)建一個(gè)簡(jiǎn)單的圖書(shū)管理系統(tǒng),該系統(tǒng)可以添加、刪除、查看和更新圖書(shū)信息。您可以使用Java中的類(lèi)和對(duì)象來(lái)表示圖書(shū)和圖書(shū)館,使用數(shù)組或集合來(lái)存儲(chǔ)多個(gè)圖書(shū)。

   4. 簡(jiǎn)單的網(wǎng)絡(luò)爬蟲(chóng)

這是一個(gè)有趣的Java項(xiàng)目,可以讓您了解Java編程語(yǔ)言在網(wǎng)絡(luò)編程方面的應(yīng)用。您可以創(chuàng)建一個(gè)簡(jiǎn)單的網(wǎng)絡(luò)爬蟲(chóng),該爬蟲(chóng)可以從網(wǎng)頁(yè)中提取信息。您可以使用Java中的URLConnection和BufferedReader類(lèi)來(lái)訪問(wèn)網(wǎng)頁(yè)內(nèi)容,并使用正則表達(dá)式或其他技術(shù)來(lái)提取所需信息。

總之,這些Java入門(mén)小項(xiàng)目都非常適合初學(xué)者,它們涵蓋了Java編程語(yǔ)言的不同方面。通過(guò)完成這些項(xiàng)目,您可以加深對(duì)Java編程語(yǔ)言的理解,并建立起自己的編程知識(shí)庫(kù)。

參考示例:

  1. 計(jì)算器應(yīng)用程序示例代碼:
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. 單詞計(jì)數(shù)器示例代碼:

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. 簡(jiǎn)單的圖書(shū)管理系統(tǒng):

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(); // 添加一些示例圖書(shū) 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請(qǐng)選擇一個(gè)操作:"); System.out.println("1. 顯示所有圖書(shū)"); System.out.println("2. 添加一本新圖書(shū)"); System.out.println("3. 刪除一本圖書(shū)"); System.out.println("4. 退出"); int choice = scanner.nextInt(); switch (choice) { case 1: library.displayBooks(); break; case 2: System.out.print("請(qǐng)輸入書(shū)名:"); String title = scanner.next(); System.out.print("請(qǐng)輸入作者名:"); String author = scanner.next(); System.out.print("請(qǐng)輸入出版年份:"); int year = scanner.nextInt(); library.addBook(new Book(title, author, year)); System.out.println("添加成功!"); break; case 3: System.out.print("請(qǐng)輸入要?jiǎng)h除的書(shū)名:"); 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("未找到該書(shū),刪除失敗!"); } break; case 4: System.out.println("程序已退出。"); return; default: System.out.println("輸入無(wú)效,請(qǐng)重新選擇。"); } } } } 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 "書(shū)名:" + title + "\t作者:" + author + "\t出版年份:" + year; } }

   4. 簡(jiǎn)單的網(wǎng)絡(luò)爬蟲(chóng)示例代碼:

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; // 當(dāng)前深度 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(); // 獲取頁(yè)面文檔對(duì)象 Elements links = document.select("a[href]"); // 獲取所有鏈接元素 for (Element link : links) { String nextUrl = link.absUrl("href"); System.out.println(nextUrl); // 輸出當(dāng)前頁(yè)的鏈接 // 遞歸爬取下一層頁(yè)面 depth++; crawl(nextUrl); depth--; } } catch (IOException e) { e.printStackTrace(); } } }


0 人點(diǎn)贊