我用java初级编的简单的图书馆管理系统,只有一个读者,10本书,一个人最多借三本,编译通过了,但有异常
哪位大哥帮我看一下程序,实在没找出解决异常的办法,多谢了.相关程序如下:importjava.util.Scanner;publicclassTestBook{publi...
哪位大哥帮我看一下程序,实在没找出解决异常的办法,多谢了.
相关程序如下:
import java.util.Scanner;
public class TestBook
{
public static void main(String[] args)
{
Books all[]=new Books[10];
Books self[]=new Books[3];
all[0]=new Books("java",1,"12345","tom",34.0f,"人民出版社");
all[1]=new Books("c",2,"12346","tnn",31.0f,"人民出版社");
all[2]=new Books("c++",3,"12445","mm",35.0f,"人民出版社");
all[3]=new Books("c#",4,"12365","tt",38.0f,"人民出版社");
all[4]=new Books("j2se",5,"13345","tosm",31.1f,"人民出版社");
all[5]=new Books("j2ee",6,"18345","ttm",32.0f,"人民出版社");
all[6]=new Books("jsp",7,"12335","cc",33.0f,"人民出版社");
all[7]=new Books("net",8,"12341","bb",36.0f,"人民出版社");
all[8]=new Books("ip",9,"12343","aa",37.0f,"人民出版社");
all[9]=new Books("tcp",10,"22345","jj",39.0f,"人民出版社");
Readers r=new Readers("xiaoming",101,"1",3);
r.searchAllBooks(all);
r.borrowBooks(all,self);
r.have(self);
r.give(all,self);
}
}
class Readers{
Scanner scan=new Scanner(System.in);
String names;
int nums;
String classes;
int grade;
int score=0;
//Books self[]=new Books[3];
Readers(String n,int u,String c,int g){
names=n;
nums=u;
classes=c;
grade=g;
}
void searchAllBooks(Books[] all){//查书
for(int i=0;i<10;i++)
all[i].showBookInfo();
//self[score]=all[0];
}
void give(Books[] all,Books[] self){//还书
System.out.println("请输入您要还的书的书号");
int n=scan.nextInt();
for(int i=0;i<10;i++){
if(n==all[i].num){
for(int j=0;j<3;j++){
if(self[j]==all[i]){
self[j]=null;
System.out.println("还书成功");
}
}
}
}
}
void have(Books[] self){
for(int i=0;i<2;i++)
{
self[i].showBookInfo();
}
}
void giveMoney(){
}
void borrowBooks(Books[] all,Books[] self){
System.out.println("请输入您要查找的书名:");
String n=scan.next();
int i;
for( i=0;i<10;i++){
if(n.equals(all[i].name)){
all[i].showBookInfo();
break;
}
}
System.out.println("你要借吗?");
int j;
j=scan.nextInt();
if(j==1){
System.out.println("借阅成功");
self[score]=all[9];
score+=1;
}
if(score<4){
System.out.println("您还可以借阅"+(3-score)+"本");
}
else{
System.out.println("对不起,一个人只能借3本");
}
}
}
class Books
{
String name;
int num;
String ISBN;
String writer;
float price;
String publisher;
Books(String n,int u,String i,String w,float p,String l){
name=n;
num=u;
ISBN=i;
writer=w;
price=p;
publisher=l;
}
void showBookInfo(){
System.out.println("**************************");
System.out.println("书名:"+name);
System.out.println("索书号:"+num);
System.out.println("ISBN号:"+ISBN);
System.out.println("价格:"+price);
System.out.println("出版社:"+publisher);
System.out.println("**************************");
}
} 展开
相关程序如下:
import java.util.Scanner;
public class TestBook
{
public static void main(String[] args)
{
Books all[]=new Books[10];
Books self[]=new Books[3];
all[0]=new Books("java",1,"12345","tom",34.0f,"人民出版社");
all[1]=new Books("c",2,"12346","tnn",31.0f,"人民出版社");
all[2]=new Books("c++",3,"12445","mm",35.0f,"人民出版社");
all[3]=new Books("c#",4,"12365","tt",38.0f,"人民出版社");
all[4]=new Books("j2se",5,"13345","tosm",31.1f,"人民出版社");
all[5]=new Books("j2ee",6,"18345","ttm",32.0f,"人民出版社");
all[6]=new Books("jsp",7,"12335","cc",33.0f,"人民出版社");
all[7]=new Books("net",8,"12341","bb",36.0f,"人民出版社");
all[8]=new Books("ip",9,"12343","aa",37.0f,"人民出版社");
all[9]=new Books("tcp",10,"22345","jj",39.0f,"人民出版社");
Readers r=new Readers("xiaoming",101,"1",3);
r.searchAllBooks(all);
r.borrowBooks(all,self);
r.have(self);
r.give(all,self);
}
}
class Readers{
Scanner scan=new Scanner(System.in);
String names;
int nums;
String classes;
int grade;
int score=0;
//Books self[]=new Books[3];
Readers(String n,int u,String c,int g){
names=n;
nums=u;
classes=c;
grade=g;
}
void searchAllBooks(Books[] all){//查书
for(int i=0;i<10;i++)
all[i].showBookInfo();
//self[score]=all[0];
}
void give(Books[] all,Books[] self){//还书
System.out.println("请输入您要还的书的书号");
int n=scan.nextInt();
for(int i=0;i<10;i++){
if(n==all[i].num){
for(int j=0;j<3;j++){
if(self[j]==all[i]){
self[j]=null;
System.out.println("还书成功");
}
}
}
}
}
void have(Books[] self){
for(int i=0;i<2;i++)
{
self[i].showBookInfo();
}
}
void giveMoney(){
}
void borrowBooks(Books[] all,Books[] self){
System.out.println("请输入您要查找的书名:");
String n=scan.next();
int i;
for( i=0;i<10;i++){
if(n.equals(all[i].name)){
all[i].showBookInfo();
break;
}
}
System.out.println("你要借吗?");
int j;
j=scan.nextInt();
if(j==1){
System.out.println("借阅成功");
self[score]=all[9];
score+=1;
}
if(score<4){
System.out.println("您还可以借阅"+(3-score)+"本");
}
else{
System.out.println("对不起,一个人只能借3本");
}
}
}
class Books
{
String name;
int num;
String ISBN;
String writer;
float price;
String publisher;
Books(String n,int u,String i,String w,float p,String l){
name=n;
num=u;
ISBN=i;
writer=w;
price=p;
publisher=l;
}
void showBookInfo(){
System.out.println("**************************");
System.out.println("书名:"+name);
System.out.println("索书号:"+num);
System.out.println("ISBN号:"+ISBN);
System.out.println("价格:"+price);
System.out.println("出版社:"+publisher);
System.out.println("**************************");
}
} 展开
3个回答
展开全部
---------------------------------------------------
给你修改了三个地方:
1.borrowBooks方法中,将System.out.println("你要借吗?"); 改为:
System.out.println("你要借吗?输入1表示借,其他数字表示不借。");
保证输入的时候输入的数字,否则会报出异常。
2.borrowBooks方法中,将self[score] = all[9]; 改为:self[score] = all[i];
如果是all[9],那么就始终是最后一本书籍信息了。
3.have方法中,你是想将所借的书籍信息都打印出来。修改的比较多,下面注释代码是原来的。
void have(Books[] self) {
// for (int i = 0; i < 2; i++) {
// self[i].showBookInfo();
// }
for (int i = 0; i < 3; i++) {
if(self[i]!=null)
self[i].showBookInfo();
}
}
****************** 附上所有代码:*************************
import java.util.Scanner;
public class TestBook {
public static void main(String[] args) {
Books all[] = new Books[10];
Books self[] = new Books[3];
all[0] = new Books("java", 1, "12345", "tom", 34.0f, "人民出版社");
all[1] = new Books("c", 2, "12346", "tnn", 31.0f, "人民出版社");
all[2] = new Books("c++", 3, "12445", "mm", 35.0f, "人民出版社");
all[3] = new Books("c#", 4, "12365", "tt", 38.0f, "人民出版社");
all[4] = new Books("j2se", 5, "13345", "tosm", 31.1f, "人民出版社");
all[5] = new Books("j2ee", 6, "18345", "ttm", 32.0f, "人民出版社");
all[6] = new Books("jsp", 7, "12335", "cc", 33.0f, "人民出版社");
all[7] = new Books("net", 8, "12341", "bb", 36.0f, "人民出版社");
all[8] = new Books("ip", 9, "12343", "aa", 37.0f, "人民出版社");
all[9] = new Books("tcp", 10, "22345", "jj", 39.0f, "人民出版社");
Readers r = new Readers("xiaoming", 101, "1", 3);
r.searchAllBooks(all);
r.borrowBooks(all, self);
r.have(self);
r.give(all, self);
}
}
class Readers {
Scanner scan = new Scanner(System.in);
String names;
int nums;
String classes;
int grade;
int score = 0;
// Books self[]=new Books[3];
Readers(String n, int u, String c, int g) {
names = n;
nums = u;
classes = c;
grade = g;
}
void searchAllBooks(Books[] all) {// 查书
for (int i = 0; i < 10; i++)
all[i].showBookInfo();
// self[score]=all[0];
}
void give(Books[] all, Books[] self) {// 还书
System.out.println("请输入您要还的书的书号");
int n = scan.nextInt();
for (int i = 0; i < 10; i++) {
if (n == all[i].num) {
for (int j = 0; j < 3; j++) {
if (self[j] == all[i]) {
self[j] = null;
System.out.println("还书成功");
}
}
}
}
}
void have(Books[] self) {
// for (int i = 0; i < 2; i++) {
// self[i].showBookInfo();
// }
for (int i = 0; i < 3; i++) {
if(self[i]!=null)
self[i].showBookInfo();
}
}
void giveMoney() {
}
void borrowBooks(Books[] all, Books[] self) {
System.out.println("请输入您要查找的书名:");
String n = scan.next();
int i;
for (i = 0; i < 10; i++) {
if (n.equals(all[i].name)) {
all[i].showBookInfo();
break;
}
}
//System.out.println("你要借吗?");
System.out.println("你要借吗?输入1表示借,其他数字表示不借。");
int j;
j = scan.nextInt();
if (j == 1) {
System.out.println("借阅成功");
//self[score] = all[9];
self[score] = all[i];
score += 1;
}
if (score < 4) {
System.out.println("您还可以借阅" + (3 - score) + "本");
} else {
System.out.println("对不起,一个人只能借3本");
}
}
}
class Books {
String name;
int num;
String ISBN;
String writer;
float price;
String publisher;
Books(String n, int u, String i, String w, float p, String l) {
name = n;
num = u;
ISBN = i;
writer = w;
price = p;
publisher = l;
}
void showBookInfo() {
System.out.println("**************************");
System.out.println("书名:" + name);
System.out.println("索书号:" + num);
System.out.println("ISBN号:" + ISBN);
System.out.println("价格:" + price);
System.out.println("出版社:" + publisher);
System.out.println("**************************");
}
}
----------------------------------------------------
给你修改了三个地方:
1.borrowBooks方法中,将System.out.println("你要借吗?"); 改为:
System.out.println("你要借吗?输入1表示借,其他数字表示不借。");
保证输入的时候输入的数字,否则会报出异常。
2.borrowBooks方法中,将self[score] = all[9]; 改为:self[score] = all[i];
如果是all[9],那么就始终是最后一本书籍信息了。
3.have方法中,你是想将所借的书籍信息都打印出来。修改的比较多,下面注释代码是原来的。
void have(Books[] self) {
// for (int i = 0; i < 2; i++) {
// self[i].showBookInfo();
// }
for (int i = 0; i < 3; i++) {
if(self[i]!=null)
self[i].showBookInfo();
}
}
****************** 附上所有代码:*************************
import java.util.Scanner;
public class TestBook {
public static void main(String[] args) {
Books all[] = new Books[10];
Books self[] = new Books[3];
all[0] = new Books("java", 1, "12345", "tom", 34.0f, "人民出版社");
all[1] = new Books("c", 2, "12346", "tnn", 31.0f, "人民出版社");
all[2] = new Books("c++", 3, "12445", "mm", 35.0f, "人民出版社");
all[3] = new Books("c#", 4, "12365", "tt", 38.0f, "人民出版社");
all[4] = new Books("j2se", 5, "13345", "tosm", 31.1f, "人民出版社");
all[5] = new Books("j2ee", 6, "18345", "ttm", 32.0f, "人民出版社");
all[6] = new Books("jsp", 7, "12335", "cc", 33.0f, "人民出版社");
all[7] = new Books("net", 8, "12341", "bb", 36.0f, "人民出版社");
all[8] = new Books("ip", 9, "12343", "aa", 37.0f, "人民出版社");
all[9] = new Books("tcp", 10, "22345", "jj", 39.0f, "人民出版社");
Readers r = new Readers("xiaoming", 101, "1", 3);
r.searchAllBooks(all);
r.borrowBooks(all, self);
r.have(self);
r.give(all, self);
}
}
class Readers {
Scanner scan = new Scanner(System.in);
String names;
int nums;
String classes;
int grade;
int score = 0;
// Books self[]=new Books[3];
Readers(String n, int u, String c, int g) {
names = n;
nums = u;
classes = c;
grade = g;
}
void searchAllBooks(Books[] all) {// 查书
for (int i = 0; i < 10; i++)
all[i].showBookInfo();
// self[score]=all[0];
}
void give(Books[] all, Books[] self) {// 还书
System.out.println("请输入您要还的书的书号");
int n = scan.nextInt();
for (int i = 0; i < 10; i++) {
if (n == all[i].num) {
for (int j = 0; j < 3; j++) {
if (self[j] == all[i]) {
self[j] = null;
System.out.println("还书成功");
}
}
}
}
}
void have(Books[] self) {
// for (int i = 0; i < 2; i++) {
// self[i].showBookInfo();
// }
for (int i = 0; i < 3; i++) {
if(self[i]!=null)
self[i].showBookInfo();
}
}
void giveMoney() {
}
void borrowBooks(Books[] all, Books[] self) {
System.out.println("请输入您要查找的书名:");
String n = scan.next();
int i;
for (i = 0; i < 10; i++) {
if (n.equals(all[i].name)) {
all[i].showBookInfo();
break;
}
}
//System.out.println("你要借吗?");
System.out.println("你要借吗?输入1表示借,其他数字表示不借。");
int j;
j = scan.nextInt();
if (j == 1) {
System.out.println("借阅成功");
//self[score] = all[9];
self[score] = all[i];
score += 1;
}
if (score < 4) {
System.out.println("您还可以借阅" + (3 - score) + "本");
} else {
System.out.println("对不起,一个人只能借3本");
}
}
}
class Books {
String name;
int num;
String ISBN;
String writer;
float price;
String publisher;
Books(String n, int u, String i, String w, float p, String l) {
name = n;
num = u;
ISBN = i;
writer = w;
price = p;
publisher = l;
}
void showBookInfo() {
System.out.println("**************************");
System.out.println("书名:" + name);
System.out.println("索书号:" + num);
System.out.println("ISBN号:" + ISBN);
System.out.println("价格:" + price);
System.out.println("出版社:" + publisher);
System.out.println("**************************");
}
}
----------------------------------------------------
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询