在查看章节案例1——搭建javahomeWork项目视频后,在自己的开发环境上完成作业项目的搭建。
实现以下功能。
交互界面可以多次重复选择作业章节号,进入对应的作业章节。如下图所示:
实现效果如下:(目录效果可以按照个人喜好进行更改,这里仅提供笔者个人设计方案)
package edu.java.homework.chapter;
import java.util.Scanner;
public class Menu {
public static final String ANSI_RESET = "\u001B[0m";
public static final String ANSI_RED = "\u001B[31m";
//设置字体颜色
public void showMenu(){
while(true){
clearScreen();
//设置清屏函数
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//设定缓冲时间,可删去
System.out.println("欢迎登陆Java程序查询系统,请选择您要查询的程序:");
System.out.println("程序Ⅰ--------1");
System.out.println("程序Ⅱ--------2");
System.out.println("程序Ⅲ--------3");
System.out.println("程序Ⅳ--------4");
System.out.println("输入程序代号(0:退出):");
Scanner reader=new Scanner(System.in);
int option=reader.nextInt();
if(option==1){
System.out.println("Loading……");
Chapter1 chapter1=new Chapter1();
chapter1.chapterMenu();
}else if(option==2){
System.out.println("Loading……");
Chapter2 chapter2=new Chapter2();
chapter2.chapterMenu();
}else if(option==3){
System.out.println("Loading……");
System.out.println("Display the title of the ChapterThree");
}else if(option==4){
System.out.println("Loading……");
System.out.println("Display the title of the ChapterFour");
}else if(option==0){
System.out.println("感谢您的使用,期待您的下次访问!");
break;
}else{
System.out.println(ANSI_RED + "Warning! \nError type: Input error\nPlease enter the correct operation code!" + ANSI_RESET);
}
//更改字体颜色的显示方法
}
}
public static void clearScreen() {
System.out.print("\033[H\033[2J");
System.out.flush();
}
}
设置好主目录后,设置主类和主方法:
package edu.java.homework.main;
import edu.java.homework.chapter.Menu;
public class Main {
public static void main(String[] args){
Menu menu=new Menu();
menu.showMenu();
}
}
后续的作业系统将基于此进行添加。