private void run() throws Exception { int choice = getChoice(); while (choice != 7) { if (choice == 0) { displayCustomers(); } else if (choice == 1) { stdOut.println("All the Restaurants List:\n"); displayShopList(); } else if (choice == 2) { System.out.println("Please input the name of the customer "); String personName = stdIn.readLine(); displayShopList(personName);// get methoid need overrided !!!! } else if (choice == 3) { System.out.println("Please input the idcode of the restaurant "); String shopIdCode = stdIn.readLine(); displayFoodList(shopIdCode); } else if (choice == 4) { displayRecordList(); } else if (choice == 5) { System.out.println("Please input the idCode of the record "); String idCode = stdIn.readLine(); displayRecord(idCode); } else if (choice == 6) { registerOrder(); } choice = getChoice(); } }
时间: 2024-03-28 11:36:40 浏览: 22
这段代码定义了一个名为run的私有方法,其抛出异常类型为Exception。该方法包含一个while循环,用于循环执行用户的选择操作。循环内部根据用户的选择来执行不同的操作,具体如下:
1. 如果用户的选择是0,则调用displayCustomers方法显示所有顾客的信息。
2. 如果用户的选择是1,则调用displayShopList方法显示所有餐厅的信息。
3. 如果用户的选择是2,则提示用户输入顾客的姓名,并调用displayShopList方法显示该顾客可以订餐的餐厅列表。
4. 如果用户的选择是3,则提示用户输入餐厅的idCode,并调用displayFoodList方法显示该餐厅的所有菜品信息。
5. 如果用户的选择是4,则调用displayRecordList方法显示所有订单的信息。
6. 如果用户的选择是5,则提示用户输入订单的idCode,并调用displayRecord方法显示该订单的详细信息。
7. 如果用户的选择是6,则调用registerOrder方法进行订单的注册。
其中,stdIn和stdOut分别是标准输入和标准输出。除了registerOrder方法之外,其他方法的具体实现还需要查看代码中的其他部分。
相关问题
public static void method() throws ArithmeticException{ int a=10/0; }是否生效
是的,上述代码中的`throws ArithmeticException`是生效的。
在Java中,当一个方法使用`throws`关键字声明了某个异常类型时,它表明该方法可能会抛出该异常。在你的例子中,`method()`方法使用`throws ArithmeticException`声明,意味着该方法可能会抛出算术异常(ArithmeticException)。
在方法体中,你使用了除以零的表达式`int a=10/0;`,这会导致一个算术异常。由于你在方法声明中声明了这个异常类型,所以编译器会允许你的代码通过编译。
然而,请注意,尽管这段代码可以通过编译,但在运行时会抛出一个算术异常。如果你调用了这个方法而没有进行异常处理,程序会终止并打印出异常信息。
所以,当你调用这个方法时,你需要使用`try-catch`语句块来捕获并处理这个异常,或者继续使用`throws`关键字将异常传递给调用者进行处理。
希望能够解答你的问题!如有其他疑问,请随时提问。
private static void readTextFile() throws Exception{ String filename = "d:/tomxd/BookDao.java"; findBookDao findBookDao = new findBookDao(); List<String> lines = findBookDao.readTextFile(filename); int pageSize = 10; int pageNum = 1; int totalPages = (int) Math.ceil(lines.size() * 1 / pageSize); while (true) { System.out.println("第 " + pageNum + " 页,共 " + totalPages + " 页"); int start = (pageNum - 1) * pageSize; int end = Math.min(start + pageSize, lines.size()); for (int i = start; i < end; i++) { System.out.println(lines.get(i)); } int choice = Tool.getInt("请输入选项:0.退出 1.首页 4.上一页 5.下一页 9.尾页"); if (choice == 0) { break; } if (choice == 1) { pageNum = 1; } if (choice == 4) { pageNum = Math.max(pageNum - 1, 1); } if (choice == 5) { pageNum = Math.min(pageNum + 1, totalPages); } if (choice == 9) { pageNum = totalPages; } } }解释一下这段代码块
这段代码是一个用于分页显示文本文件内容的程序。程序首先读取指定文件(这里是"d:/tomxd/BookDao.java")的所有行,然后每页显示指定的行数(这里是10行),并且可以根据用户输入选择不同的操作,比如翻页、返回首页等。
具体来说,程序通过Math.ceil()方法计算出总页数,然后进入一个循环,每次输出当前页的内容,并等待用户输入操作。如果用户输入0,则跳出循环退出程序;如果输入1,则返回第一页;如果输入4,则显示上一页;如果输入5,则显示下一页;如果输入9,则跳转到最后一页。其中,pageNum表示当前页码,totalPages表示总页数。