晴颜
2016-10-28 22:10
import java.util.Scanner;
import java.util.Arrays;
public class Tushuguan {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input=new Scanner(System.in);
System.out.println("欢迎使用图书管理系统!");
String []names={"语文","数学","英语","物理","化学"};//图书名称数组
int f=1;
do {
System.out.println("输入命令:");
System.out.println("按照名称查询图书请输入1;");
System.out.println("按照序号查询图书请输入2");
int num=input.nextInt();//输入命令序号
try{
if(num!=1&&num!=2){
throw new Exception();
}
}catch(Exception e){
System.out.println("请输入正确的命令!");
}
if(num==1){
System.out.println("请输入图书名称:");
String name=input.next();
try{
for(int i=0;i<names.length;i++){
if(name.equals(names[i])){
System.out.println(names[i]);
}else{
throw new Exception();
}}
}catch(Exception e){
System.out.println("您查找的图书不存在!");
continue;
}
}
try{
if(num==2){
System.out.println("请输入图书序号:");
Arrays.sort(names);
int nameNum=input.nextInt();
if(nameNum<5){
System.out.println(names[nameNum]);
}else{
throw new Exception();
}
}
}catch(Exception e){
System.out.println("您查找的图书不存在!");
continue;
}
}while(f==1);
input.close();
}
}
你把那个else 改成这样else if(i==names.length)
package com.imooc;
import java.util.Scanner;
import java.util.Arrays;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input=new Scanner(System.in);
System.out.println("欢迎使用图书管理系统!");
String []names={"语文","数学","英语","物理","化学"};//图书名称数组
int f=1;
do {
System.out.println("输入命令:");
System.out.println("按照名称查询图书请输入1;");
System.out.println("按照序号查询图书请输入2");
int num=input.nextInt();//输入命令序号
try{
if(num!=1&&num!=2){
throw new Exception();
}
}catch(Exception e){
System.out.println("请输入正确的命令!");
}
if(num==1){
System.out.println("请输入图书名称:");
String name=input.next();
try{
for(int i=0;i<names.length;i++){
if(name.equals(names[i])){
System.out.println(names[i]);
}else if(i==names.length){
throw new Exception();
}}
}catch(Exception e){
System.out.println("您查找的图书不存在!");
continue;
}
}
try{
if(num==2){
System.out.println("请输入图书序号:");
Arrays.sort(names);
int nameNum=input.nextInt();
if(nameNum<5){
System.out.println(names[nameNum]);
}else {
throw new Exception();
}
}
}catch(Exception e){
System.out.println("您查找的图书不存在!");
continue;
}
}while(f==1);
input.close();
}
}
import java.util.Scanner;
import java.util.Arrays;
public class Tushuguan {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input=new Scanner(System.in);
System.out.println("欢迎使用图书管理系统!");
String []names={"语文","数学","英语","物理","化学"};//图书名称数组
int f=1;
do {
System.out.println("输入命令:");
System.out.println("按照名称查询图书请输入1;");
System.out.println("按照序号查询图书请输入2");
int num=input.nextInt();//输入命令序号
try{
if(num!=1&&num!=2){
throw new Exception();
}
}catch(Exception e){
System.out.println("请输入正确的命令!");
}
if(num==1){
System.out.println("请输入图书名称:");
String name=input.next();
try{
for(int i=0;i<names.length;i++){
if(name.equals(names[i])){
System.out.println(names[i]);
}else{
throw new Exception();
}}
}catch(Exception e){
System.out.println("您查找的图书不存在!");
continue;
}
}
try{
if(num==2){
System.out.println("请输入图书序号:");
Arrays.sort(names);
int nameNum=input.nextInt();
if(nameNum<5){
System.out.println(names[nameNum]);
}else{
throw new Exception();
}
}
}catch(Exception e){
System.out.println("您查找的图书不存在!");
continue;
}
}while(f==1);
input.close();
}
}
是不是因为在for循环里面,判断只有一次是正确,其他的n-1次都不对。有多次执行else语句,抛出异常。打印那句话啊。
应该是异常抛出的使用方法有问题
//创建BadCommand异常类
package com.imooc;
public class BadCommand extends Exception{
public BadCommand(){
}
public BadCommand(String message){
super(message);
}
}
//创建NoExist异常类
package com.imooc;
public class NoExist extends Exception {
public NoExist(){
}
public NoExist(String message){
super(message);
}
}
//主程序
package com.imooc;
import java.util.*;
public class Borrowingsystem {
String[] books={new String("高数"),new String("语文"),new String("英语"),new String("政治"),new String("历史")};
Scanner input=new Scanner(System.in);
public static void main(String[] args) {
// TODO Auto-generated method stub
Borrowingsystem borrow=new Borrowingsystem();
while(true){
try{
String choose=borrow.choice1();
if(choose.equals("1")){
try{//通过书名查书
borrow.SearchBookName();
break;
}catch(NoExist e){
System.out.println(e.getMessage());
}
}else if(choose.equals("2")){
try {//通过序号查书
borrow.SearchBookId();
break;
} catch (NoExist e) {
// TODO: handle exception
System.out.println(e.getMessage());
}
}
}catch(BadCommand e){
System.out.println(e.getMessage());
}
}
}
public String choice1() throws BadCommand{
System.out.println("输入命令:1-按照名称查找图书;2-按照序号查找图书");
String ip1=input.next();
if(!ip1.equals("1")&&!ip1.equals("2")){
throw new BadCommand("命令输入错误!请根据提示输入数字命令!");
}
return ip1;
}
public void SearchBookName() throws NoExist{
System.out.println("输入图书名称:");
String bookname=input.next();
int findbook=0;
for(int i=1;i<=books.length;i++){
if(bookname.equals(books[i-1])){
System.out.println("book:"+bookname);
findbook=1;
}
}
if(findbook==0){
throw new NoExist("图书不存在!");
}
}
public void SearchBookId() throws NoExist{
System.out.println("输入图书序号:");
int bookid=input.nextInt();
int findbook=0;
if(bookid>=1&&bookid<=books.length){
System.out.println("book:"+books[bookid-1]);
findbook=1;
}
if(findbook==0){
throw new NoExist("图书不存在");
}
}Java入门第三季
409776 学习 · 4546 问题
相似问题