21xrx.com
2024-11-05 18:28:09 Tuesday
登录
文章检索 我的文章 写文章
Java初学者练手项目推荐与代码案例
2023-06-18 01:33:58 深夜i     --     --
Java初学者 练手项目 代码案例

作为一个Java初学者,不光要掌握基本语法,还需要多做练习项目提高自己的编程技能。在这篇文章中,我将为大家推荐几个适合Java初学者的练手项目,并提供一些简单的代码案例,帮助大家更好地理解。

1. 简单的计算器

这个项目比较简单,但是可以练习Java中的变量声明、控制语句和基本的算法知识。代码示例如下:


import java.util.Scanner;

public class Calculator {

  public static void main(String[] args) {

    Scanner scanner = new Scanner(System.in);

    System.out.println("请输入两个数字:");

    double num1 = scanner.nextDouble();

    double num2 = scanner.nextDouble();

    System.out.println("请选择要进行的操作:");

    System.out.println("1. 加法");

    System.out.println("2. 减法");

    System.out.println("3. 乘法");

    System.out.println("4. 除法");

    int choice = scanner.nextInt();

    double result = 0;

    switch (choice) {

      case 1:

        result = num1 + num2;

        break;

      case 2:

        result = num1 - num2;

        break;

      case 3:

        result = num1 * num2;

        break;

      case 4:

        result = num1 / num2;

        break;

      default:

        System.out.println("输入错误!");

    }

    System.out.println("结果为:" + result);

  }

}

2. 学生信息管理系统

这个项目可以让你练习Java中的类和对象、数组、输入输出等基础知识。代码示例如下:


import java.util.Scanner;

public class StudentManagementSystem {

  public static void main(String[] args) {

    int maxStudents = 100;

    Student[] students = new Student[maxStudents];

    Scanner scanner = new Scanner(System.in);

    int choice = 0;

    while (choice != 5) {

      System.out.println("请选择要进行的操作:");

      System.out.println("1. 添加学生");

      System.out.println("2. 删除学生");

      System.out.println("3. 查询学生");

      System.out.println("4. 修改学生");

      System.out.println("5. 退出程序");

      choice = scanner.nextInt();

      scanner.nextLine();

      switch (choice) {

        case 1:

          System.out.println("请输入学生姓名:");

          String name = scanner.nextLine();

          System.out.println("请输入学生年龄:");

          int age = scanner.nextInt();

          System.out.println("请输入学生性别:");

          String gender = scanner.next();

          Student student = new Student(name, age, gender);

          students[Student.getCount()] = student;

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

          break;

        case 2:

          System.out.println("请输入要删除学生的姓名:");

          String deleteName = scanner.nextLine();

          boolean isDeleted = false;

          for (int i = 0; i < Student.getCount(); i++) {

            if (students[i].getName().equals(deleteName)) {

              students[i] = null;

              isDeleted = true;

            }

            if (isDeleted && i < Student.getCount() - 1) {

              students[i] = students[i + 1];

            }

          }

          if (isDeleted) {

            Student.decreaseCount();

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

          } else {

            System.out.println("该学生不存在!");

          }

          break;

        case 3:

          System.out.println("请输入要查询学生的姓名:");

          String searchName = scanner.nextLine();

          boolean isExist = false;

          for (int i = 0; i < Student.getCount(); i++) {

            if (students[i].getName().equals(searchName)) {

              System.out.println(students[i]);

              isExist = true;

            }

          }

          if (!isExist) {

            System.out.println("该学生不存在!");

          }

          break;

        case 4:

          System.out.println("请输入要修改学生的姓名:");

          String modifyName = scanner.nextLine();

          boolean isModified = false;

          for (int i = 0; i < Student.getCount(); i++) {

            if (students[i].getName().equals(modifyName)) {

              System.out.println("请输入修改后的学生姓名:");

              String newName = scanner.nextLine();

              students[i].setName(newName);

              System.out.println("请输入修改后的学生年龄:");

              int newAge = scanner.nextInt();

              students[i].setAge(newAge);

              System.out.println("请输入修改后的学生性别:");

              String newGender = scanner.next();

              students[i].setGender(newGender);

              isModified = true;

            }

          }

          if (isModified) {

            System.out.println("修改成功!");

          } else {

            System.out.println("该学生不存在!");

          }

          break;

        case 5:

          break;

        default:

          System.out.println("输入错误!");

      }

    }

  }

}

