App下載

Java入門簡單小項目:w3cschool助你學(xué)Java!

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

如果您想學(xué)習Java編程語言,最好的方法是通過實踐來掌握它。在這篇文章中,我將介紹一些簡單的Java入門小項目,可以幫助您開始學(xué)習 Java。(參考示例附在最后)

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

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

   2. 單詞計數(shù)器

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

   3. 簡單的圖書管理系統(tǒng)

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

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

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

總之,這些Java入門小項目都非常適合初學(xué)者,它們涵蓋了Java編程語言的不同方面。通過完成這些項目,您可以加深對Java編程語言的理解,并建立起自己的編程知識庫。

參考示例:

  1. 計算器應(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. 單詞計數(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. 簡單的圖書管理系統(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(); // 添加一些示例圖書 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. 簡單的網(wǎng)絡(luò)爬蟲示例代碼:

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(); } } }


0 人點贊