我正在制作一个小应用程序,允许学生在选中时加入课程。我想让用户能够按ID选择学生,按ID选择一个班级并将用户添加到该班级。这是有效的,但是在添加学生之后,我希望程序询问学生是否想要加入另一个课程,而不是重置整个课程并且必须返回。
我认为我接近解决方案,但不知道如何让我的方法(signUp())回到方法的中途。这有意义吗?一旦学生进入课堂并选择“加入另一个班级”,我想返回代码中的粗体评论。
对不起,如果这是啰嗦和混乱,我是新手,所以我感谢你的耐心!
static void signUp() {
System.out.println("\nSign Up For a Class\n");
try {
Scanner input = new Scanner(System.in);
System.out.println("Enter Student ID: ");
String user_entered_student_id = input.nextLine();
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/ClassSelector?autoReconnect=true&useSSL=false", "", "");
Statement myStmt = con.createStatement();
Statement myStmt2 = con.createStatement();
Statement myStmt3 = con.createStatement();
ResultSet rs;
rs = myStmt.executeQuery("SELECT student_name FROM ClassSelector.students WHERE student_id = " + user_entered_student_id);
while (rs.next()) {
String userEnterId = rs.getString("student_name");
System.out.println("Is " + userEnterId + " the correct student? (Y/N)");
String confirm = input.nextLine();
if (confirm.equals("Y") || confirm.equals("y")) {
ResultSet rs2 = myStmt2.executeQuery("SELECT * FROM ClassSelector.classes");
while (rs2.next()) {
String avlClasses = rs2.getString("class_id") + "\t" + rs2.getString("classname") + "\t" + rs2.getString("description");
System.out.println(avlClasses);
}
} else if (confirm.equals("N") || confirm.equals("n")) {
System.out.println("Oops, let start over");
return;
}
**//RETURN TO THIS SECTION OF CODE AND PROCEEED**
System.out.println("Enter Class ID from Classes Listed Above to Join: ");
String selectedClass = input.nextLine();
ResultSet rs3 = myStmt3.executeQuery("SELECT * FROM ClassSelector.classes WHERE class_id = " + selectedClass);
while (rs3.next()) {
String innerJoin = (userEnterId + " has been added to " + rs3.getString("classname") + " " + rs3.getString("class_id"));
System.out.println(innerJoin);
String student_classJoin = "INSERT IGNORE INTO student_x_class" + "(student_id,student_name, class_id, classname)" + "VALUES (?, ?, ?, ?)";
PreparedStatement pStmt = con.prepareStatement(student_classJoin);
pStmt.setString(1, user_entered_student_id);
pStmt.setString(2, userEnterId);
pStmt.setString(3, rs3.getString("class_id"));
pStmt.setString(4, rs3.getString("classname"));
pStmt.executeUpdate();
System.out.println("Would you like to enroll " + userEnterId + " into another class? (Y/N)");
String additionalClass = input.nextLine();
if(additionalClass.equals("Y") || additionalClass.equals("y")){
signUp();
}
}
}
} catch (java.sql.SQLException SQL) {
SQL.printStackTrace();
} catch (Exception EXC) {
EXC.printStackTrace();
}
}
答案 0 :(得分:2)
此代码非常令人困惑。但是,让我们退后一步,看看一些伪代码,它可能代表您正在寻找的那种结构。
让我们从正在执行的操作开始。像这样:
If ThisWorkbook.Sheets("INDIVIDUAL PERFORMANCE SUMMARY").Range("C1") <> "TRUE" And ThisWorkbook.Sheets("INDIVIDUAL PERFORMANCE SUMMARY).Range("B226").value <> 0 Then
细节是特定于实现的,但一般来说,您的主要逻辑方法在语义上看起来应该非常相似。实际上,将代码提取到方法名称描述正在执行的操作的小型单个方法中通常是非常好的主意。然后,整个步骤序列就像一个描述业务流程的简短故事。
现在,你想重复那个结构?嗯,那是什么循环。像这样:
studentID = prompt("enter a student ID");
student = get_from_data(studentID);
courseID = prompt("enter a course ID");
course = get_from_data(courseID);
insert_in_data(studentID, courseID);
现在我们只需要定义那个条件。它基于用户输入,对吧?所以像这样:
while(!someTerminatingCondition) {
// the steps above
}
为了向Google提供一些内容,您所描述的内容通常被称为游戏循环&#34;。这个想法是整个过程应该反复重复,直到满足某些特定条件。 (在游戏中,这种情况将决定胜利者。)