class Student {

  private static int count = 0;

  private String name;

  private int age;

  private String gender;

  public Student(String name, int age, String gender) {

    this.name = name;

    this.age = age;

    this.gender = gender;

    count++;

  }

  public String getName()

    return name;

  

  public void setName(String name)

    this.name = name;

  

  public int getAge()

    return age;

  

  public void setAge(int age)

    this.age = age;

  

  public String getGender()

    return gender;

  

  public void setGender(String gender)

    this.gender = gender;

  

  public static int getCount()

    return count;

  

  public static void decreaseCount()

    count--;

  

  @Override

  public String toString() {

    return "姓名:" + name + ",年龄:" + age + ",性别:" + gender;

  }

}

3. 入门级图书管理系统

这个项目可以让你练习Java中的面向对象编程、文件IO、异常处理等进阶知识。代码示例如下:


import java.io.*;

import java.util.ArrayList;

import java.util.Scanner;

public class BookManagementSystem {

  public static void main(String[] args) {

    Scanner scanner = new Scanner(System.in);

    ArrayList books = new ArrayList<>();

    readFromFile(books);

    int choice = 0;

    while (choice != 4) {

      System.out.println("请选择要进行的操作:");

      System.out.println("1. 查看所有图书");

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

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

      System.out.println("4. 退出程序");

      choice = scanner.nextInt();

      scanner.nextLine();

      switch (choice) {

        case 1:

          displayBooks(books);

          break;

        case 2:

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

          String name = scanner.nextLine();

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

          String author = scanner.nextLine();

          System.out.println("请输入图书价格:");

          float price = scanner.nextFloat();

          Book book = new Book(name, author, price);

          books.add(book);

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

          break;

        case 3:

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

          String searchName = scanner.nextLine();

          boolean isExist = false;

          for (Book b : books) {

            if (b.getName().equals(searchName)) {

              System.out.println(b);

              isExist = true;

            }

          }

          if (!isExist) {

            System.out.println("该图书不存在!");

          }

          break;

        case 4:

          writeToFile(books);

          break;

        default:

          System.out.println("输入错误!");

      }

    }

    scanner.close();

  }

  public static void displayBooks(ArrayList books) {

    for (Book b : books) {

      System.out.println(b);

    }

  }

  public static void readFromFile(ArrayList books) {

    File file = new File("books.txt");

    try {

      if (!file.exists()) {

        file.createNewFile();

      }

      BufferedReader reader = new BufferedReader(new FileReader(file));

      String line = null;

      while ((line = reader.readLine()) != null) {

        String[] data = line.split("\\|");

        String name = data[0];

        String author = data[1];

        float price = Float.parseFloat(data[2]);

        Book book = new Book(name, author, price);

        books.add(book);

      }

      reader.close();

    } catch (IOException e) {

      e.printStackTrace();

    }

  }

  public static void writeToFile(ArrayList books) {

    File file = new File("books.txt");

    try {

      if (!file.exists()) {

        file.createNewFile();

      }

      BufferedWriter writer = new BufferedWriter(new FileWriter(file));

      for (Book b : books) {

        writer.write(b.getName() + "|" + b.getAuthor() + "|" + b.getPrice());

        writer.newLine();

      }

      writer.close();

    } catch (IOException e) {

      e.printStackTrace();

    }

    System.out.println("程序已退出!");

  }

}

class Book {

  private String name;

  private String author;

  private float price;

  public Book(String name, String author, float price) {

    this.name = name;

    this.author = author;

    this.price = price;

  }

  public String getName() {

    return name;

  }

  public String getAuthor() {

    return author;

  }

  public float getPrice() {

    return price;

  }

  @Override

  public String toString() {

    return "书名:" + name + ",作者:" + author + ",价格:" + price;

  }

}

以上就是三个适合Java初学者的练手项目,希望对大家有所帮助!

  
  

评论区

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