欢迎哥哥们指正
代码在二楼
日常偷懒
package throwable;
import java.util.Scanner;
public class PracticeBooks {
//实例化本类
static PracticeBooks pbs = new PracticeBooks();
//main函数
public static void main(String[] args)
{
pbs.books();
pbs.choose1();
}
//书架
public String[] bookss =
{
"高等数学","离散数学","自动机形式导论","计算机网络",
};
//展示书本
public void books()
{
for(int i = 0, j = 1; i < bookss.length; i++)
{
System.out.println(j+"."+bookss[i]);
j++;
}
}
//选择选书方式
public void choose1()
{
Scanner cs = new Scanner(System.in);
System.out.println("1-使用序号选书;2-使用书名选书。");
System.out.println("输入编号选择查书方式:");
try
{
int ti = cs.nextInt();
switch (ti)
{
case 1:
pbs.choose2();
cs.close();
break;
case 2:
pbs.choose3();
cs.close();
break;
}
}
catch(Exception e)
{
System.out.println("请输入数字编号选择查书方式。");
pbs.choose1();
}
}
//以序号方式选择书本
public void choose2()
{
Scanner cs = new Scanner(System.in);
System.out.println("请输入序号选:");
pbs.books();
try
{
int ic = cs.nextInt();
System.out.println("你选择的书是:"+"《"+bookss[ic-1]+"》");
cs.close();
System.exit(0);
}
catch(Exception e)
{
System.out.println("不存在的序号,请输入正确的序号。");
pbs.choose2();
}
}
//以书名方式选择书本
public void choose3()
{
Scanner cs = new Scanner(System.in);
System.out.println("请输入书名:");
pbs.books();
try
{
String sc = cs.nextLine();
boolean x = false;
for(int i = 0; i < bookss.length; i++)
{
if(sc.equals(bookss[i]))
{
x = true;
cs.close();
break;
}
}
if(x)
{
System.out.println("你选择的书是: 《"+sc+"》");
System.exit(0);
}
else
{
pbs.choose3_1();
}
}
catch(Exception e)
{
System.out.println("你输入的书名不存在,请重新输入。");
pbs.choose3();
}
}
//如果书架中没有输入的书
public void choose3_1() throws Exception
{
throw new Exception();
}
}
举报