不同于老師說的結構,但實現同樣效果,不知道兩種方法有什么區別
每本書創建成單獨類調用
package com.xiaohongshu;
import java.util.Scanner;
public class Initail {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("書庫中的圖書目錄:");
System.out.println("1.《紅樓夢》");
System.out.println("2.《三國演義》");
System.out.println("3.《水滸傳》");
? ? ? ? System.out.println("4.《西游記》");
? ? ? ??
? ? ? ? while(true) {
? ? ? ? System.out.println("請輸入圖書的編號或名字:");
? ? ? ? Scanner input=new Scanner(System.in);
? ? ? ? String score=input.next();
? ? ? ? switch (score) {
case "1" :
case "紅樓夢":
book hello=new HongLouMeng();
? ? ? ? hello.name();
? ? ? ? hello.category();
? ? ? ? hello.author();
? ? ? ? break;
case "2":
case "三國演義":
book hello2=new SanGuoYanYi();
? ? ? ? hello2.name();
? ? ? ? hello2.category();
? ? ? ? hello2.author();
? ? ? ? break;
case "3":
case "水滸傳":
book hello3=new ShuiHuZhuan();
? ? ? ? hello3.name();
? ? ? ? hello3.category();
? ? ? ? hello3.author();
? ? ? ? break;
case "4":
case "西游記":
book hello4=new SanGuoYanYi();
? ? ? ? hello4.name();
? ? ? ? hello4.category();
? ? ? ? hello4.author();
? ? ? ? break;
default:
System.out.println("輸入錯誤,請重新輸入!");
continue;
}
? ? ? ?
//? ? ? ??
? ? ? ??
? ? ? ? }
}
}
2020-10-13
當書本數量很多的時候,一個個創建類就麻煩了