21xrx.com
2024-12-23 00:19:06 Monday
登录
文章检索 我的文章 写文章
《Java课程设计题目及代码实现》
2023-06-11 23:01:37 深夜i     --     --
Java课程设计 字符统计 图书管理 计算器

Java作为一门广受欢迎的编程语言,其在各个领域都有着广泛的应用。为此,许多学校和培训机构都开设了Java课程,旨在培养从基础到高级的Java开发人才。在学习Java的过程中,课程设计是不可或缺的一环,它能够深入学生的思考和实践,帮助他们更好地掌握开发技能。

本文将介绍一些Java课程设计题目,并提供简单的代码实现,以供读者参考学习。

1. 统计字符出现次数

题目描述:输入一串字符串,统计每个字符出现的次数,并输出结果。

代码实现:


import java.util.*;

public class CharacterCount {

  public static void main(String[] args) {

    Scanner scanner = new Scanner(System.in);

    System.out.print("请输入一个字符串:");

    String str = scanner.nextLine();

    char[] chars = str.toCharArray();

    HashMap map = new HashMap<>();

    for (char c : chars) {

      if (map.containsKey(c)) {

        map.put(c, map.get(c) + 1);

      } else {

        map.put(c, 1);

      }

    }

    for (Map.Entry entry : map.entrySet()) {

      System.out.println(entry.getKey() + "出现了" + entry.getValue() + "次");

    }

  }

}

2. 图书管理系统

题目描述:实现一个简单的图书管理系统,能够对图书的添加、删除、查询和借还进行管理。

代码实现:


import java.util.*;

public class BookManagement {

  private static ArrayList books = new ArrayList<>();

  private static Scanner scanner = new Scanner(System.in);

  public static void main(String[] args) {

    while (true) {

      System.out.println("欢迎使用图书管理系统!");

      System.out.println("1. 添加图书");

      System.out.println("2. 删除图书");

      System.out.println("3. 查询图书");

      System.out.println("4. 借出图书");

      System.out.println("5. 归还图书");

      System.out.println("6. 退出系统");

      System.out.print("请选择您需要的操作:");

      int choice = scanner.nextInt();

      switch (choice) {

        case 1:

          add();

          break;

        case 2:

          delete();

          break;

        case 3:

          search();

          break;

        case 4:

          borrow();

          break;

        case 5:

          returnBook();

          break;

        case 6:

          System.out.println("感谢使用图书管理系统!");

          System.exit(0);

        default:

          System.out.println("输入有误,请重新输入!");

      }

    }

  }

  private static void add() {

    scanner.nextLine();

    System.out.print("请输入图书名称:");

    String name = scanner.nextLine();

    System.out.print("请输入图书作者:");

    String author = scanner.nextLine();

    System.out.print("请输入图书分类:");

    String category = scanner.nextLine();

    books.add(new Book(name, author, category));

    System.out.println("图书添加成功!");

  }

  private static void delete() {

    scanner.nextLine();

    System.out.print("请输入要删除的图书名称:");

    String name = scanner.nextLine();

    for (int i = 0; i < books.size(); i++) {

      Book book = books.get(i);

      if (book.getName().equals(name)) {

        books.remove(i);

        System.out.println("图书删除成功!");

        return;

      }

    }

    System.out.println("未找到该图书!");

  }

  private static void search() {

    scanner.nextLine();

    System.out.print("请输入要查询的图书名称:");

    String name = scanner.nextLine();

    for (Book book : books) {

      if (book.getName().equals(name)) {

        System.out.println(book);

        return;

      }

    }

    System.out.println("未找到该图书!");

  }

  private static void borrow() {

    scanner.nextLine();

    System.out.print("请输入要借出的图书名称:");

    String name = scanner.nextLine();

    for (Book book : books) {

      if (book.getName().equals(name)) {

        if (book.isBorrowed()) {

          System.out.println("该图书已被借出!");

        } else {

          book.setBorrowed(true);

          System.out.println("借书成功!");

        }

        return;

      }

    }

    System.out.println("未找到该图书!");

  }

  private static void returnBook() {

    scanner.nextLine();

    System.out.print("请输入要归还的图书名称:");

    String name = scanner.nextLine();

    for (Book book : books) {

      if (book.getName().equals(name)) {

        if (book.isBorrowed()) {

          book.setBorrowed(false);

          System.out.println("还书成功!");

        } else {

          System.out.println("该图书未被借出,无法归还!");

        }

        return;

      }

    }

    System.out.println("未找到该图书!");

  }

}

class Book {

  private String name;

  private String author;

  private String category;

  private boolean borrowed;

  public Book(String name, String author, String category)

    this.name = name;

    this.author = author;

    this.category = category;

  

  public String getName()

    return name;

  

  public String getAuthor()

    return author;

  

  public String getCategory()

    return category;

  

  public boolean isBorrowed()

    return borrowed;

  

  public void setBorrowed(boolean borrowed)

    this.borrowed = borrowed;

  

  public String toString() {

    return "图书名称:" + name + ",作者:" + author + ",分类:" + category + ",是否借出:" + (borrowed ? "是" : "否");

  }

}

3. 简单计算器

题目描述:实现一个简单的计算器,能够进行加减乘除四则运算。

代码实现:


import java.util.Scanner;

public class Calculator {

  public static void main(String[] args) {

    Scanner scanner = new Scanner(System.in);

    System.out.print("请输入第一个数字:");

    double num1 = scanner.nextDouble();

    System.out.print("请输入运算符(+、-、*、/):");

    String operator = scanner.next();

    System.out.print("请输入第二个数字:");

    double num2 = scanner.nextDouble();

    double result;

    switch (operator) {

      case "+":

        result = num1 + num2;

        break;

      case "-":

        result = num1 - num2;

        break;

      case "*":

        result = num1 * num2;

        break;

      case "/":

        result = num1 / num2;

        break;

      default:

        System.out.println("输入有误,程序结束!");

        return;

    }

    System.out.println(num1 + " " + operator + " " + num2 + " = " + result);

  }

}

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复