Java开发预约中心程序时循环内赋值变量无法外部调用问题求助
问题原因
- 局部变量未初始化:Java方法内的局部变量没有默认初始值,你声明的
name和time仅在用户选择「1. 预约」的分支才会被赋值,若用户未创建预约直接触发查询操作,变量处于未初始化状态,触发编译报错。 - 逻辑缺陷:查询分支的
do-while循环条件appointment<appointment+1永远为真,会触发死循环;现有逻辑仅支持存储单条预约,多次提交预约会覆盖之前的记录;Scanner在循环内重复创建,且nextInt()读取后残留的换行符会导致后续nextLine()读取到空值。
单预约场景最简修复
仅需支持存储一条预约的场景,可通过初始化变量、修复查询逻辑解决报错:
import java.util.Scanner; public class appointment { public static void main(String[]args) { int input; int appointment = 0; // 给变量赋初始值解决未初始化报错 String name = null; int time = 0; // Scanner移到循环外,避免重复创建 Scanner sc = new Scanner(System.in); do { System.out.println("Welcome to ACEPrep Appointment Center! How can we help you? \n(1. Make an appointment 2. Check Appointment Details 3. Check Number of Appointments 4. Leave)"); input=Integer.parseInt(sc.nextLine()); if(input==1) { System.out.println("Name:"); name=sc.nextLine(); System.out.println("Select a time between 1-8 PM:"); time=sc.nextInt(); // 读取nextInt残留的换行符,避免下次循环nextLine读空 sc.nextLine(); System.out.println("Saved! You have an appointment at " + time +" PM with " + name); appointment++; } if(input==2) { // 先判断是否有预约,避免输出空值 if(appointment == 0) { System.out.println("You have no appointments yet."); } else { System.out.println("You have an appointment with " + name + " at " + time + " PM"); } } if(input==3) { System.out.println("You have " + appointment + " appointment(s)."); } if(input==4) { System.out.println("Thank you for using us! Goodbye!"); break; } }while(input<4); sc.close(); } }
多预约场景优化方案
如果需要支持存储多条预约,可通过集合存储每条预约记录:
import java.util.ArrayList; import java.util.Scanner; // 预约实体类,存储每条预约的姓名和时间 class AppointmentItem { String name; int time; public AppointmentItem(String name, int time) { this.name = name; this.time = time; } } public class appointment { public static void main(String[]args) { int input; // 用ArrayList存储所有预约 ArrayList<AppointmentItem> appointmentList = new ArrayList<>(); Scanner sc = new Scanner(System.in); do { System.out.println("Welcome to ACEPrep Appointment Center! How can we help you? \n(1. Make an appointment 2. Check Appointment Details 3. Check Number of Appointments 4. Leave)"); input=Integer.parseInt(sc.nextLine()); if(input==1) { System.out.println("Name:"); String name=sc.nextLine(); System.out.println("Select a time between 1-8 PM:"); int time=sc.nextInt(); sc.nextLine(); appointmentList.add(new AppointmentItem(name, time)); System.out.println("Saved! You have an appointment at " + time +" PM with " + name); } if(input==2) { if(appointmentList.isEmpty()) { System.out.println("You have no appointments yet."); } else { System.out.println("Your appointments:"); for(int i=0; i<appointmentList.size(); i++) { AppointmentItem item = appointmentList.get(i); System.out.println((i+1) + ". Appointment with " + item.name + " at " + item.time + " PM"); } } } if(input==3) { System.out.println("You have " + appointmentList.size() + " appointment(s)."); } if(input==4) { System.out.println("Thank you for using us! Goodbye!"); break; } }while(input<4); sc.close(); } }
内容的提问来源于stack exchange,提问作者di on Minecraft
相关产品推荐
相关产品推荐

