你们从控制台获取的汉字 跟 书名的数组中的汉字比较相同 是怎么比较的,用汉字书名 总是出错
System.out.println("请输入图书名称:"); String name = new Scanner(System.in).nextLine().toString(); String[] books = {"xiyouji" , "hongloumeng" , "zhuxian" , "guichuideng"}; boolean isFind = false; for (String bookName : books) { if (bookName.equals(name)) { System.out.println("book:" + name); isFind = true; break; } } if (!isFind) { throw new Exception(); }
public void kindChoice (int num) throws NameNoMatchException{ switch (num) { case 1: System.out.println("请输入图书名称:"); String name = (String)new Scanner(System.in).nextLine().toString(); try { nameChoice(name); } catch (NameNoMatchException e) { System.out.println("输入的图书名字不存在"); //回滚操作 orderChoice(); } break; case 2: System.out.println("请输入图书序号:"); int bookNum = new Scanner(System.in).nextInt(); try { numChoice(bookNum); } catch (NameNoMatchException e) { System.out.println("输入的图书序号不存在"); orderChoice(); } break; default: throw new NameNoMatchException("命令输入错误,请根据提示重新输入"); } }
重新截了一下kindChoice方法 这样好看点
public void nameChoice(String name) throws NameNoMatchException { String[] books = {"xiyouji" , "hongloumeng" , "zhuxian" , "guichuideng"}; boolean isFind = false; for (String bookName : books) { if (bookName.equals(name)) { System.out.println("book:" + name); isFind = true; break; } } if (!isFind) { throw new NameNoMatchException(); } }
public void kindChoice (int num) throws NameNoMatchException{
switch (num) {
case 1:
System.out.println("请输入图书名称:");
String name = (String)new Scanner(System.in).nextLine().toString();
try {
nameChoice(name);
} catch (NameNoMatchException e) {
System.out.println("输入的图书名字不存在");
//回滚操作
orderChoice();
}
break;
case 2:
System.out.println("请输入图书序号:");
int bookNum = new Scanner(System.in).nextInt();
try {
numChoice(bookNum);
} catch (NameNoMatchException e) {
System.out.println("输入的图书序号不存在");
orderChoice();
}
break;
default:
throw new NameNoMatchException("命令输入错误,请根据提示重新输入");
}
}
在kindChoice方法中获取的控制台输入 汉字比较总是出错
代码贴出来吧~
这样试了还是不行 书名是从控制台通过scanner获取的
for(int i=0;i<arr.length;i++){
str.equals(arr[i]);
}