英文:
Validating inputs in a function in java to avoid duplicate data other than non entry with default of 0 (No database )
问题
我有三个整数变量,每个都有默认值为0。我想将每个值更新为与其他值不同的值。如果我尝试将两个变量更新为相同的值,验证应该允许。我卡在程序的验证部分上。请问有人可以指导我吗?
public static int ValidateChoice(int choice, int firstChoice, int secondChoice, int thirdChoice, int totalCredit) {
// TO DO - Add Code to:
// 验证用户菜单选择(int choice 方法参数)
/* 根据以下给定的注册业务规则验证:
不允许注册程序未显示的其他课程
不允许多次注册相同的课程
不允许注册超过9学分(例如,不超过3门课程)
*/
// 事实上,选择只限于七个选项
// 超出七个或小于1都被视为无效
if (choice > 8 || choice < 0) {
return -1;
}
// 从这里开始是我的问题
/// 选择的两门课程不应相同
// 因此,通过if中的条件捕获不当提交的课程
// 初始状态 thirdChoice=0 secondChoice=0 firstChoice=0
else if (firstChoice == secondChoice || firstChoice == thirdChoice || secondChoice == thirdChoice) {
return -2;
}
// 到此为止
// 课程的总学分最多为9学时
else if (totalCredit > 9) {
return -3;
} else {
return 0;
}
}
英文:
I have three integer variable of which each has default value of 0. I want to update the values each to a value that is unique from the other. In case I try update two variable to same value I should be allowed by the validation. I am stuck on writing the validation part of the program. Please can anyone guide me?
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 below:
No registration of other courses not displayed by the program
No registration more than once for the same course
No registration for more than 9 credit hours (e.g. no more than 3
courses)
*/
//by fact the choice are limited to seven option
//anything beyond seven or below 1 is considered invalid
if(choice > 8 || choice < 0){
return -1;
}
//my problem is from here
///No two courses chosen should be the same
//therefore blacklisting the inappropriate submission of course is captured by the condition in the if
// initial state thirdChoice=0 secondChoice=0 firstChoice=0
else if((firstChoice==secondChoice || firstChoice== thirdChoice || secondChoice==thirdChoice)){
return -2;
}
// upto here
//the total credit for the course is upto 9 hours
else if(totalCredit > 9) {
return -3;
}
else {
return 0;
}
}
答案1
得分: 0
不太清楚如何处理默认值,我尝试提供两个选项。我假设return 0
是验证通过的返回值。
如果要求仅是在两个值相等时拒绝验证,那么只需具有以下条件即可:
if (firstChoice == secondChoice ||
firstChoice == thirdChoice || secondChoice == thirdChoice) {
return -2;
}
return 0;
与默认值无关。
如果要验证所有值为0的字段,检查非0字段是否相等,条件可以如下所示:
if ((firstChoice != 0 && firstChoice == secondChoice) ||
(firstChoice != 0 && firstChoice == thirdChoice) || (secondChoice != 0 && secondChoice == thirdChoice)) {
return -2;
}
return 0;
这种方式可以验证值为0的字段。
英文:
As not very clear what to do with defaults, i try to post 2 options. I assume return 0
as the validation passed return value.
If the requirement is simply to reject validation if 2 values are equal, it should be enough to have the condition
if( firstChoice==secondChoice ||
firstChoice== thirdChoice || secondChoice==thirdChoice){
return -2;
}
return 0;
indipendently from defaults.
If instead you should validate anyway all fields with value 0 and check for equality the non-0 fields, the condition can became as follows
if( (firstChoice!=0 && firstChoice==secondChoice) ||
(firstChoice!=0 && firstChoice== thirdChoice) || (secondChoice !=0 && secondChoice==thirdChoice)){
return -2;
}
return 0;
in this way fields with value 0 are validated
答案2
得分: 0
使用这个:
boolean isInvalid = firstChoice != 0 ?
(firstChoice == secondChoice || firstChoice == thirdChoice) :
(secondChoice != 0 && secondChoice == thirdChoice);
if (isInvalid) {
return -2;
}
你代码中还有另一个bug,检查选择应该包括8和0:
if (choice >= 8 || choice <= 0) {
return -1;
}
<details>
<summary>英文:</summary>
Use this:
boolean isInvalid = firstChoice!=0 ?
(firstChoice == secondChoice ||firstChoice == thirdChoice):
(secondChoice!=0 && secondChoice==thirdChoice);
if(isInvalid) {
return -2;
}
Another bug in your code, check of choice should be inclusive of 8 and 0
if(choice >= 8 || choice =< 0){
return -1;
}
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论