java - 注册课程 Java
问题描述
我一直在研究为学生注册课程的代码。我得到了一份教师副本,需要为 validateChoice 方法编写代码。我已经完成了,但我仍然遇到两个错误。该程序将 0 识别为重复而不是无效的课程编号,并允许用户在抛出无效消息之前重复两次。不允许有重复。关于为什么会发生这种情况的任何想法?我已经把它上交了,因为它与我为作业提供的课程代码正确运行,我只是想知道以供个人参考。谢谢!
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package u6a1_consoleregisterforcourse;
import java.util.Scanner;
/**
*
* @author omora
*/
public class U6A1_ConsoleRegisterForCourse {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
System.out.println("Kim's Copy");
Scanner input = new Scanner(System.in);
//choice is the current menu selection
//firstChoice is the first menu selection mande by the user
//secondChoice is the second menu selection mande by the user
//thirdChoice is the third menu selection mande by the user
// a choice of 0 means the choice has not been made yet
int choice;
int firstChoice = 0, secondChoice = 0, thirdChoice = 0;
int totalCredit = 0;
String yesOrNo = "";
do {
choice = getChoice(input);
switch (ValidateChoice(choice, firstChoice, secondChoice, thirdChoice, totalCredit)) {
case -1:
System.out.println("**Invalid** - Your selection of " + choice + " is not a recognized course.");
break;
case -2:
System.out.println("**Invalid** - You have already registerd for this " + ChoiceToCourse(choice) + " course.");
break;
case -3:
System.out.println("**Invalid** - You can not register for more than 9 credit hours.");
break;
case 0:
System.out.println("Registration Confirmed for course " + ChoiceToCourse(choice) );
totalCredit += 3;
if (firstChoice == 0)
firstChoice = choice;
else if (secondChoice == 0)
secondChoice = choice;
else if (thirdChoice == 0)
thirdChoice = choice;
break;
}
WriteCurrentRegistration(firstChoice, secondChoice, thirdChoice);
System.out.print("\nDo you want to try again? (Y|N)? : ");
yesOrNo = input.next().toUpperCase();
} while (yesOrNo.equals("Y"));
System.out.println("Thank you for registering with us");
}
public static int getChoice(Scanner input) {
System.out.println("Please type the number inside the [] to register for a course");
System.out.println("[1]IT4782\n[2]IT4784\n[3]IT4786\n[4]IT4789\n[5]IT2230\n[6]IT3345\n[7]IT3349");
System.out.print("Enter your choice : ");
return (input.nextInt());
}
//This method validates the user menu selection
//against the given registration business rules
//it returns the following code based on the validation result
// -1 = invalid, unrecognized menu selection
// -2 = invalid, alredy registered for the course
// -3 = invalid, No more than 9 credit hours allowed
// 0 = menu selection is valid
public static int ValidateChoice(int choice, int firstChoice, int secondChoice, int thirdChoice, int totalCredit) {
// TO DO - Add Code to:
// Validate user menu selection (the int choice method argument)
// against the given registration business rules
int result = 0;
if (choice <=0 ||choice >7){
result = -1;
}
if(choice==firstChoice||choice == secondChoice||choice == thirdChoice) {
result = -2;
}
if(totalCredit == 3) {
result = 0;
}
if (totalCredit > 9) {
result = -3;
}
return result;
}
public static void WriteCurrentRegistration(int firstChoice, int secondChoice, int thirdChoice) {
if (firstChoice == 0)
System.out.println("Current course registration: { none } " );
else if (secondChoice == 0)
System.out.println("Current course registration: { " + ChoiceToCourse(firstChoice) + " }" );
else if (thirdChoice == 0)
System.out.println("Current course registration: { " + ChoiceToCourse(firstChoice) +
", " + ChoiceToCourse(secondChoice) + " }");
else
System.out.println("Current course registration: { " + ChoiceToCourse(firstChoice) +
", " + ChoiceToCourse(secondChoice) + ", " + ChoiceToCourse(thirdChoice) + " }");
}
public static String ChoiceToCourse(int choice) {
String course = "";
switch (choice)
{
case 1:
course = "IT4782";
break;
case 2:
course = "IT4784";
break;
case 3:
course = "IT4786";
break;
case 4:
course = "IT4789";
break;
case 5:
course = "IT2230";
break;
case 6:
course = "IT3345";
break;
case 7:
course = "IT3349";
break;
default:
break;
}
return course;
}
}
解决方案
推荐阅读
- c - SWIG 在 perl 包中导致“符号查找问题”
- javascript - 将事件侦听器添加到 svg 的动态元素
- reactjs - 当父状态更新时,React 钩子会更新子状态
- c# - 使用 Steamworks.NET 包装器时未触发回调
- python - 如何从 PyQt5 中的对话框窗口触发主窗口中的方法?
- c# - 如何在水晶报表中字段字体颜色的不透明度
- android - 日期选择器于 1970 年 1 月 1 日重置
- java - “同步”会影响流的内存占用吗?
- azure-active-directory - Chevron.Directory.Graph:公共函数 IsInGroup(id As String) As Boolean 为来宾用户返回 false
- tensorflow - tf.data.Dataset:不能为给定的输入类型指定 `batch_size` 参数