我有一个网络设计应用程序,它根据特定条件执行模拟遍历网络的数据包,如下所示: 当我单击Config按钮时,我有一个if..else语句,用于验证网络前缀(斜杠/ 48/64之后的数字等)是否相同,如果它们相同,则数据包沿着线。
正如你所看到的,前两个前缀是相同的,因此数据包沿着线路(由绿线显示),对于最后一台计算机,前缀是64,它没有&能够到达那里。
这个if语句应该检查:
CODES
/* enter is the ip entered */
if (enter != null) {
/*Verify if all prefixes are the same*/
/* s is the array that contains the prefixes */
if (s[0] == s[1]) {
/*every code in the if statement is for the animations*/
var start = $("#sldr");
if (!start.hasClass('started')) {
start.addClass('started');
$('#sldr').css({
"left": startx[0],
"top": 160 + starty[0],
"visibility": "visible"
});
$("#sldr").css({
'display': 'block',
'transition': 'none',
'width': '50px'
}).animate({
left: endx[0] - 15,
top: 160 + endy[0]
}, 2000,
function () {
//add if statements here
//for lines: give a specific color of line and specify which kind of cables it is
//retrieve the values and add conditions, same for the devices
//save the line coordinates in an array and use it for the animation
paths[0].attr("stroke", "green");
start.removeClass('started');
$('#sldr').css('visibility', 'hidden'); //remove this for ease in width
$('#img2').css('visibility', 'visible');
});
}
}
else if (s[1] == s[2]) {
var start2 = $("#img2");
if (!start2.hasClass('started')) {
start2.addClass('started');
$('#img2').css({
"left": startx[1],
"top": 160 + starty[1]
});
$("#img2").delay(2000).css({
'display': 'block',
'transition': 'none',
'width': '50px'
}).animate({
left: endx[1] - 15,
top: 160 + endy[1]
}, 2000,
function () {
//add if statements here
//for lines: give a specific color of line and specify which kind of cables it is
//retrieve the values and add conditions, same for the devices
//save the line coordinates in an array and use it for the animation
paths[1].attr("stroke", "green");
start2.removeClass('started');
$('#img2').css('visibility', 'hidden'); //remove this for ease in width
});
}
}
else {
$('#errorPrefix').dialog({ //dialog box for error message that prefix is not the same
height: 190,
width: 330,
modal: true,
buttons: {
Ok: function () {
$(this).dialog("close");
}
},
resizable: false,
dialogClass: 'no-close error-dialog'
});
}
}
else {
$('#error').dialog({ //dialog for the error message that ip hasn't been set
height: 190,
width: 330,
modal: true,
buttons: {
Ok: function () {
$(this).dialog("close");
}
},
resizable: false,
dialogClass: 'no-close error-dialog'
});
}
修改
/* enter is the ip entered */
if (enter != null) {
/*Verify if all prefixes are the same*/
/* s is the array that contains the prefixes */
if (s[0] == s[1]) {
/*every code in the if statement is for the animations*/
}
else if (s[1] == s[2]) {
//animations
}
else {
//dialog box for error message that prefix is not the same
}
}
else {
//dialog for the error message that ip hasn't been set
}
这里的问题是显示前缀不相同的错误消息的对话框没有打开。
if..else语句中有任何问题吗?
答案 0 :(得分:1)
您当前的逻辑被破坏了:您正在测试前2个前缀是否相同,如果是,您将永远不会输入其他else ìf
和else
。
你应该这样做:
if (enter != null) {
/*Verify if all prefixes are the same*/
if (s[0] == s[1] && s[1] == s[2]) {
// 1st animation
// 2nd animation
}
else {
//dialog box for error message that prefix is not the same
}
}
else {
//dialog for the error message that ip hasn't been set
}
根据关于上述答案的OP评论,这里不仅显示错误,而且不是所有前缀都相同,但仍显示相同的动画:
if (enter != null) {
if (s[0] == s[1]) {
// 1st animation
if (s[1] == s[2] {
// 2nd animation
}
}
// Add error if not all prefixes are the same
if (s[0] != s[1] || s[1] != s[2]) {
//dialog box for error message that prefix is not the same
}
}
else {
//dialog for the error message that ip hasn't been set
}
请注意,第二个动画会保留第一个动画成功。