验证Java函数中的输入,以避免重复数据(非输入除外),默认值为0(无数据库) [英] Validating inputs in a function in java to avoid duplicate data other than non entry with default of 0 (No database )
本文介绍了验证Java函数中的输入,以避免重复数据(非输入除外),默认值为0(无数据库)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有三个整数变量,每个变量的默认值为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;
}
}
推荐答案
使用此:
boolean isInvalid = firstChoice!=0 ?
(firstChoice == secondChoice ||firstChoice == thirdChoice):
(secondChoice!=0 && secondChoice==thirdChoice);
if(isInvalid) {
return -2;
}
代码中的另一个错误,选择检查应包括8和0
Another bug in your code, check of choice should be inclusive of 8 and 0
if(choice >= 8 || choice =< 0){
return -1;
}
这篇关于验证Java函数中的输入,以避免重复数据(非输入除外),默认值为0(无数据库)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文