黑马程序员技术交流社区
标题:
二维数组排序问题
[打印本页]
作者:
余琪琪
时间:
2014-3-26 21:36
标题:
二维数组排序问题
本帖最后由 余琪琪 于 2014-3-27 00:36 编辑
import java.util.Scanner;
public class BMS_0324
{
static int count = 1;//二维数组字符串
static String[][] book = new String[10000][6];
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) throws Exception
{
printWelcome();
main:while(true)//主界面
{
System.out.println("请选择服务:(1)显示图书 (2)增加图书 (3)删除图书 (4)修改信息 (5)退出");
int Select = sc.nextInt();
if(Select == 1)
{
showBooks();
}
//增加图书模块
else if(Select == 2)
{
addBook();
}//增加模块到这里结束
else if(Select == 3)
{
deleteBook();
}
else if(Select == 4)
{
amendBook();
}
//退出模块
else if(Select == 5)
{
exitSystem();
}//退出模块到这里结束
}
}
//--------------------------------------------------------------------------------------------------------------------------------------------------
public static void printWelcome()
{
//输出行数15,列数60的空心长方形框
for(int x=1;x<=15;x++)//外部循环
{
//暂时只能这样输出中间那行
if(x==8)
{
System.out.print("* 欢迎进入图书管理系统 *");
}else
{
for(int y=1;y<=60;y++)//内部循环
{
if(x==1 || x==15 || y==1 || y==60)//打印四个边框
{
System.out.print("*");
}else
{
System.out.print(" ");//打印空心部分
}
}
}
System.out.println();//每一行换行
}
}
//增加模块
public static void addBook() throws Exception
{
add:while (true)//增加模块界面
{
System.out.println("请输入您需要增添的书名:");
String name = sc.next();
System.out.println("请输入此书的作者:");
String author = sc.next();
System.out.println("请输入此书的价格:");
String price = sc.next();
System.out.println("请输入此书的出版社:");
String publish = sc.next();
System.out.println("请输入此书的日期:");
String date = sc.next();
System.out.println("请输入此书的书号:");
String isbn = sc.next();
book[count-1][0] = name;
book[count-1][1] = author;
book[count-1][2] = price;
book[count-1][3] = publish;
book[count-1][4] = date;
book[count-1][5] = isbn;
System.out.println("尊敬的用户,你输入的新书信息如下:");
System.out.println("书名:" + name+ "作者:" + author + "价格:"+ price + "出版社:" + publish + "日期:" + date + "书号:"+ isbn);
System.out.println("请核对后选择是否保存:(1)是(2)否");
String answer;
affirm:while(true)//增加图书-保存模块
{
answer = sc.next();
//保存
if(answer.equals("1"))
{
count++;//下次就保存到二维数组的下一个
System.out.print("正在保存请稍后");
for(int i=0;i<6;i++)
{
Thread.sleep(300);
System.out.print(".");
}
System.out.println("");
System.out.println("已保存,是否继续录入?(1)是(2)否");
Scanner sc = new Scanner(System.in);
String answer1 = sc.next();
if(answer1.equals("1"))
{
break affirm;//跳出"增加图书-保存模块",到"增加模块主界面"
}
else if(answer1.equals("2"))
{
break add;//跳到"增加模块主界面",到"主界面"
}
else
{
System.out.println("输入错误,请重新输入:");
continue;
}
}
//不保存
else if(answer.equals("2"))
{
for(int i=0;i<6;i++)
{
book[count-1][i]=null;
}
System.out.println("已放弃保存,是否继续录入?(1)是(2)否");
answer = sc.next();
if(answer.equals("1"))
{
break affirm;//跳出"增加图书-保存模块",到"增加模块主界面"
}
else if(answer.equals("2"))
{
break add;//跳到"增加模块主界面",到"主界面"
}
else
{
System.out.println("输入错误,请重新输入:");
continue;
}
}
else
{
System.out.println("输入错误,请重新输入:");
continue;
}
}//增加图书-保存模块到这里结束
}//增加模块主界面到这里结束
}
//查看模块
public static void showBooks()
{
if(book[0][0]==null)
{
System.out.println("尊敬的用户,很抱歉,您尚未录入图书");
}
else if(count == 2)
{
for(int i=0;i<count-1;i++)
{
System.out.println("书名:" + book[i][0] + "作者:" + book[i][1] + "价格:"+ book[i][2] + "出版社:" + book[i][3] + "日期:" + book[i][4] + "书号:"+ book[i][5]);
}
}
else
{
for(int x=0;x<count;x++)
{
for(int y=0;y<count-x-1;y++)
{
double n = Double.parseDouble(book[count-1][2]);
double m = Double.parseDouble(book[count-1][2]);
if(n>m)
{
for(int z=0;z<6;z++)
{
String temp;
temp = book[x][z];
book[x][z]=book[y][z];
book[y][z]=temp;
}
}
}
}
for(int i=0;i<count-1;i++)
{
System.out.println("书名:" + book[i][0] + "作者:" + book[i][1] + "价格:"+ book[i][2] + "出版社:" + book[i][3] + "日期:" + book[i][4] + "书号:"+ book[i][5]);
}
}
}
//删除模块
public static void deleteBook()
{
System.out.println("进入删除模块");
}
//修改模块
public static void amendBook()
{
System.out.println("进入修改模块");
}
//退出模块
public static void exitSystem()
{
System.out.println("是否确认退出?(1)是(2)否");
String answer;
exit:while(true)
{
answer = sc.next();
if(answer.equals("1"))
{
System.out.print("谢谢使用,再见!");
System.exit(0);
}
else if(answer.equals("2"))
{
break exit;
}
else
{
System.out.println("输入错误,请重新输入:");
continue;
}
}
}
}
复制代码
count是计算图书保存次数的计数器,初始化为一.编译时显示空指针异常.求解!!!!!!!!!!!!
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2